点击地图 POI 时,Google Map Api 要素层点击事件不会被触发

问题描述 投票:0回答:1

我的代码如下

map = new google.maps.Map(document.getElementById('map'), {
            mapTypeId: window.google.maps.MapTypeId.ROADMAP,
            disableDefaultUI: true,
            fullscreenControl: false,
            streetViewControl: false,
            clickableIcons:false,
            mapId:<My Map Id>
});
featureLayer.addListener('click', function(event) {
  /* when clickableIcons is false,
       1. on click of feature layer it will fire this event and map click event will not fire. 
       2. "on click of POI elements that are on feature layer, **this event will not fire but map click event will fires**" 

     when clickableIcons is true,
       1. on click of feature layer it will fire this event and map click event will not fire.
       2. "on click of POI elements that are on feature, **first this event will fire than map click event will fire**"
*/
});
map.addListener('click', function(event) {
  /* map click event*/
});

现在的问题是,单击地图地标/POI 元素时,它默认会打开我不想要的弹出窗口, 所以我将 clickableIcons 设置为 false 这解决了弹出窗口的问题。

但是,现在当我单击 featureLayer 上的 POI 元素时,我无法触发 featureLayer 单击事件,它只会触发我不需要的地图单击事件。

我想知道是否有任何方法可以通过禁用单击地标/POI 时出现的弹出窗口来触发 featureLayer 事件。

我尝试的是, 我设置了 clickableIcons:false 因为现在如果我单击 POI 元素,它不会触发 featureLayer 单击事件, 我尝试检查是否可以从地图点击事件中获取 placeId,但没有成功。

javascript google-maps google-maps-api-3 event-handling javascript-events
1个回答
0
投票

一种方法是将事件侦听器直接附加到地图容器,然后确定单击事件的坐标。然后,您可以执行自定义逻辑来确定点击是否落在地标/POI 附近,并相应地处理事件。

© www.soinside.com 2019 - 2024. All rights reserved.