Force Android GPS to update reading

Asked

Viewed 792 times

9

I recently added my application to collect functionality the latitute and longitude points of the GPS at which the device is located.

To do so I added the 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 in the Oncreate Event I instituted 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());
    }

This way this working, it happens that the returned point is not always the best. Most of the time that point is 50 meters away from the device and I don’t see how to update it. When this application ran on a device with Windows Mobile I read about 100 points and then took an average of those points and the resulting position was accurate in the centimeter range.

How can I 'force' you to refresh your reading as many times as I want?

1 answer

10


You can "force" a reading through one of the variants of the method requestSingleUpdate().

With requestLocationUpdates() cannot "force" updates, just set under what conditions a new update is done.

The frequency at which the method onLocationChanged() of Listener 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 number of milliseconds minimum between calls.
  • minDistance indicates the distance in metres the device has to move in order to have a new call to the method.

Notes:

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

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

  • minTime takes precedence over minDistance. When minDistance is different from 0 there will be an update if there is a position change higher than minDistance and if you have passed at least minTime millisecond.

Observing:
Consider using the Api Fused Location Provider that Google claims to have several advantages over Locationmanager. See the documentation on Making Your App Location-Aware.
This reply has an example of use.

  • Note that I passed 0.0 for the update to be maximum, but this does not occur. Updates occur, but I did not understand how. I’ve stayed in the same place and they occurred and I moved and they didn’t occur.

  • Yes, I understand the question of the reception and the number of satelites. It turns out that even in favorable conditions the point is inaccurate. The technique of reading several times and taking an average had a good effect on Windows Mobile devices.

  • Look how strange. I changed the parameters and put a minTime equal to 100. On the way back from lunch, I run my application and the coordinate he gives me is that of the restaurant, 200 meters away from where I am now.

  • Seeing that call to onLocationChanged(location); in the onCreate() it is possible that this is the LastKnownLocation. The fact of putting minTime igual a 100 does not mean that the method is called 100 in 100 milliseconds. GPS may not have been able to get a new location between the restaurant’s route to where it is now.

  • 1

    Now. Using the Locationservices Googleapiclient API calls to updates can be made quite easily and responses are much more accurate. Although I am in a covered area the point 'falls' exactly where I am. Thanks. Thanks even.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.