0

我正在遍历一个数组以创建标记和信息窗口,但信息窗口始终附加到第一个标记...(即使您单击另一个标记)

function initialize() {
    var myOptions = {
        zoom: 8,
        center: new google.maps.LatLng(50.902946,4.241436),
        mapTypeId: google.maps.MapTypeId.ROADMAP
    }
    var map = new google.maps.Map(document.getElementById("map_canvas"),
                                        myOptions);

    setMarkers(map, shops);
   }

    var shops = [['United Brands', 51.218342, 4.405464, 4, 'ZZEERRR'],['United Brands', 51.162582, 4.991303, 4, 'ZZEERRR'],['United Brands Hasselt', 50.929314, 5.339501, 4, 'ZZEERRR'],['United Brands Oud-Turnhout', 51.320259, 4.974206, 4, 'ZZEERRR'],['United Brands Leuven', 50.879509, 4.705221, 4, 'ZZEERRR'],['United Brands Veurne', 51.083534, 2.644152, 4, 'ZZEERRR'],['United Brands Wijnegem', 51.222084, 4.498419, 4, 'ZZEERRR'],['United Brands Knokke', 51.340641, 3.234586, 4, 'ZZEERRR'],['United Brands Aalst', 50.938343, 4.034963, 4, 'ZZEERRR'],['UBextreme', 51.052570, 3.723077, 4, 'ZZEERRR'],['UBextreme', 51.218273, 4.402394, 4, 'ZZEERRR']];

    function setMarkers(map, locations) {
      for (var i = 0; i < locations.length; i++) {
        var shop = locations[i];
        var myLatLng = new google.maps.LatLng(shop[1], shop[2]);
        var marker = new google.maps.Marker({
            position: myLatLng,
            map: map,
            animation: google.maps.Animation.DROP,
            title: shop[0],
            zIndex: shop[3],
            clickable: true
        });
        var infowindow = new google.maps.InfoWindow({ content: shop[4]  });
        google.maps.event.addListener(marker, 'click', function() {
            infowindow.open(map,marker);
        });

      }
    }

    initialize();
4

1 回答 1

0

试试这个:

var map;

function initialize(){
    ...
    map = ...
}

变量“map”现在超出了 setMarker 的范围

于 2011-09-25T10:09:31.143 回答