如何知道哪些标记被点击谷歌地图V2为Android? [英] How to know which Marker was clicked on Google Maps v2 for Android?

查看:127
本文介绍了如何知道哪些标记被点击谷歌地图V2为Android?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在与谷歌地图上,针对Android的应用程序。我有很多标记我的屏幕上,我preparing定制的气球每个标记他们被点击时。这意味着,我必须根据该被点击的标记是不同的信息。

I am making an application with a Google Maps on it, for Android. I have plenty of Markers on my screen and I am preparing customizable balloon for each marker when they are clicked. This means, I have information which is different according to the marker which was clicked.

我设置了​​setInfoWindowAdapter标记观的内容,然后我重写方法getInfoContents。

I set up the contents of the View of the marker with setInfoWindowAdapter and then I override the method getInfoContents.

的问题是:该方法的信息窗口中的内容的一般的实现,但每个标记须显示它自己的信息。所以,据我了解,我必须以某种方式检测上getInfoContents(标记标记)的标记已被点击,以便从我的数据结构加载到present必要信息的信息窗口。现在的问题是:我如何确定哪些实体点击标记'标记'重新presents 我的意思是,刚刚在其上触发,显示信息窗口getInfoContents对象标记,怎么可以?我发现这是正确的信息来显示?不过,我觉得有关使用marker.getTitle()比较字符串标题,但是这使我不得不显示的信息窗口,我不希望在一个标题。还有一个marker.getId(),但这样的ID是由API产生的,我无法控制它。

The problem is: This method is the general implementation of the contents of the Info Window, but each marker shall show its own information. So, as far as I understand, I have to somehow detect on getInfoContents(Marker marker) which of the markers have been clicked, in order to load from my data structures the necessary info to present on the info window. The question is: How do I identify what entity the clicked Marker 'marker' represents? I mean, having just the object Marker on getInfoContents which was triggered to show the info window, how can I detect which is the proper information to display? I though about comparing the string Title by using marker.getTitle(), but this obliges me to display a Title on the info window, which I do not want. There's also a marker.getId(), but such ID is generated by the API and I can't control it

任何想法?

推荐答案

在回答:

现在的问题是:我如何确定哪些实体点击标记标记重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                         
    }
});

这篇关于如何知道哪些标记被点击谷歌地图V2为Android?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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