How to get My Location changed event with Google Maps android API v2?
In v1 you can do the next in order to handle location changed event:
public class MyLocationOverlay extends com.google.android.maps.MyLocationOverlay {
/**
* Listener
*/
public interface Listener {
void onLocationChanged(android.location.Location location);
}
private Listener listener;
public MyLocationOverlay(Context context, MapView mapView, Listener listener) {
super(context, mapView);
this.listener = listener;
}
@Override
public synchronized void onLocationChanged(Location location) {
super.onLocationChanged(location);
// fire listener
if (listener != null)
listener.onLocationChanged(location);
}
}
UPDATE: Google introduced the new
LocationClientand associatedLocationListener(theOnMyLocationChangeListenerinterface is now deprecated).You can do this by creating a custom
LocationSourcefor the my-location layer. Below is an example that automatically centers the camera on current location (“my location”), similar to the functionality that was offered byMyLocationOverlayin Google Maps Android API v1.So you can simply replace this line of code
with whatever functionality you need to be executed when the location changes.