Infowindow谷歌地图API v3多个标记

时间:2012-08-11 16:02:52

标签: google-maps marker

我已使用下面的代码显示包含多个标记和infowindows的地图。现在我遇到了最后一个infowindow出现在所有标记上的常见问题。我尝试了各种解决方案,包括:http://you.arenot.me/2010/06/29/google-maps-api-v3-0-multiple-markers-multiple-infowindows/和此http://www.robertbolton.com/blog/google-maps-v3-multiple-markers-and-infowindows-in-a-loop,但没有一个能解决问题。

这是我的代码:

var infowindow = null;
var geocoder;
var map; 

$(document).ready(function() {
    initialize();
});

function initialize() {

    var myOptions = {
            zoom: 8, 
            mapTypeId: google.maps.MapTypeId.ROADMAP, 
            disableDefaultUI: true, 
            scrollwheel: false 
    };

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

    setMarkers(map, people);

    infowindow = new google.maps.InfoWindow({
            content: "loading..."
        });

}

    var people = [
        {"userid":"47","lastname":"lastname1","firstname":"firstname1","address":"Paris, France","phone1":"00000000000","phone2":"","email":"me@me.com"},
        {"userid":"42","lastname":"lastname2","firstname":"firstname2","address":"Versaille, France","phone1":"0","phone2":"0","email":"me@me.com"}
    ];

    function setMarkers(map, people) {

    for (var i = 0; i < people.length; i++) {
         var p = people[i];

        geocoder = new google.maps.Geocoder();

        geocoder.geocode( { 'address': p["address"] }, function(results, status) {

            if (status == google.maps.GeocoderStatus.OK) {
            map.setCenter(results[0].geometry.location);


                var marker = new google.maps.Marker({
                    position: results[0].geometry.location,
                    map: map,
                    html: p["address"]
                });

                var contentString = "Some content";

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


            } else {
                alert("Geocode was not successful for the following reason: " + status);
            }

    });

}   

}

1 个答案:

答案 0 :(得分:7)

地理编码是一种异步请求。因此,当您在for循环中使用地理编码时,在收到结果时循环已经完成,i将始终为1并指向最后一个项目。

您可以做什么:将标记创建分成2个功能。 1表示循环,它调用创建标记的第二个函数:

删除当前函数setMarkers并将以下2个函数添加到脚本中:

function setMarkers(map,people) {
   for (var i = 0; i < people.length; i++) {
      setMarker(map, people[i])
   }
}

function setMarker(map, people) {     
    var p=people;
    geocoder = new google.maps.Geocoder();

    geocoder.geocode( { 'address': p["address"] }, function(results, status) {
        if (status == google.maps.GeocoderStatus.OK) {
            map.setCenter(results[0].geometry.location);

            var marker = new google.maps.Marker({
                position: results[0].geometry.location,
                map: map,
                html: p["address"]
            });

            var contentString = "Some content";

            google.maps.event.addListener(marker, "click", function () {
                infowindow.setContent(this.html);
                infowindow.open(map, this);
            });
        } 
        else {
            alert("Geocode was not successful for the following reason: " + status);
        }
    });  
}

您脚本的其余部分可能保持不变。