android location update frequency

泄露秘密 提交于 2020-01-04 06:57:15

问题


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(?).


回答1:


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.



来源:https://stackoverflow.com/questions/27851816/android-location-update-frequency

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!