【问题标题】:GoogleMap: Multiple Markers showing same InfoWindow and not clickable on Mobile DevicesGoogleMap:多个标记显示相同的信息窗口并且在移动设备上不可点击
【发布时间】:2012-02-17 21:49:47
【问题描述】:

Google Map API 上的“Multiple Markers InfoWindows”问题。
有2个问题:

  • 标记显示SAME InfoWindow
  • 同样,标记不能在移动设备上点击

这是我的代码:

var map = new google.maps.Map(document.getElementById('map'), {
    zoom: 10,
    center: new google.maps.LatLng(60.037760, -44.100494),
    mapTypeId: google.maps.MapTypeId.ROADMAP
});

var locations = [
                    ['Alvin', 60.074433, -44.011917],
                    ['Sirius', 60.037760, -44.100494]
                ];

for (var i = 0; i < locations.length; i++) {
    var marker = new google.maps.Marker({
        position: new google.maps.LatLng(locations[i][1], locations[i][2]),
        map: map
    });

    var infowindow = new google.maps.InfoWindow({content: locations[i][0]});

    new google.maps.event.addListener( marker, 'click', function() {
        infowindow.open(map,this);
    });
}

【问题讨论】:

    标签: google-maps mobile geolocation google-maps-markers infowindow


    【解决方案1】:

    创建对信息窗口的全局引用。

    var infowindow = new google.maps.InfoWindow();
    for (var i = 0; i < locations.length; i++) {
        var marker = new google.maps.Marker({
            position: new google.maps.LatLng(locations[i][1], locations[i][2]),
            map: map
        });
    
        new google.maps.event.addListener( marker, 'click', function() {
             infowindow.setContent(locations[i][0]);
             infowindow.open(map,this);
        });
    }
    

    【讨论】:

    • 不工作 :( 更糟糕的结果是:即使在 PC 上也无法点击标记。在移动设备上也是如此。
    猜你喜欢
    • 2012-02-18
    • 2019-09-02
    • 2017-01-22
    • 2013-11-19
    • 2016-06-03
    • 2017-03-15
    • 2013-10-06
    • 1970-01-01
    • 2016-07-27
    相关资源
    最近更新 更多