Google地图 - 多个标记 - 1个InfoWindow问题 [英] Google Maps - Multiple markers - 1 InfoWindow problem

查看:162
本文介绍了Google地图 - 多个标记 - 1个InfoWindow问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我无法让我的标记显示不同的infowindows。标记始终显示最后一个contentString的内容。



我已经阅读了其他一些帖子,但没有一篇似乎有帮助。



这是代码:

 函数setMarkers(branches,map){
var bounds = new google.maps.LatLngBounds();
var contentString = null;
var infowindow = null;
infowindow = new google.maps.InfoWindow();
for(var i = 0; i< branches.length; i ++){
var marker = null;
branch = branches [i];
var myLatlngMarker = new google.maps.LatLng(branch [0],branch [1]);
contentString ='< p>'+ branch [3] +'< / p>';

var marker = new google.maps.Marker({
position:myLatlngMarker,
map:map,
title:branch [2]
} );

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

bounds.extend(myLatlngMarker);
}

map.fitBounds(bounds);
}

任何人都可以看到我做错了什么?



谢谢

解决方案



我找到了一个解决方案。我为标记'info'添加了一个额外的属性,然后从'addlistener'事件'infowindow.setContent(this.info)'中引用它。

这里是更新的代码:

  function setMarkers(branches,map){
var marker = null;
var infowindow = new google.maps.InfoWindow();
var bounds = new google.maps.LatLngBounds();

for(var i = 0; i< branches.length; i ++){
branch = branches [i];

var myLatlngMarker = new google.maps.LatLng(branch [0],branch [1]);

var marker = new google.maps.Marker({
position:myLatlngMarker,
map:map,
title:branch [2],
info:branch [3],
icon:'<%=图标%>'
});

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

bounds.extend(myLatlngMarker);
}

map.fitBounds(bounds);
}


I can't get my markers to display different infowindows. The markers always display the content of the last "contentString".

I've read a few other posts but none of them seem to help.

This is the code:

    function setMarkers(branches, map) {
        var bounds = new google.maps.LatLngBounds();
        var contentString = null;
        var infowindow = null;
        infowindow = new google.maps.InfoWindow();
        for (var i = 0; i < branches.length; i++) {
            var marker = null;
            branch = branches[i];
            var myLatlngMarker = new google.maps.LatLng(branch[0], branch[1]);
            contentString = '<p>' + branch[3] + '</p>';

            var marker = new google.maps.Marker({
                position: myLatlngMarker,
                map: map,
                title: branch[2]
            });

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

            bounds.extend(myLatlngMarker);
        }

        map.fitBounds(bounds);
    }

Can anyone see what am I doing wrong?

Thanks

解决方案

Right,

I found a solution. I added an extra property to the marker called 'info' and then referenced it from the 'addlistener' event 'infowindow.setContent(this.info)'.

Here is the updated code:

function setMarkers(branches, map) {
    var marker = null;
    var infowindow = new google.maps.InfoWindow();
    var bounds = new google.maps.LatLngBounds();

    for (var i = 0; i < branches.length; i++) {
        branch = branches[i];

        var myLatlngMarker = new google.maps.LatLng(branch[0], branch[1]);

        var marker = new google.maps.Marker({
            position: myLatlngMarker,
            map: map,
            title: branch[2],
            info: branch[3],
            icon: '<%=Icon%>'
        });

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

        bounds.extend(myLatlngMarker);
    }

    map.fitBounds(bounds);
}

这篇关于Google地图 - 多个标记 - 1个InfoWindow问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆