Click Listener for Info Window Google Maps V2

本秂侑毒 提交于 2019-11-30 20:54:19

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....
               }

            }
        }
    });

Documentation:

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

I have a method to get the index of the marker and you can use it to get the element in Places.

map.setOnInfoWindowClickListener(new OnInfoWindowClickListener() {
        @Override
        public void onInfoWindowClick(Marker marker) {
            Log.i("MAP", "the index of this element is: " + String.valueOf(getMarkerIndex(marker.getId())));
        }
    });

getMarkerIndex() method:

private int getMarkerIndex(String index){
    int myIndex = -1;
    try{
        myIndex = Integer.parseInt(index.replace("m", ""));
    }catch(NumberFormatException nfe){
        Log.e(TAG, nfe.getMessage());
    }
    return myIndex;
}

i use the key of an object to find it for example i have two places:

place1 : name= "test" , address ="test2" , id= 0
place2 : name= "test" , address ="test2" , id= 1

in the marker they appear the same if i use this :

 map.addMarker(new MarkerOptions().position(latlng)
                            .title(name).snippet(address);

so to distinguish between them i modified the code to be :

  map.addMarker(new MarkerOptions().position(latlng)
                            .title(id+"-"name).snippet(address);

and in the infoWindowClickListener i just access the id by

  String  id=marker.getTitle().substring(0,1);//if your places are < 10

2nd way

is ceating your custom infoWindow and create your object "place" http://androidfreakers.blogspot.com/2013/08/display-custom-info-window-with.html

edit your loop this way

for(Place place : places) {
    Marker marker = map.addMarker(new MarkerOptions().position(place.latlng).title(place.name);
    place.markerId = marker.getId();
}

Of course you need add variable markerid to place

getId();

Gets this marker's id. The id will be unique amongst all Markers on a map.

then in onInfoWindowClickListener you can find place by markerId.

I suggest using a HashMap.

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

In your loop:

for(Place place : places){
   MarkerOption marker =new MarkerOptions().position(place.latlng).title(place.name);
   map.addMarker(marker);
   markerMap.put(marker, place)
}

In your Listener:

map.setOnInfoWindowClickListener(new OnInfoWindowClickListener() {
        @Override
        public void onInfoWindowClick(Marker marker) {
           Place place = markerMap.get(marker);// here you get your exact Place Object
        }
    });

try this

{
...   
 map.setOnMarkerClickListener(this);
 For(Place place : places)
     map.addMarker(new MarkerOptions().position(place.latlng).title(place.name);
...
}

@Override
    public boolean onMarkerClick(final Marker marker) {
            //handle click here
}
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!