如何谷歌地图Android的API V2标记链接到一个对象 [英] How to link Google Maps Android API v2 Marker to an object

查看:93
本文介绍了如何谷歌地图Android的API V2标记链接到一个对象的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我动态地添加在地图中,其中每个都涉及到我的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.

这篇关于如何谷歌地图Android的API V2标记链接到一个对象的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆