Android Google Maps v2 - Add object to marker

前端 未结 4 1190
说谎
说谎 2020-12-31 00:02

How can we add an object to a marker in the new Google Maps Android API v2? So if we click on the InfoWindow, we can do something with the obje

4条回答
  •  有刺的猬
    2020-12-31 00:12

    As of Play Services v9.4.0, you can now set any object directly upon a marker!

    To save the data:

    Marker marker = getMap().addMarker(new MarkerOptions()
            .position(lat, lng)
            .title(spot.getName());
    
    CustomObject myData = new CustomObject();
    marker.setTag(myData);
    

    Then to retreive your data:

    CustomObject myRestoredData = (CustomObject)marker.getTag(myData);
    

    For more infomration on marker data, here are the docs.

提交回复
热议问题