问题
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;
}
}
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
回答1:
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
回答2:
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;
}
回答3:
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
回答4:
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.
回答5:
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
}
});
回答6:
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
}
来源:https://stackoverflow.com/questions/29976663/click-listener-for-info-window-google-maps-v2