[英]Android Google maps adding multiple markers, cannot bind onInfoWindowClick method
I am currently trying to add multiple markers to my Android application. 我目前正在尝试向我的Android应用程序添加多个标记。 This works just perfectly.
这非常完美。 The only thing I am getting stuck at is the fact that I cannot bind multiple "onInfoWindowClick" on multiple markers.
我唯一遇到的困扰是我无法在多个标记上绑定多个“ onInfoWindowClick”。
For instance, if I have like: 例如,如果我有:
for (int i = 0; i < randomList; i++) {
MarkerOptions marker = new MarkerOptions().position(latlng).title(MainActivity.list.get(i).aMessage);
// adding marker
googleMap.addMarker(marker);
googleMap.setOnInfoWindowClickListener(new GoogleMap.OnInfoWindowClickListener() {
@Override
public void onInfoWindowClick(Marker marker) {
// Do something onclick
}
});
}
This would result in a infowindowclick that works, for each marker, but I always get the same data back inside that "// Do something onclick", this is because the last marker is getting set to this event. 这将导致对每个标记都起作用的infowindowclick,但是我总是在“ // Do on onclick”中获得相同的数据,这是因为最后一个标记已设置为该事件。
What is my procedure to attach this event to multiple markers? 如何将此事件附加到多个标记的程序是什么?
Create a new Marker
on every loop and execute the method: showInfoWindow()
; 在每个循环上创建一个新的
Marker
并执行以下方法: showInfoWindow()
;
for (int i = 0; i < randomList; i++) {
// MarkerOptions marker = new MarkerOptions().position(latlng).title(MainActivity.list.get(i).aMessage);
// adding marker
// googleMap.addMarker(marker);
googleMap.addMarker(new MarkerOptions().position(latlng).title(MainActivity.list.get(i).aMessage)).showInfoWindow();
googleMap.setOnInfoWindowClickListener(new GoogleMap.OnInfoWindowClickListener() {
@Override
public void onInfoWindowClick(Marker marker) {
// Do something onclick
}
});
}
by the way you are setting the same latlng
value! 顺便说一下,您要设置相同的
latlng
值!
You should not be calling setOnInfoWindowClickListener()
inside a loop, this just needs to be called once, and it will apply to all Markers. 您不应该在循环内调用
setOnInfoWindowClickListener()
,只需调用一次即可,它将应用于所有Marker。
The Marker object that gets passed into onInfoWindowClick(Marker marker)
will always be the Marker that was just clicked. 传递给
onInfoWindowClick(Marker marker)
的Marker对象将始终是刚刚单击的Marker。
So, take that out of the loop. 因此,将其带出循环。 Next, in order to figure out what Marker was just clicked, you could get the title of the Marker, and loop through your list until you find the list item that has a
aMessage
value that corresponds to the Marker's title. 接下来,为了找出刚刚单击的标记,您可以获取标记的标题,并循环浏览列表,直到找到具有与标记标题相对应的
aMessage
值的列表项。
Note you could also identify that current marker by location by calling marker.getPosition();
注意,您还可以通过调用
marker.getPosition();
按位置识别当前标记marker.getPosition();
. 。
googleMap.setOnInfoWindowClickListener(new GoogleMap.OnInfoWindowClickListener() {
@Override
public void onInfoWindowClick(Marker marker) {
//get location of Marker that was just clicked
LatLng latLon = marker.getPosition();
// get the title of Marker that was just clicked
String title = marker.getTitle();
//find item in the list that corresponds to currently clicked Marker
for (int i = 0; i < MainActivity.this.list.size(); i++){
if (title.equals(MainActivity.this.list.get(i).aMessage)){
//found current list item corresponding to
//the Marker that was just clicked!
}
}
}
});
for (int i = 0; i < randomList; i++) {
MarkerOptions marker = new MarkerOptions().position(latlng).title(MainActivity.this.list.get(i).aMessage);
// adding marker
googleMap.addMarker(marker);
}
I answered a similar question here , you may find that helpful as well. 我在这里回答了类似的问题,您可能也会发现它也很有帮助。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.