点击监听器信息窗口谷歌地图V2监听器、窗口、地图、信息

2023-09-07 17:47:06 作者:琵琶半掩的美人脸°

我有一个简单的地方类:

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

 
精彩推荐
图片推荐