-1

I want to fetch the current location (latitude,longitude pair) of user and send it to server only when user has traveled 100 Kilo meters away from previous fetched location. What is the best way to do this if there is any?

My purpose is to use least possible resources of user while fetching location.

Tushar Kathuria
  • 645
  • 2
  • 8
  • 22

1 Answers1

0

There is an interface called LocationListener:

http://developer.android.com/reference/android/location/LocationListener.html

You can use your LocationManager like:

public void startLocationUpdates() {
    Criteria criteria = new Criteria();
    String bestProvider = locationManager.getBestProvider(criteria, false);
    locationManager.requestLocationUpdates(bestProvider, 60000, 10, new LocationListener() {

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

        @Override
        public void onProviderEnabled(String provider) {}

        @Override
        public void onProviderDisabled(String provider) {}

        @Override
        public void onLocationChanged(Location location) {
            System.out.println("LOCATION CHANGED: "+location);
        }
    });
}

public void stopLocationUpdates() {
    locationManager.removeUpdates(this);
}

The 10 is the minimum distance, and the 60000 in this case is the minimum time between requests.

breakline
  • 5,776
  • 8
  • 45
  • 84