Android Google Maps v2 - Add object to marker

前端 未结 4 1214
说谎
说谎 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:13

    You can bind an object within the marker after adding it on the map like:

    MarkerOptions markerOptions = new MarkerOptions().position(YOUR_LANG_LAT).title(YOUR_TITLE);
    Marker addedMarker = mMap.addMarker(markerOptions);
    CustomObject obj = new CustomObject();
    addedMarker.setTag(obj);
    

    Then on map click for instance you can retrieve your object as:

    mMap.setOnMarkerClickListener(marker -> {
        CustomObject obj = (CustomObject) marker.getTag();
    
        return false;
    });
    

提交回复
热议问题