【问题标题】:Google Maps Listener Not Working谷歌地图监听器不工作
【发布时间】: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);
}

}

自定义标记添加得很好,但事件侦听器(鼠标悬停和单击)不起作用。如果需要,我可以提供更多信息。

【问题讨论】:

标签: javascript php jquery google-maps


【解决方案1】:

您是否尝试过简单地做类似的事情:

// Allow each marker to have an info window
google.maps.event.addListener(marker, 'mouseover', function(marker, i) {
  infoWindow.setContent(infoWindowContent[i][0]);
  infoWindow.open(map, marker);
});

除非在使用 Google 地图添加事件侦听器方面存在一些我不知道的变化,否则您应该能够传递一个直接执行您想要它执行的操作的函数,而不是传递一个返回另一个函数的函数.

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2023-04-08
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-05-10
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多