我正在尝试使用 google maps javascript api 在地图上制作一个可点击的标记。当我单击标记时,什么也没有发生。但是,当我将点击事件设置为地图而不是标记时,我可以在任何地方点击,控制台将记录“点击”,并在标记处弹出一个信息窗口。标记和地图都是在范围内的同一点声明的,所以我无法弄清楚为什么一个有效,另一个无效。这是我现在的代码:
window.onload = function() {
var marker = new google.maps.Marker({ animation: google.maps.Animation.DROP });
var infoWindow = new google.maps.InfoWindow();
var pos;
// The location of Uluru
var uluru = { lat: 43.221009, lng: -79.865291 };
// The map, centered at Uluru
var map = new google.maps.Map(
document.getElementById('map'), { zoom: 12, center: uluru });
// The marker, positioned at Uluru
// Try HTML5 geolocation.
if (navigator.geolocation) {
navigator.geolocation.getCurrentPosition(function (position) {
pos = {
lat: position.coords.latitude,
lng: position.coords.longitude
};
marker = new google.maps.Marker({ position: pos, map: map });
map.setCenter(pos);
}, function () {
handleLocationError(true, infoWindow, map.getCenter());
marker = new google.maps.Marker({ position: uluru, map: map });
});
}
else {
marker = new google.maps.Marker({ position: uluru, map: map });
// Browser doesn't support Geolocation
handleLocationError(false, infoWindow, map.getCenter());
}
marker.addListener('click', function (){
showInfoWindow();
});
function showInfoWindow() {
console.log("Click");
infoWindow.setPosition(pos);
infoWindow.setContent('Your Location');
infoWindow.open(map);
}
呼唤远方
相关分类