简体   繁体   中英

how to set id for an google map marker to identify which one is selected?

如何从onMarkerClicked(标记标记)回调中查找谷歌地图中标记列表中选择的标记。

The recommended way to do this is to have a Hash with marker ID and your custom data. The Marker object might change if the activity is killed and restored but the ID will remain the same. You map would look like:

HashMap<String, MyObject> markersAndObjects = new HashMap<String, MyObject>();

Marker objects have a getId() method to get the ID. Hope it helps.

You can do this via marker.setTag(MyMarkerTagObject) and marker.getTag(MyMarkerTagObject) .

You'll just need to create a plain MyMarkerTagObject class that contains your id field (along with any other fields you would like to add) with setter/getter methods for those fields. For example:

MyMarkerTagObject myMarkerTagObject = new MyMarkerTagObject();
myMarkerTagObject.setId(someId);
marker.setTag(myMarkerTagObject);

And on the other side of things:

MyMarkerTagObject myMarkerTagObject = (MyMarkerTagObject)marker.getTag();
long sameId = myMarkerTagObject.getId();

You can identify by id or title.

@Override
public boolean onMarkerClick(Marker marker) {
      marker.getId()
      marker.getTitle();
});

Or you can extend Marker and in constructor pass every data which you want, and in onMarkerClick cast to your own Marker and get data.

You can get lat, long, marker id, etc of a selected marker from onMarkerClick(Marker marker) like :

@Override
public boolean onMarkerClick(Marker marker) {
    Log.i("GoogleMapActivity", "onMarkerClick");
    LatLng position = marker.getPosition();
    String idSelected= marker.getId();
/*  Toast.makeText(getApplicationContext(),
            "Marker Clicked: " + marker.getTitle(), Toast.LENGTH_LONG)
            .show();*/

    return false;
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM