Google将多个标记从外部链接打开

时间:2014-05-20 05:25:36

标签: google-maps google-maps-markers marker

创建了Google标记地图,并在地图旁边列出了地点名称 - 工作正常

Demo

:定位 - 当我点击列出的每个地点的名字时,如何打开相应的 infowindow

代码

var markers = [
    ['Bondi Beach', -33.890542, 151.274856],
    ['Coogee Beach', -33.923036, 151.259052],
    ['Cronulla Beach', -34.028249, 151.157507],
    ['Manly Beach', -33.80010128657071, 151.28747820854187],
    ['Maroubra Beach', -33.950198, 151.259302]
];

function initializeMaps() {
    var myOptions = {
        mapTypeId: google.maps.MapTypeId.ROADMAP,
        mapTypeControl: false
    };
    var map = new google.maps.Map(document.getElementById("map_canvas"),myOptions);
    var infowindow = new google.maps.InfoWindow(); 
    var marker, i;
    var bounds = new google.maps.LatLngBounds();

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

}
initializeMaps()

1 个答案:

答案 0 :(得分:2)

在您创建标记的循环中,还会创建链接并附加一个触发标记点击的点击侦听器:

(function(marker){
            google.maps.event
              .addDomListener($('<li/>')
                              .text(markers[i][0])
                               .appendTo('#list')[0],
                              'click',
                               function(){
                                google.maps.event.trigger(marker,'click',{});
                               });
 })(marker);

演示:http://jsfiddle.net/doktormolle/HNd6H/