Getting started with clicking on google map marker infoWindow

I need to start some action by clicking on infoWindow (I can do this with setOnInfoWindowClickListener()

), but this action may be different and I need something else. I should be able to set an ID for each map marker. Can I pass a custom ID to every marker on the map? Or how can I create a button in infoWindow

with this id (if I can do that, I can pass this id).

+3


source to share


1 answer


I suggest using HashMap or something similar. When you iterate over your list of objects and create markers for them, add the marker to the list using the object id as the key and the marker as the value:

private HashMap<Integer, Marker> markerMap = new HashMap<Integer, Marker>();

      

...



for(MarkerObject obj : this.markerObjects)
{
     //If the marker isn't already being displayed
     if(!markerMap.containsKey(obj.getId()))
     {
         //Add the Marker to the Map and keep track of it 
         this.markerMap.put(obj.getId(), this.mMap.addMarker(getMarkerForObject(obj)));
     }
}

      

Then you can use OnInfoWindowClickListener to find the object id of the marked marker in your map and do something with the relevant data, like opening a new action with details.

+7


source







All Articles