google maps api中的多个infoBubbles显示最后添加的内容

时间:2014-11-21 16:13:18

标签: jquery google-maps google-maps-api-3 infobubble

我在google maps api v3中遇到了infoBubble的问题。我从db加载标记并为该标记添加infoBubbles。但是从最后添加的标记开始只使用infoBubble。你能看到问题吗?

 var gmarkers = [];
 var map;

 function load() {

     var defaultLocation = new google.maps.LatLng(49.8248188, 15.4684482);

     var map = new google.maps.Map(document.getElementById("map"), {
         center: defaultLocation,
         zoom: 8,
         mapTypeId: 'roadmap'
     });

     downloadUrl("generatexml.php", function (data) {
         var xml = data.responseXML;
         var markers = xml.documentElement.getElementsByTagName("marker");
         for (var i = 0; i < markers.length; i++) {
             var id = markers[i].getAttribute("id");
             var name = markers[i].getAttribute("name");
             var point = new google.maps.LatLng(
             parseFloat(markers[i].getAttribute("lat")),
             parseFloat(markers[i].getAttribute("lng")));
             var marker = new google.maps.Marker({
                 map: map,
                 position: point
             });

             var infoBubble = new InfoBubble({
                 content: '<div' + name + '</div',
                 boxClass: 'info-box',
                 alignBottom: true,
                 pixelOffset: new google.maps.Size(-150, -40),
                 maxWidth: 300,
                 disableAutoPan: false,
                 hideCloseButton: false,
             });

             google.maps.event.addListener(marker, 'click', function () {
                 infoBubble.open(map, marker);
                 console.log(infoBubble.content);
             });

             gmarkers.push(marker);

         }
     });
 }

1 个答案:

答案 0 :(得分:0)

我找到了,只需替换:

  

google.maps.event.addListener(marker,'click',function(){

            infoBubble.open(map, marker);
            console.log(infoBubble.content);

        });

使用:

  

google.maps.event.addListener(marker,'click',function(){

                   infoBubble.setContent(this.content);
                   infoBubble.open(map, this);

         });

感谢您的帮助。