Force Android GPS to refresh reading

9

I recently added my application to the collect feature the latitude and longitude points of the GPS on which the device is located.

I've added lines to AndroidManifest.xml:

<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION"/>   
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION"/>

I created an Activity that implements LocationListener

public class MinhaClasse extends FragmentActivity implements LocationListener {

and the OnCreate Event instantiated the classes that will be used.

   locationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
   Criteria criteria = new Criteria();
   String provider = locationManager.getBestProvider(criteria, false);


   location = locationManager.getLastKnownLocation(provider);
   locationManager.requestLocationUpdates( LocationManager.GPS_PROVIDER, 0, 0, this );


   if (location != null) {
       onLocationChanged(location);
   } else {
       .... "Localização não disponível"
   }


@Override
    public void onLocationChanged(Location location) {
        float lat = (float) (location.getLatitude());
        float lng = (float) (location.getLongitude());
    }

In this way it is working, it happens that the point returned is not always the best. Most of the time this point is 50 meters away from the device and I do not see how to update it. When this application was running on a Windows Mobile device I read a 100 points and then took an average of those points and the resulting position was accurate in the case of centimeters.

How can I 'force' you to update the reading as many times as I want?

    
asked by anonymous 20.02.2017 / 15:21

1 answer

9

You can "force" a read through one of the requestSingleUpdate () .

With requestLocationUpdates() you can not "force" updates, just define under what conditions a new update is done.

The frequency at which the onLocationChanged() of listener method is called is determined by the values passed to the second and third parameters of requestLocationUpdates()

void requestLocationUpdates (String provider, 
                long minTime, 
                float minDistance, 
                LocationListener listener)
  • minTime indicates the minimum number of milliseconds between calls.
  • minDistance indicates the distance in meters that the device must move to have a new method call.

Notes:

  • The accuracy of the location is not determined by the number of times the reading is done but by the GPS quality and reception conditions (number of satellites).

  • If you pass the value 0 to minTime the method is called when a read is obtained and will not be called again until there is a change of position in minDistance .

    / li>
  • minTime takes precedence over minDistance . When minDistance is different from 0 there will be an update if there is a position change greater than minDistance and if you have passed minTime milliseconds.

Note:
Consider using the Fused Location Provider that Google claims to have several advantages about the LocationManager. Here's how to use it in Making Your App Location-Aware .

    
20.02.2017 / 15:41