点击监听器信息窗口谷歌地图V2 [英] Click Listener for Info Window Google Maps V2

查看:144
本文介绍了点击监听器信息窗口谷歌地图V2的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个简单的地方类:

I have a simple "Place" class:

public class Plac{
    String name;
    int id;
    LatLng latlng;

    public Product(String name, int id, LatLng latlng) {
        this.name = name;
        this.id= id;
        this.latlng = latlng;
    }
}

和我添加邻居这样一个ArrayList:(注意的名称是不可以唯一的)

and I'm adding "Places" to an ArrayList like this: (Note the names are not unique)

ArrayList<Place> places = new ArrayList<Place>();
places.add(new Place("McDonald's", 1));
places.add(new Place("McDonald's", 2));
places.add(new Place("McDonald's", 3));

我添加标记,以我的谷歌地图这样的:

I'm adding markers to my Google Map like this:

for(Place place : places)
map.addMarker(new MarkerOptions().position(place.latlng).title(place.name);

我想知道如何添加一个侦听器正在被添加到地图中的每个标记。我试图用

I want to know how to add a listener for each marker that is being added to the map. I've tried to use

map.setOnInfoWindowClickListener(new OnInfoWindowClickListener() {
        @Override
        public void onInfoWindowClick(Marker marker) {

        }
    });

这工作,但我唯一可以用这个做的就是这是不是唯一的,所以我真的不能找到它的确切的地方对象被点击了标记的标题。有任何想法吗?谢谢

which works, however, the only thing I can do with this is get the title of the marker which is not unique so I can't really find which EXACT "Place" object is being clicked on. Any ideas? Thanks

推荐答案

您可以只使用了经纬度对象从调用为getPosition()返回标记来唯一地标识每个标记,并找到匹配的地方阵列。

You could just use the LatLng object returned from calling getPosition() on the Marker to uniquely identify each Marker, and find the match in your places array.

map.setOnInfoWindowClickListener(new OnInfoWindowClickListener() {
        @Override
        public void onInfoWindowClick(Marker marker) {

            LatLng latLon = marker.getPosition();

            //Cycle through places array
            for(Place place : places){
               if (latLon.equals(place.latlng)){
                    //match found!  Do something....
               }

            }
        }
    });

文件:

https://developer.android.com/reference/com/google/android/gms/maps/model/Marker.html

http://developer.android.com/reference/com/google/android/gms/maps/model/MarkerOptions.html

这篇关于点击监听器信息窗口谷歌地图V2的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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