Maps API v2 with different marker actions

前端 未结 4 576
我在风中等你
我在风中等你 2020-12-24 14:37

I\'m trying to port my application to the new Google Maps API v2, but i\'m having trouble when interacting with markers.

My context: I have a map showing buses and

4条回答
  •  情书的邮戳
    2020-12-24 15:11

    I have run into this problem as well. My solution was:

    private Map markerMap = new HashMap<>();
    private GoogleMap mMap;
    
    private void doMarkers(){
        MarkerOptions opt = new MarkerOptions();
        //Fill out opt from MyModel
        Marker marker = mMap.addMarker(opt);
        markerMap.put(marker, myModel);
    }
    

    and then in the OnMarkerClickListener callback, pull your model out of the HashMap using the clicked marker. There is also a method Marker.getId() which for some reason returns a string. I don't understand why you can't specify an int id when you make the marker, or why you can't access the marker object before you add it to the map.

    UPDATE: After almost 4 years Google have added a method Marker.setTag(Object tag) to associate arbitrary data with a marker.

提交回复
热议问题