【问题标题】:InfoWindow shows same content for different markers with markerclusterersInfoWindow 使用 markerclusterers 显示不同标记的相同内容
【发布时间】:2014-02-08 05:20:36
【问题描述】:

我在下面的代码中找不到错误,但它为所有信息窗口显示了相同的内容。

$('#map').gmap({'zoom': 2, 'disableDefaultUI':true}).bind('init', function(evt, map) { 
    var bounds = map.getBounds();
    var southWest = bounds.getSouthWest();
    var northEast = bounds.getNorthEast();
    var lngSpan = northEast.lng() - southWest.lng();
    var latSpan = northEast.lat() - southWest.lat();
    var image = 'http://localhost/images/dot.png';
    for ( var i = 0; i < len; i++ ) {

        boxText.innerHTML = '<div id="content" style="margin-left:2px; font-weight:bold; margin-top:1px;overflow:hidden;">'+
                        '<div id="bodyContent" style="font-weight:bold;">'+
                        '<div style="font:10px verdana;color:#375A9E; margin-left:2px;font-weight:bold;">' +
                        '<a href="' + locations[i][5] + '" target="_blank">' + locations[i][0] + '</a><div>' +
                        '</div>';
                boxText1.innerHTML ='<div id="content" class="map_rightclick" style="margin-left:10px; font-weight:bold; margin-top:1px;overflow:hidden;">'+
                        '<div id="bodyContent" style="font-weight:bold;">'+
                        '<div style="font:10px verdana;color:red; margin-left:2px;">' +
                        '<a href="' + locations[i][5] + '" target="_blank" style="font-weight:bold;">' + locations[i][0] + '</a><div>' +
                        '<br><b>City:</b><span style="color:black;">' + locations[i][3] +'</span>'+
                   '</div>';                       
        $('#map').gmap('addMarker', { 
            'position': new google.maps.LatLng(locations[i][1], locations[i][2]),
            'icon': image
        }).click(function() {
            $('#map').gmap('openInfoWindow', { content : boxText }, this);
        }).rightclick(function(){
          $('#map').gmap('openInfoWindow', { content : boxText1 }, this);
                  }).mouseover(function(){
          $('#map').gmap('openInfoWindow', { content : boxText }, this);
          }).mouseout(function(){
          $('#map').gmap('closeInfoWindow',this);
          });
    }
    $('#map_canvas').gmap('set', 'MarkerClusterer', new MarkerClusterer(map, $(this).gmap('get', 'markers')));
    // To call methods in MarkerClusterer simply call 
    // $('#map_canvas').gmap('get', 'MarkerClusterer').callingSomeMethod();
});

【问题讨论】:

    标签: javascript jquery google-maps google-maps-markers markerclusterer


    【解决方案1】:

    看起来您正在将点击处理程序和信息窗口内容附加到地图本身,而不是标记(并且您正在附加它len 次!)。

        $('#map').gmap('addMarker', { 
            'position': new google.maps.LatLng(locations[i][1], locations[i][2]),
            'icon': image
        }).click(function() {
            $('#map').gmap('openInfoWindow', { content : boxText }, this);
        })
    

    信息窗口内容将仅包含 for 循环中最后一项的内容。

    您希望将信息窗口的内容与标记相关联。您需要在“addMarker”的回调中执行此操作。

    看起来您正在使用 jquery.ui 的地图插件。看看这个例子:http://code.google.com/p/jquery-ui-map/wiki/Examples#Example_addInfoWindow

    【讨论】:

    • 这似乎可行,让我实施并看看。谢谢你的回答。
    • 你拯救了这一天!我也明白我做错了什么:D
    猜你喜欢
    • 2011-06-21
    • 2023-03-21
    • 2016-02-20
    • 2021-06-27
    • 1970-01-01
    • 2020-02-03
    • 2011-05-16
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多