Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

onlocationChanged is called even if I am sitting at the same place

I have used the code below and everything is working fine except that onLocationChanged is called even if I am sitting at the same location .

I thought it should be called only when I am moving right ?

I only want to get the location after I have moved a certain distance.

Please help me out. Thanks in advance.

 @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);

    locationMgr = (LocationManager) getSystemService(Context.LOCATION_SERVICE);

    locationMgr.requestLocationUpdates(LocationManager.NETWORK_PROVIDER, 0, 0,
    this);


}

    @Override
       public void onLocationChanged(Location location) {
           Toast.makeText(this, "Working!", Toast.LENGTH_SHORT).show();
                    if (location != null) {
                            double lat = location.getLatitude();
                            double lng = location.getLongitude();

                            String Text = "Latitud = " + lat + "\nLongitud = " +
     lng;

      Toast.makeText(getBaseContext(),Text,Toast.LENGTH_SHORT).show();
                    }
           }
like image 943
James Patrick Avatar asked Dec 17 '25 03:12

James Patrick


1 Answers

You're requesting location updates at the shortest possible intervals/distances

locationMgr.requestLocationUpdates(LocationManager.NETWORK_PROVIDER, 0, 0,
this);

This is what the documentation says about those parameters

" The location update interval can be controlled using the minTime parameter. The elapsed time between location updates will never be less than minTime, although it can be more depending on the Location Provider implementation and the update interval requested by other applications. "

The minDistance parameter can also be used to control the frequency of location updates. If it is greater than 0 then the location provider will only send your application an update when the location has changed by at least minDistance meters, AND at least minTime milliseconds have passed. However it is more difficult for location providers to save power using the minDistance parameter, so minTime should be the primary tool to conserving battery life.

I personally use a minTime of 10 seconds and 10 meters for my app

locationManager.requestLocationUpdates(LocationManager.NETWORK_PROVIDER, 10000,
                10, locationListener);
like image 149
Jade Byfield Avatar answered Dec 19 '25 18:12

Jade Byfield