更改Google地图中信息窗口的内容
根据选定的国家/地区和日期,结果列表将显示在地图上。带有 html 数据的标记的放置效果很好,我遇到的问题是处理带有重复标记的情况。
我找到了一种检查重复项的方法,但我希望能够修改已存在的信息窗口中的内容。这样我就可以将重复标记的内容添加到位于同一位置的现有标记中。
我遇到的问题是我找不到正确的方法来访问现有信息窗口中重复信息窗口中的数据。
如果我这样做alert(infoWindow.getContent());,那么我只会得到“未定义”。我也尝试过markers[i].getContent(),但这也没有做任何事情。
关于如何访问先前添加的标记的信息窗口、使用 getContent() 获取其内容并将重复标记的数据添加到其中的任何想法或建议?
var marker = new google.maps.Marker({
map: map,
position: latlng
});
for (var i=0; i<markers.length; i++) {
if (markers[i].getPosition().equals(marker.getPosition())) {
alert('duplicate found');
alert(infoWindow.getContent());
} else {
google.maps.event.addListener(marker, 'click', function() {
infoWindow.setContent(html);
infoWindow.open(map, marker);
});
}
}
Based on a selected country and date a list of results will show up on the map. The placements of markers with html data in them works fine, the problem I have is handling the cases with duplicate markers.
I found a way to check for duplicate ones, but I would like to be able to modify the content in the infowindow of the one already present. So that I can add the content of the duplicate marker to the existing marker that stands in the same location.
The problem I'm having is that I can't find the right way to access the data in the existing infowindow for the duplicate one.
If I do alert(infoWindow.getContent());, then I just get "undefined". I also tried markers[i].getContent(), but that also didn't do anything.
Any idea's, or suggestion on how I can access the infowindows of previously added markers, get their content with getContent(), and add the data of the duplicate one to it?
var marker = new google.maps.Marker({
map: map,
position: latlng
});
for (var i=0; i<markers.length; i++) {
if (markers[i].getPosition().equals(marker.getPosition())) {
alert('duplicate found');
alert(infoWindow.getContent());
} else {
google.maps.event.addListener(marker, 'click', function() {
infoWindow.setContent(html);
infoWindow.open(map, marker);
});
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我很确定你不能完全做你想做的事。
据我所知,并且可以在 API 中看到,信息窗口或标记都不知道彼此的任何信息。唯一知道单击某个标记时应显示哪个信息窗口的是谷歌地图事件处理程序。
解决您的问题的方法是将所有信息窗口存储在一个数组中,并将标记存储在另一个数组中,您将确保相应标记和信息窗口的索引相同。
另一种方法是定义您自己的对象,确保它可以同时容纳标记和信息窗口。
无论如何,您都必须循环遍历标记数组以查找重复标记,然后使用我描述的两种方案之一获取相应的信息窗口。这似乎有点愚蠢,但我不知道还有其他方法可以做到这一点。
I am pretty sure you can't do exactly what you want.
As far as I am aware, and can see in the API, neither the infowindow or the marker knows anything about eachother. The only thing that knows which infowindow should be shown when clicking a certain marker is the google maps eventhandler.
A solution to your problem would then be to store all the infowindows in an array, and the markers in another array where you would make sure that the index of the corresponding markers and infowindows where the same.
Another way to do it would be to define your own object where you make sure it can hold both a marker and a infowindow.
In any way, you would have to loop through the markers array to find the duplicate marker and then get the corresponding infowindow by using one of the two scenarios I have described. It seems a bit silly, but I am not aware of another way to do it.