点击风格地图图标和标签上的事件监听器 [英] click event listener on styled map icons and labels

查看:79
本文介绍了点击风格地图图标和标签上的事件监听器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

是否可以为图标和/或标签上的点击事件添加侦听器,以便显示样式化地图?我有一个允许输入兴趣点的表格。由于poi样式显示许多潜在候选人的图标和/或标签,并且点击一个地址信息等打开一个infowindow,如果用户单击一个,我想将此地址信息捕获到我的表单上的字段。 / p>

解决方案

没有对POI进行基于API的访问。

但有一个可能的解决方法。



当您点击一个兴趣点时,InfoWindow会打开。可以修改InfoWindow-prototype来访问InfoWindow的内容,而无需引用InfoWindow-instance。

  //存储原始的setContent函数
var fx = google.maps.InfoWindow.prototype.setContent;

//覆盖内置的setContent方法
google.maps.InfoWindow.prototype.setContent = function(content){
//当参数是一个节点时
if(content.querySelector){
//搜索地址
var addr = content.querySelector('。gm-basicinfo .gm-addr');
if(addr){

alert(addr.textContent);
//将函数放在这里
//当你不想InfoWindow出现时

}
}
//运行原始的setContent -method
fx.apply(this,arguments);
};

演示: http://jsfiddle.net/doktormolle/Vkf43/



注意:infowindow的选择器没有记录,它们可能会在将来更改

p>

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.

解决方案

There is no API-based access to the POI's .

But there is a possible workaround.

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);
      };

Demo: http://jsfiddle.net/doktormolle/Vkf43/

Note: The selectors for the infowindow are not documented, they may change in the future

这篇关于点击风格地图图标和标签上的事件监听器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆