gingo
gingo

Reputation: 3169

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 its ids. The question is how to remove from map Marker with particular id?

My use case is to add Markers to the map, store its id's with mapping to real objects. Then user removes one of this real objects, so I find the Marker id and want to remove Marker from the map and the only way I know about is to have Marker object and call remove() on it.

Upvotes: 1

Views: 6427

Answers (3)

MaciejGórski
MaciejGórski

Reputation: 22232

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

Upvotes: 2

Sandeep Dhull
Sandeep Dhull

Reputation: 1648

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.

Upvotes: 1

Karan_Rana
Karan_Rana

Reputation: 2823

SImply try the following:

private Marker myMarker;

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

now the marker you want to remove you can call the

myMarker.remove();

Upvotes: 0

Related Questions