在样式化的地图图标和标签上单击事件监听器

use*_*359 4 google-maps-api-3 google-maps-styling

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

Dr.*_*lle 8

没有基于API的POI访问权限.

但是有一种可能的解决方法.

当您单击POI时,InfoWindow将打开.可以修改InfoWindow-prototype,以便能够访问InfoWindow的内容,而无需引用InfoWindow实例.

      //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);
      };
Run Code Online (Sandbox Code Playgroud)

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

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