如何谷歌地图Android的API V2标记链接到一个对象标记、对象、链接、地图

2023-09-06 14:17:52 作者:人心可畏

我动态地添加在地图中,其中每个都涉及到我的POCO类的一个实例的标记的非固定量

I'm adding dynamically a non-fixed amount of markers in a map, which each of them are related to one instance of my POCO class.

我需要把它们连接起来,所以当用户点击标记之一,我显示数据的自定义信息窗口内的其余部分

I need to link them so when the user clicks on one of the markers, I show the rest of the data inside the custom InfoWindow.

你有什么建议怎么样?

PS:我每次增加新的标记,用户平移或缩放地图,我担心超载的应用程序。是不可见的标记处理?

PS: I add new markers everytime the user pans or zooms the map and I worried about overloading the app. Are the non visible markers disposed?

推荐答案

我建议使用一个HashMap或类似的东西。如你迭代的对象的列表,并创建标记它们,也标记添加到列表中,使用对象作为密钥的ID,并且将标记物作为值:

I suggest using a HashMap or something similar. As you iterate over your list of objects and create markers for them, also add the Marker to a list, using the ID of the object as the key, and the marker as the value:

private HashMap<Integer, Marker> markerMap = new HashMap<Integer, Marker>();

...

for(MarkerObject obj : this.markerObjects)
{
     //If the marker isn't already being displayed
     if(!markerMap.containsKey(obj.getId()))
     {
         //Add the Marker to the Map and keep track of it 
         this.markerMap.put(obj.getId(), this.mMap.addMarker(getMarkerForObject(obj)));
     }
}

然后你可以使用一个OnInfoWindowClickListener找到地图中的窃听标记的对象ID和做一些与相应的数据,如细节打开一个新的活动。

Then you can use a OnInfoWindowClickListener to find the object id of the tapped marker in your Map and do something with the corresponding data, like open a new activity with details.

 
精彩推荐
图片推荐