且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

如何知道哪些标记被点击谷歌地图V2为Android?

更新时间:2023-01-07 19:55:40

在回答:

现在的问题是:我如何确定哪些实体点击标记标记重presents?   [...]   还有一个marker.getId(),但这样的ID是由API产生的,我无法控制它。

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

您可以控制​​它。该标志是由addMarker()返回,这样你就可以得到它的ID和存储。这里的code:

You can control it. The marker is returned by addMarker(), so you can get its ID and store it. Here's the code:

HashMap <String, Integer> mMarkers = 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);

在您的标记添加到地图,存储其ID在容器上

When you add the marker to the map, store its ID on the container

Marker mkr = map.addMarker(mo);
mMarkers.put(mkr.getId(), myObject.getId()); 

然后点击标记时,就可以恢复myObject的的id是这样

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 = mMarkers.get(marker.getId());
        // Do stuff with the id                         
    }
});