How to link Google Maps Android API v2 Marker to an object

后端 未结 2 1675
一整个雨季
一整个雨季 2021-01-07 09:13

I\'m adding dynamically a non-fixed amount of markers in a map, which each of them are related to one instance of my POCO class.

I need to link them so when the user

2条回答
  •  小鲜肉
    小鲜肉 (楼主)
    2021-01-07 09:49

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

    private HashMap markerMap = new HashMap();
    

    ...

    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 a OnInfoWindowClickListener to find the object id of the tapped marker in your Map and do something with the corresponding data, like open a new activity with details.

提交回复
热议问题