Google Maps API V3 - 信息框和循环问题

时间:2013-12-06 16:03:24

标签: javascript google-maps google-maps-api-3

我正在使用Google Maps API V3并面临我的信息框的问题。我的代码只显示我最后一个标记的信息框。我找不到原因。可能是关闭问题,但不确定。

这是我的代码:

var directionsDisplay;
var directionsService;
var info;
var i;

function initialize() {
        var markers=[];
        var GeocoderOptions;
        var myGeocoder;
        var temp;
        info = [
        ['57 Avenue Joseph Kessel 78180 Montigny-le-Bretonneux','Paul VERLAINE','Testinfo'],
        ['24 Rue du champ d avoine 78180 Mintigny-le-Bretonneux','Charles PEGUY','Testinfo'],
        ['21 Rue du Poirier Saint Martin 78180 Mintigny-le-Bretonneux','Maurice GENEVOIX','Testinfo'],
        ['13 Rue des Pyrenees 78180 Montigny-le-Bretonneux','Neil COSAQUE','Testinfo'],
        ['14 Rue des Pyrenees 78180 Montigny-le-Bretonneux','Louise THEVENOUX','Testinfo'],
        ['9 Rue des Bleuets 78180 Montigny-le-Bretonneux','Melanie JARNET','Testinfo'],
        ['10 Rue des Bleuets 78180 Montigny-le-Bretonneux','Antony JARNET','Testinfo'],
        ['30 Rue de la Republique 78180 Montigny-le-Bretonneux','Erin BARTOUT','Testinfo'],
        ['31 Rue de la Republique 78180 Montigny-le-Bretonneux','Fabien BARTOUT','Testinfo'],
        ['29 Rue de la Republique 78180 Montigny-le-Bretonneux','Christophe BARTOUT','Testinfo']
        ];
        var pinImage;
        var marker;
        var loc =new google.maps.LatLng(48.772, 2.028);
        var mapOptions = {
          center: loc,
          zoom: 14,
          mapTypeId: google.maps.MapTypeId.ROADMAP
        };

        var map = new google.maps.Map(document.getElementById("map-canvas"),
            mapOptions);
        directionsDisplay = new google.maps.DirectionsRenderer();
        directionsService = new google.maps.DirectionsService();

        directionsDisplay.setMap(map);
        directionsDisplay.setPanel(document.getElementById('directionsPanel'));

        myGeocoder = new google.maps.Geocoder();

        for(i=0;i<info.length;i++){
            GeocoderOptions={
                'address' : info[i][0],
                'region':'FR'       
            };
            myGeocoder.geocode( GeocoderOptions, function(i){
            return function(results, status){

                if(status == google.maps.GeocoderStatus.OK){
                marker = new google.maps.Marker({
                        position: results[0].geometry.location,
                        map: map,
                        title: info[i][1],
                        icon: pinImage
                    });
                markers.push(this);
                //var contentMarker = 'Testinfo';
                var infoWindow = new google.maps.InfoWindow(/*{
                    content : contentMarker
                }*/);
                google.maps.event.addListener(marker, 'click', function() {

                infoWindow.setContent(info[i][2]);
                infoWindow.open(map,marker);
            }); 

                } else {
                    alert("L'adresse n'a pas pu etre geocodee avec succes.");
                }

            }
            }(i));      
        }
         calcRoute();

      }
      function calcRoute(){

            var request = {
                origin: info[5][0],
                destination: info[1][0],
                travelMode: google.maps.TravelMode.DRIVING
            };

            directionsService.route(request, function(response,status){
                if(status==google.maps.DirectionsStatus.OK){
                //alert("Je passe ici2");
                    directionsDisplay.setDirections(response);
                }
            });
        }
      google.maps.event.addDomListener(window, 'load', initialize);

1 个答案:

答案 0 :(得分:2)

似乎是关闭问题:你得到最后一个索引的infoWindow。我添加了一个新功能:

function addInfoWindowOnEvent(marker, infoWindow, map, event) {
    google.maps.event.addListener(marker, event, function () {
        infoWindow.open(map, marker);
    });
}

并注释/更改了事件监听器的部分:

                    //google.maps.event.addListener(marker, 'click', function() {
                    infoWindow.setContent(info[i][2]);
                    //infoWindow.open(map,marker);
                    //});
                    addInfoWindowOnEvent(marker, infoWindow, map, 'click');

infowindow的文字很少扩展,看不出差异。之前它只是 testinfo

此外,我还从标记中注释掉变量pinImage,因为它未定义。

请参阅example at jsbin