单击样式地图图标和标签上的事件侦听器 [英] click event listener on styled map icons and labels
问题描述
是否可以为样式化地图显示的图标和/或标签上的单击事件添加侦听器?我有一个允许输入兴趣点的表格.由于 poi 样式显示了许多潜在候选人的图标和/或标签,并且单击一个会打开一个包含地址信息等的信息窗口,如果用户单击一个,我想将此地址信息捕获到表单上的字段中.
Is it possible to add a listener for the click event on the icons and/or labels that appear for styled maps? I have a form that allows entry of Points of Interest. Since the poi styling shows icons and/or labels for many potential candidates, and clicking on one opens an infowindow with address info, etc, I would like to capture this address info to the fields on my form if the user clicks on one.
推荐答案
没有基于 API 的 POI 访问.
There is no API-based access to the POI's .
但是有一个可能的解决方法.
But there is a possible workaround.
当您单击 POI 时,将打开一个信息窗口.可以修改 InfoWindow 原型,以便无需引用 InfoWindow 实例即可访问 InfoWindow 的内容.
When you click on a POI an InfoWindow opens. It's possible to modify the InfoWindow-prototype to be able to access the content of the InfoWindow without having a reference to the InfoWindow-instance.
//store the original setContent-function
var fx = google.maps.InfoWindow.prototype.setContent;
//override the built-in setContent-method
google.maps.InfoWindow.prototype.setContent = function (content) {
//when argument is a node
if (content.querySelector) {
//search for the address
var addr = content.querySelector('.gm-basicinfo .gm-addr');
if (addr) {
alert(addr.textContent);
//leave the function here
//when you don't want the InfoWindow to appear
}
}
//run the original setContent-method
fx.apply(this, arguments);
};
演示:http://jsfiddle.net/doktormolle/Vkf43/
注意:信息窗口的选择器没有记录,它们将来可能会改变
Note: The selectors for the infowindow are not documented, they may change in the future
这篇关于单击样式地图图标和标签上的事件侦听器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!