Android – How to get location and its updates in Google Maps Android API V2

androidgoogle-maps-android-api-2

I want to enable location in Google Maps Android API v2. What I want is:

  1. load the map centered on a fix latitude/longitude,.
  2. Start listening to location changes, and once a provider (network/GPS) gets the location, center the map on this location and animate it (animation shall only run once). Meanwhile, location listener updates user's location after specified amount of time,
  3. Store this location data and use it to fetch nearby places of interest via my API.

I'm kind of confused with how to get periodic updates, the amount of time interval to choose. Right now, I have set up the project to show a map and used

googleMap.setMyLocationEnabled(true);
googleMap.setOnMyLocationChangeListener(myLocationChangeListener);

private GoogleMap.OnMyLocationChangeListener myLocationChangeListener = new GoogleMap.OnMyLocationChangeListener() {
    @Override
    public void onMyLocationChange(Location location) {
        LatLng loc = new LatLng(location.getLatitude(), location.getLongitude());
        if(googleMap != null){
            if(!firstTime){
                CameraPosition cameraPosition = new CameraPosition.Builder()
                .target(loc).zoom(zoom).build();
                googleMap.animateCamera(CameraUpdateFactory.newCameraPosition(cameraPosition)); 
                firstTime = true;
            }

            lat = location.getLatitude();
            lng = location.getLongitude();
            Toast.makeText(context, "Location is: " + String.valueOf(lat) + ", " 
                    + String.valueOf(lng), Toast.LENGTH_LONG).show();
        }
    }
};

to set lat/lng in variables.
My questions are:

  1. Is this a correct approach? Or can it be made better?
  2. The location change listener updates itself quite often. I'm seeing toasts being made in a few seconds interval. How to regulate that?
  3. How to stop the listener from fetching location when the activity's gone (paused/stopped)?
    Thank you!

Best Answer

For question 2 :

Use requestLocationUpdates(long minTime, float minDistance, Criteria criteria, PendingIntent intent)

First two parameters are :

Parameters minTime : minimum time interval between location updates, in milliseconds minDistance : minimum distance between location updates, in meters

           private static final long POLLING_FREQ = 1000 * 10;
           private static final float MIN_DISTANCE = 10.0f;

           // Reference to the LocationManager and LocationListener
           private LocationManager mLocationManager;
           private LocationListener mLocationListener;

           // Register for network location updates
            if (null != mLocationManager
                    .getProvider(LocationManager.NETWORK_PROVIDER)) {
                mLocationManager.requestLocationUpdates(
                        LocationManager.NETWORK_PROVIDER, POLLING_FREQ,
                        MIN_DISTANCE, mLocationListener);
            }

            // Register for GPS location updates
            if (null != mLocationManager
                    .getProvider(LocationManager.GPS_PROVIDER)) {
                mLocationManager.requestLocationUpdates(
                        LocationManager.GPS_PROVIDER, POLLING_FREQ,
                        MIN_DISTANCE, mLocationListener);
            }

You can adjust them to your own needs.

For question 3 :

Use : removeUpdates(PendingIntent intent) Removes all location updates for the specified pending intent.

As of question 1.

Your approach seems reasonable. If you need constant location updates if not then I can suggest other things to reduce battery consumption.

     mLocationListener = new LocationListener() {

    // Called back when location changes

    public void onLocationChanged(Location location) {


        // Get your location here
        // estimate

    }

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

    public void onProviderEnabled(String provider) {
        // NA
    }

    public void onProviderDisabled(String provider) {
        // NA
    }
};