我正在尝试关注this tutorial以在地图上点击事件时创建一个圈子。这里是地图的tab1.js
下初始化:Google地图未在点击事件中注册
function initMap() {
forecastmap = new google.maps.Map(document.getElementById('forecastmap'), {
center: {lat: 1.352083, lng: 103.81983600000001},
zoom: 11
});
forecastmap.addListener('click', function(e) {
createBuffer(e.latLng, forecastmap);
});
geocoder = new google.maps.Geocoder();
infowindow = new google.maps.InfoWindow();
}
然后我tab2.js
,我执行所有的逻辑里面添加标记:
function createBuffer(coord, forecastmap) {
console.log('come in');
var marker = new google.maps.Marker({
position: coord,
map: forecastmap
});
clusterData.push(marker);
marker = new google.maps.Circle({
center: coord,
map: forecastmap,
strokeColor: '#000',
strokeWeight: 2,
strokeOpacity: 0.5,
fillColor: '#f0f0f0',
fillOpacity: 0.5,
radius: 20 * 1000
});
clusterData.push(marker);
}
我插入虚拟消息,以检查是否点击事件已登记。但是,该信息甚至没有打印出来。我想知道哪部分代码错了。
我的HTML DIV:
<div class="box-body">
<div id="forecastmap" style="width:100%;height:350px" onclick="initMap();"></div>
</div>
谢谢!
你要找的'addEventListener'而不是'addListener'。 –
@ObsidianAge \t 我试着从这里搜索:developers.google.com/maps/documentation/javascript/events但我没有看到任何addEventListener。你介意给我提供语法吗?因为我将上述内容更改为addEventListener,它告诉我函数未定义 – hyperfkcb
'addEventListener'是[**原始JavaScript方法**](https://developer.mozilla.org/en-US/docs/Web/ API /事件目标/的addEventListener)。我自己并不使用Google Maps,但Google显然有一个'addDomListener':'google.maps.event.addDomListener':https://developers.google。COM /地图/文档/ JavaScript的/示例/事件domListener。 –