O
O
OM12016-09-07 23:04:07
Android
OM1, 2016-09-07 23:04:07

Android: Why is location manager constantly updating position?

Hello, two similar methods :

LocationListener locationListener = new LocationListener() {
            public void onLocationChanged(Location location) {
                // Called when a new location is found by the network location provider.
                //makeUseOfNewLocation(location);
                Toast.makeText(getApplicationContext(), "test:"+Double.toString(location.getLatitude()), Toast.LENGTH_SHORT).show();
            }

            public void onStatusChanged(String provider, int status, Bundle extras) {}

            public void onProviderEnabled(String provider) {}

            public void onProviderDisabled(String provider) {}
        };


        if ( Build.VERSION.SDK_INT >= 23 &&
                ContextCompat.checkSelfPermission( getBaseContext(), android.Manifest.permission.ACCESS_FINE_LOCATION ) != PackageManager.PERMISSION_GRANTED &&
                ContextCompat.checkSelfPermission( getBaseContext(), android.Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) {
        }

        LocationManager locationManager = (LocationManager) this.getSystemService(Context.LOCATION_SERVICE);
        locationManager.requestLocationUpdates(LocationManager.NETWORK_PROVIDER, 0, 0, locationListener);
        locationManager.removeUpdates(locationListener);

private GoogleMap.OnMyLocationChangeListener myLocationChangeListener = new GoogleMap.OnMyLocationChangeListener() {
        @Override
        public void onMyLocationChange(Location location) {
            LatLng loc = new LatLng(location.getLatitude(), location.getLongitude());
            //mMarker = mMap.addMarker(new MarkerOptions().position(loc));
            Toast.makeText(getApplicationContext(), location.getLatitude()+" "+location.getLongitude(), Toast.LENGTH_SHORT).show();
            if(mMap != null){
                mMap.animateCamera(CameraUpdateFactory.newLatLngZoom(loc, 16.0f));
            }
        }
    };




mMap.setOnMyLocationChangeListener(myLocationChangeListener);

The coordinates are determined, onMyLocationChange works as it should.
But both methods always work, i.e. with a given period, I don’t understand where this period came from and how to work with it?
For example - call the definition of coordinates and process the result?
Thank you!

Answer the question

In order to leave comments, you need to log in

1 answer(s)
D
Denis, 2016-09-07
@OM1

I myself did not fully understand the documentation, and therefore it is rather difficult to explain, but the whole point is:
requestLocationUpdates(long, float, android.locati...
And the idea is something like this:
There is a minimum distance and a minimum update time.
The minimum update time is to talk about that the data will be updated NO MORE THAN this time.This is done to save battery.Also, the
data will not come if the position has not changed within the minimum distance.o if it has changed, it will come no more often than the "minimum time".
But if there are zeros everywhere, then everything is more complicated there. Updates are constantly going on. And apparently, as often as the provider gives them.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question