M m M m - 1 year ago 126
Android Question

How to get object id from google map marker?

I am using this code to draw markers on google map (hide the ones that are invisible on screen)

for (MyMapPointModel item : items) {
// If the item is within the the bounds of the screen
if (bounds.contains(item.getLatLng())) {
// If the item isn't already being displayed
if (!visibleMarkers.containsKey(item.getId())) {
// Add the Marker to the Map and keep track of it with
// the HashMap
// getMarkerForItem just returns a MarkerOptions object
customMarker = getMap().addMarker(getMarkerForItem(item));
visibleMarkers.put(item.getId(), customMarker);
drawMarker(item.getLatLng(), item.getThumbUri(), item.getId());
} else { // If the marker is off screen
// If the course was previously on screen
if (visibleMarkers.containsKey(item.getId())) {
// 1. Remove the Marker from the GoogleMap
// 2. Remove the reference to the Marker from the
// HashMap


and i am storing markers with items id in hashmap
I want to call an activity with details for taped marker and i can't get item id from onMarkerClick listener (he provides only marker object). Am i missing something, and if i am what? Does anyone have a better idea?

Answer Source

Try the below

 mMap.setOnMarkerClickListener(new OnMarkerClickListener()

                    public boolean onMarkerClick(Marker arg0) {
                        arg0.getId() //get id
                        //use switch case
                        // or use title
                        // do something
                        return true;

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download