How often to update gps location?

I am writing an application that shows your location on a google map ... so far, so good ... It correctly shows my location.

The problem is that this gps does not update if I move (or this happens, but only after a while)

Does anyone know how to do it the way native google maps do (for android)? This means that if you press "my location", a flashing blue dot will appear on the screen, which changes as you move ...

This is my code:

//Initializing the listeners locationManager.requestLocationUpdates(LocationManager.NETWORK_PROVIDER, 35000, 10, networkLocationListener); locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 35000, 10, gpsLocationListener); //and now, in both 'networkLocationListener' and 'gpsLocationListener' //I overwrite the method 'onLocationChanged': @Override public void onLocationChanged(Location location) { Log.i("test", "New network location: "+location.getLatitude()+ " , "+location.getLongitude()); myLongitude= location.getLongitude(); myLatitude= location.getLatitude(); } //and the variables myLongitude and myLatitude are the only ones that I need to display my //position in the map... 

Does anyone know if I missed something?

+4
source share
2 answers

Calling the method you use is requestLocationUpdates(String provider, long minTime, float minDistance, LocationListener listener) and you tell it to provide you with updates every 35000 ms or every 35 seconds, Try to reduce this number to what suits you . A lower number will mean more battery use.

If you need an indicator on a MapView , check out MyLocationOverlay .

+5
source

You would not want to leave it like this forever, but it would be worth setting the minTime and minDistance values ​​to zero if you have not given this snapshot yet.

 locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 0, gpsLocationListener); 

If minTime is greater than 0, the LocationManager can potentially take a rest in milliseconds of minTime between location updates to save energy. If minDistance is greater than 0, the location will be broadcast only if the device is moved using the minDistance counters. To receive notifications as often as possible, set both parameters to 0.

- requestLocationUpdates Reference

+1
source

Source: https://habr.com/ru/post/1383954/


All Articles