• 15
name

A PHP Error was encountered

Severity: Notice

Message: Undefined index: userid

Filename: views/question.php

Line Number: 191

Backtrace:

File: /home/prodcxja/public_html/questions/application/views/question.php
Line: 191
Function: _error_handler

File: /home/prodcxja/public_html/questions/application/controllers/Questions.php
Line: 433
Function: view

File: /home/prodcxja/public_html/questions/index.php
Line: 315
Function: require_once

name Punditsdkoslkdosdkoskdo

android location update frequency

I want to get 5 consecutive update locations in order to check if the user is moving and to make sure the GPS is calibrated. I did the following:

I added android.permission.ACCESS_FINE_LOCATION" to the manifest and in onConnected:

mLocationRequest = new LocationRequest();
mLocationRequest.setPriority(LocationRequest.PRIORITY_HIGH_ACCURACY);
mLocationRequest.setInterval(1000); // Update location every second
mLocationRequest.setFastestInterval(1000);
LocationServices.FusedLocationApi.requestLocationUpdates(mGoogleApiClient, mLocationRequest, this); 

and in onLocationChanged I did the following:

locationRetries++;
switch (locationRetries) {
case 1: {
    firstLatitude = location.getLatitude();
    firstLongitude = location.getLongitude();               
}
case 5: {
    locationRetries = 0;
    lastLatitude = location.getLatitude();
    lastLongitude = location.getLongitude();
    accuracy = Math.round(location.getAccuracy());
    stopLocationUpdates();//will remove from location updates and disconnects
    float[] results = new float[1];
    Location.distanceBetween(firstLatitude, firstLongitude, lastLatitude, lastLongitude, results);              
    if (results[0] > 5)... //if moved at least 5meters, show popup that the user is moving else do your thing
}

Now I have 3 issues:

1) It seems to take much less than 5 seconds to retrieve the 5 updates even though I set both parameters to be 1000 milliseconds.

2) All the 5 locations (at least the first and last ones) are the same exact location even though I was walking fast. I thought I maybe moving too slow so

3) I closed my app, reopened it on a far location and pressed the button. Almost instantly I got the previous location. I pressed the button again and then I got the real location I was on. It's as if it didn't really asked/waited for a location from the GPS but instead took the last one which was remotely inaccurate at the time. I don't have any "get last known location" code.

I guess the bottom line would be: how can I make sure that it really asks the GPS where am I when I asked for the location and also when asking it for 5 consecutive times, to give me the real locations and not from the cache(?).

The Fused Location Provider

intelligently manages the underlying location technology and gives you the best location according to your needs.

  • Simple APIs: Lets you specify high-level needs like "high accuracy" or "low power", instead of having to worry about location providers.
  • Immediately available: Gives your apps immediate access to the best, most recent location.
  • Power-efficiency: Minimizes your app's use of power. Based on all incoming location requests and available sensors, fused location provider chooses the most efficient way to meet those needs.
  • Versatility: Meets a wide range of needs, from foreground uses that need highly accurate location to background uses that need periodic location updates with negligible power impact.

There is no guarantee that the fused location provider will ever use GPS - if it has a recent location of the accuracy you request it will return until a better location is returned (i.e., live GPS is returning accurate locations). This ensures that you'll get a better location sooner without waiting for GPS to be primed.

If you specifically need data from GPS, you need to use the LocationManager using the GPS_PROVIDER.

If you are trying to determine what the user is currently doing, you can instead use the ActivityRecognitionApi, which returns DetectedActivity such as WALKING or STILL: using that would give a faster method to understand what the user is currently doing.

  • 2
Reply Report
      • 2
    • GPS will always be slower than using Fused Location Provider and since Fused Location Provider uses GPS when it returns the best location (GPS on first start can be very inaccurate), you're much more likely to get good accuracy location with Fused Location as well.
      • 2
    • Added a note about using the ActivityRecognitionApi, which might be an easier way to understand if the user is walking/running/biking vs standing still
    • I will check it regarding walking/still etc. thanks. The problem was that it gave me the last known location instead of where I'm at the moment, only a retry gave me exactly where I was at that time. Also, it took less than 5 seconds to make 5 request locations even though I set it to 1000 milliseconds.
    • I'm using the fused location provider. I understood it's the recommended way for this but apparently it is not as fast and accurate as I was hoping it will be. Is there a better way to make sure it will be more accurate? Maybe force it to use the GPS if available (I'm forcing the user to turn it on)?