【发布时间】:2018-03-12 22:37:29
【问题描述】:
我接管了一些代码,这些代码使用 Google Map 的 API 在带有信息窗口的地图上创建标记。这是创建标记和侦听器的 sn-p(注意,这只是代码的一部分):
if(markers.length > 0) {
// Display multiple markers on a map
var infoWindow = new google.maps.InfoWindow({
maxWidth:420
}), marker, i;
// Loop through our array of markers & place each one on the map
var pinIcon = new google.maps.MarkerImage(
"<?php echo SITE_URL;?>images/site-structure/map-marker1.png",
null, /* size is determined at runtime */
null, /* origin is 0,0 */
null, /* anchor is bottom center of the scaled image */
new google.maps.Size(20, 30));
for( i = 0; i < markers.length; i++ ) {
var position = new google.maps.LatLng(markers[i][1], markers[i][2]);
bounds.extend(position);
marker = new google.maps.Marker({
position: position,
map: map,
icon:pinIcon,
title: markers[i][0]
});
// Allow each marker to have an info window
google.maps.event.addListener(marker, 'mouseover', (function(marker, i) {
return function() {
infoWindow.setContent(infoWindowContent[i][0]);
infoWindow.open(map, marker);
}
})(marker, i));
google.maps.event.addListener(marker, 'click', (function(marker, i) {
return function() {
infoWindow.setContent(infoWindowContent[i][0]);
infoWindow.close(map, marker);
}
})(marker, i));
// Automatically center the map fitting all markers on the screen
map.fitBounds(bounds);
}
}
自定义标记添加得很好,但事件侦听器(鼠标悬停和单击)不起作用。如果需要,我可以提供更多信息。
【问题讨论】:
-
请提供一个minimal reproducible example 来证明您的问题。
-
这些代码不足以让我们复制您的问题。但是,您想要实现的目标已经在此处接受的答案中完成:stackoverflow.com/questions/3059044/…
标签: javascript php jquery google-maps