Skip to content Skip to sidebar Skip to footer

Google Maps Api V2 Find Marker By Id

You can simply add Markers with the new V2 Google maps API on Android. Because the Markers are recreated on configuration change or on save instance, you have to reference them via

Solution 1:

The documentation is wrong about recreating markers on configuration change and it's actually good for us it is wrong there.

Solution 2:

I have also came across similar situation in doing map clustering, where i need to remove the marker when it is added to an cluster.

The solution which i used is that, i am holding the reference to the markers when they are being created and added into the map and store the marker in a Map (String - Marker) ,where key(String) would be an auto-generated marker id, and value would be the marker object.

Now,you can get the reference to the marker object by its id and call remove() on that marker.

I hope this will be helpful to you.

Solution 3:

SImply try the following:

private Marker myMarker;

myMarker = getMap().addMarker(newMarkerOptions()
                    .position(latLng)
                    .title("My Spot")
                    .snippet("This is my spot!"));

now the marker you want to remove you can call the

myMarker.remove();

Post a Comment for "Google Maps Api V2 Find Marker By Id"