2

Google finally added a callback for location changes in the Android API v2! However, I cannot intuitively get it to work, and Google does not have much documentation for it. Has anyone gotten it to work? What more do I need?

    public class ... extends SupportMapFragment implements GoogleMap.OnMyLocationChangeListener {
GoogleMap map;
LocationManager locationManager;
String provider;

        @Override
        public void onActivityCreated(android.os.Bundle savedInstanceState) {
            super.onActivityCreated(savedInstanceState);
            map = getMap();
                    if (map != null) {
                       Criteria criteria = new Criteria();
                       criteria.setAccuracy(Criteria.ACCURACY_FINE);
                           locationManager =(LocationManager)getActivity().getSystemService(Context.LOCATION_SERVICE);
                provider = locationManager.getBestProvider(criteria, false);
            }
        }

        @Override
        public void onResume() {
            super.onResume();
            while(map == null) {
                map = getMap();
                map.setMyLocationEnabled(true);
                map.setOnMyLocationChangeListener(this);
            }
        }
        @Override
        public void onMyLocationChange(Location loc) {
            //implementation
        }
}
brainmurphy1
  • 1,082
  • 1
  • 19
  • 24

1 Answers1

7

This is how I do to navigate to the center of the map when we get the first location-update.

my class header:

public class FragActivity extends SherlockFragmentActivity implements  OnMyLocationChangeListener

private GoogleMap mMap;

my mMap-setup:

    if (mMap == null) {
        // Try to obtain the map from the SupportMapFragment.
        mMap = customMapFragment.getMap();

        // Check if we were successful in obtaining the map.
        if (mMap != null)
            setUpMap();
    }

setUpMap-method:

private void setUpMap() {
    mMap.setMyLocationEnabled(true);
    mMap.setOnMyLocationChangeListener(this);
}

and my onlocationchange:

@Override
public void onMyLocationChange(Location lastKnownLocation) {
    CameraUpdate myLoc = CameraUpdateFactory.newCameraPosition(
            new CameraPosition.Builder().target(new LatLng(lastKnownLocation.getLatitude(),
                    lastKnownLocation.getLongitude())).zoom(6).build());
    mMap.moveCamera(myLoc);
    mMap.setOnMyLocationChangeListener(null);
}

Works like a charm

ullstrm
  • 9,812
  • 7
  • 52
  • 83
  • I did this, however, the first location change update never comes. I am doing this process from within the "customMapFragment". Could that be the issue? – brainmurphy1 Feb 27 '13 at 14:13
  • I used the solution that starts with "update". http://stackoverflow.com/questions/13742551/how-to-get-my-location-changed-event-with-google-maps-android-api-v2 – brainmurphy1 Feb 27 '13 at 15:43
  • You have to have google play services rev >=5 installed to use OnMyLocationChangeListener – stoefln Mar 07 '13 at 13:05
  • @brainmurphy1 :comment line mMap.setOnMyLocationChangeListener(null); – MR. Kumar Nov 19 '14 at 06:04
  • it worked but the map also got MyLocation button on top right corner. How can we remove it? Here is how: `googleMap.getUiSettings().setMyLocationButtonEnabled(false);` – Bugs Happen Oct 30 '15 at 10:28