简体   繁体   English

Google地图(v3)infowindow始终在同一标记上打开

[英]Google map (v3) infowindow opening on the same marker all the time

I create several Gmarkers (from JSON data loaded by JQuery "load" function), on all of them I add an event listener to open the infowindow object I created before on the marker, and then I add them all to the map. 我创建了几个Gmarkers(来自JQuery加载的JSON数据“load”函数),在所有这些上我添加了一个事件监听器来打开我之前在标记上创建的infowindow对象,然后我将它们全部添加到地图中。

The issue is that the infowindow always opens on the same marker. 问题是infowindow总是在同一个标​​记上打开。 I all had this working before, i can't see where the problem is... scope of the variable ? 我之前都有这个工作,我看不出问题出在哪里......变量的范围? stupid mistake somewhere ? 某处愚蠢的错误?

I uploaded an example , and here is a shortcut to the javascript file 我上传了一个示例 ,这是javascript文件快捷方式

The code : 代码 :

    var map;
    var infowindow;
    $(document).ready(function() {

        var myLatlng = new google.maps.LatLng(47.15984,2.329102);
      var myOptions = {
        zoom: 6,
        center: myLatlng,
        mapTypeId: google.maps.MapTypeId.HYBRID,
        scrollwheel: false
      }

      map = new google.maps.Map(document.getElementById("map_canvas"), myOptions);

      infowindow = new google.maps.InfoWindow({content:'<p>Test</p>'});

        $.getJSON("data.json", function(data) {

            var markers = [];
            for (var i = data.length - 1; i >= 0; i--){
                var latLng = new google.maps.LatLng(data[i].lat, data[i].lng);
              var marker = new google.maps.Marker({position: latLng});

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

              markers.push(marker);
            };

            for (var j = markers.length - 1; j >= 0; j--){
                markers[j].setMap(map);
            };

        });
    });

Change 更改

infowindow.open(map,marker);

to

infowindow.open(map,this);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM