4

我使用下面的代码来显示带有多个标记和信息窗口的地图。现在我遇到了一个非常常见的问题,即最后一个信息窗口出现在所有标记上。我尝试了各种解决方案,包括: 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);
            }

    });

}   

}
4

1 回答 1

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);
        }
    });  
}

您的脚本的其余部分可能会保持原样。

于 2012-08-11T21:47:39.537 回答