1

Im creating an app where you can toggle the FollowLocation on and off using MyLocationOverlay.

My problem is, when i've already set the MyLocationOverlay earlier, and want to turn on the FollowLocation, but i cant seem to find/get the MyLocationOverlay object in the MapView's OverlayManager. The OverlayManager seems to only return an Overlay object, not the MyLocationOverlay object.

This is how i add the MyLocationOverlay:

main_mapView = (MapView) findViewById(R.id.main_mapView);   
MyLocationOverlay mMyLocationOverlay = new MyLocationOverlay(mContext, main_mapView);
mMyLocationOverlay.enableMyLocation();
mMyLocationOverlay.enableCompass();
mMyLocationOverlay.main_mapView.getOverlays().add(mMyLocationOverlay);
main_mapView.invalidate();

Later i would like to find the MyLocationOverlay doing something like this, but as mentioned it returns an Overlay object.

MyLocationOverlay mMyLocationOverlay = main_mapView.getOverlayManager().get(i);
mMyLocationOverlay.enableFollowLocation()

Anyone?

2 Answers 2

2

Just keep a reference to the MyLocationOverlay you new initaly... No need to get it back from the overlay manager.

1

Alternatively, check the getClass() method of the returned Overlay object. It may show you that the requested Overlay is in fact a MyLocationOverlay, in which case you can cast it. For example:

Overlay overlay = main_mapView.getOverlayManager().get(i);

if ( overlay.getClass().equals(MyLocationOverlay.class) ) {
    ((MyLocationOverlay)mMyLocationOverlay).enableFollowLocation()
}
0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.