M m M m - 1 month ago 12
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
visibleMarkers.get(item.getId()).remove();
// 2. Remove the reference to the Marker from the
// HashMap
visibleMarkers.remove(item.getId());

}
}
}


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

Try the below

 mMap.setOnMarkerClickListener(new OnMarkerClickListener()
                {

                    @Override
                    public boolean onMarkerClick(Marker arg0) {
                        arg0.getId() //get id
                        //use switch case
                        // or use title
                        if(arg0.getTitle().equals("title"))   
                        // do something
                        return true;
                    }

                });