In reply to:
The question is: How do I identify what entity the clicked Marker 'marker' represents?
[...]
There's also a marker.getId(), but such ID is generated by the API and I can't control it
You can control it. The marker is returned by addMarker(), so you can get its ID and store it. Here's the code:
Map <String, Integer> markers = new HashMap<String, Integer>();
...
MarkerOptions mo = new MarkerOptions()
.icon(BitmapDescriptorFactory.fromResource(R.drawable.my_marker))
.position(new LatLng(mLat, mLon))
.flat(true)
.snippet("Click here for details")
.rotation(0)
.title(title);
When you add the marker to the map, store its ID on the container
MyClass myObject = new MyClass(lat, lon); // The class that you are rendering on the map with Markers, for example "Monument"
Marker mkr = map.addMarker(mo);
markers.put(mkr.getId(), myObject.getId());
Then when the marker is clicked, you can recover the id of "myObject" like this
map.setOnInfoWindowClickListener(new OnInfoWindowClickListener() {
public void onInfoWindowClick(Marker marker) {
int id = markers.get(marker.getId());
// Now id contains which Monument (or your preferred class) was clicked
}
});
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…