谷歌地图 infowindows 始终附加到第一个标记

发布于 2024-12-06 03:59:24 字数 1744 浏览 0 评论 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();

i'm looping over an array to create markers and infowindows but the infowindow is always attached to the first marker... (even if you click another marker)

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();

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

梦魇绽荼蘼 2024-12-13 03:59:24

试试这个:

var map;

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

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

try this:

var map;

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

The variable "map" now is out of setMarker's scope

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文