简体   繁体   English

谷歌地图infowindow显示错误的标记

[英]Google maps infowindow showing on wrong marker

I have a piece of javascript code where I create markers and attach InfoWindows to them, like this: 我有一段javascript代码,我在其中创建标记并将InfoWindows附加到它们,如下所示:

for (var i = 0; i < 8; i++) {
    var marker = new google.maps.Marker({
       map: map,
       position: new google.maps.LatLng(lat[i], lng[i]),
       icon: '/static/images/iconsets/gmap/iconb' + (i+1) + '.png',
    });
    var infowindow = new google.maps.InfoWindow({
        content: 'test string'
    });
    google.maps.event.addListener(marker, 'click', function() {
        infowindow.open(map,marker);
    });
}

But when I click one of the markers, the infowindow always shows only on one marker. 但是当我点击其中一个标记时,infowindow总是只显示在一个标记上。 What am I doing wrong? 我究竟做错了什么?

There's a very simple solution to your problem, which is to put the loop's code into a function. 对你的问题有一个非常简单的解决方案,即将循环代码放入函数中。 Your problem is that you overwrite the variable marker , so that when it is accessed in the click event, it uses the latest version of that variable, which is the last marker you added. 您的问题是您覆盖变量marker ,以便在单击事件中访问它时,它使用该变量的最新版本,这是您添加的最后一个标记。

So, when you put it into a function, the variable is in a separate namespace and therefore not overwritten. 因此,当您将其放入函数时,该变量位于单独的命名空间中,因此不会被覆盖。 In other words, this should work: 换句话说,这应该工作:

for (var i = 0; i < 8; i++) {
    createMarker(i);
}

function createMarker(i) {
    var marker = new google.maps.Marker({
        map: map,
        position: new google.maps.LatLng(lat, lng),
        icon: '/static/images/iconsets/gmap/iconb' + (i+1) + '.png',
    });
    var infowindow = new google.maps.InfoWindow({
        content: 'test string'
    });
    google.maps.event.addListener(marker, 'click', function() {
        infowindow.open(map,marker);
    });
}
google.maps.event.addListener(Marker, 'click', function() {
    InfoWindow.open(map, this);
});

You shoud use this instead of marker because marker will hold the value of last placed marker. 您应该使用而不是标记,因为标记将保留最后放置的标记的值。

for (var i = 0; i < 8; i++) {
    var marker = new google.maps.Marker({
       map: map,
       position: new google.maps.LatLng(lat[i], lng[i]),
       icon: '/static/images/iconsets/gmap/iconb' + (i+1) + '.png',
       content: 'test string'
    });
    google.maps.event.addListener(marker, 'click', function() {
        new google.maps.InfoWindow({
            content: this.content
        }).open(map, this);
    });
}

As a new option that was not available 7 years ago: 作为7年前无法使用的新选项:

all modern browsers (which is all browsers supporting ECMAScript 6) support block scoped variables which are defined using the let statement. 所有现代浏览器(所有支持ECMAScript 6的浏览器)都支持使用let语句定义的块作用域变量。 let initializes a variable inside of a given scope eg inside of a loop whereas var defines variables on global or function level. let初始化给定范围内的变量,例如循环内部,而var定义全局或函数级别的变量。 In your case exchanging the var with let will make sure that each marker is properly initialized as a new variable: 在你的情况下,用let交换var将确保每个标记被正确初始化为一个新变量:

for (var i = 0; i < 8; i++) {
    let marker = new google.maps.Marker({
       map: map,
       position: new google.maps.LatLng(lat[i], lng[i]),
       icon: '/static/images/iconsets/gmap/iconb' + (i+1) + '.png',
    });
    let infowindow = new google.maps.InfoWindow({
        content: 'test string'
    });
    google.maps.event.addListener(marker, 'click', function() {
        infowindow.open(map,marker);
    });
}

Try this: 试试这个:

        // Create a marker for each place.
         marker = new google.maps.Marker({
             map: map,
             icon: icon,
             title: place.name,
             animation: google.maps.Animation.DROP,
             position: place.geometry.location
         });
         var infowindow = new google.maps.InfoWindow({
         content:'<div><strong>' + place.name + '</strong><br>' +
            'Place ID: ' + place.place_id + '<br>' +
            place.formatted_address + '</div>'
            });
        marker.addListener('click', function() {
        infowindow.open(map, this);
        });

Thank you 谢谢

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

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