How to know which Marker was clicked on Google Maps v2 for Android?

前端 未结 5 2139
挽巷
挽巷 2020-12-09 20:15

I am making an application with a Google Maps on it, for Android. I have plenty of Markers on my screen and I am preparing customizable balloon for each marker when they are

5条回答
  •  一整个雨季
    2020-12-09 20:29

    There is a better option, and this is what google suggests:

    Tag : An Object associated with the marker. For example, the Object can contain data about what the marker represents. This is easier than storing a separate Map. As another example, you can associate a String ID corresponding to the ID from a data set. Google Maps Android API neither reads nor writes this property.

    Source

    So you can do something like this:

    for (ListItem it: mList) {
        Marker mk = mMap.addMarker(new MarkerOptions().position(it.getLatLng()).title(it.getName()).snippet(it.getAddress()));
        mk.setTag(it.getID());
    }
    
    mMap.setOnMarkerClickListener(new GoogleMap.OnMarkerClickListener() {
        @Override
        public boolean onMarkerClick(Marker marker) {
            Integer id = (Integer) marker.getTag();
            return false;
        }
    });
    

提交回复
热议问题