Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/392.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/google-maps/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 谷歌地图API V3-信息盒和循环问题_Javascript_Google Maps_Google Maps Api 3 - Fatal编程技术网

Javascript 谷歌地图API V3-信息盒和循环问题

Javascript 谷歌地图API V3-信息盒和循环问题,javascript,google-maps,google-maps-api-3,Javascript,Google Maps,Google Maps Api 3,我正在使用GoogleMapsAPIv3,我的信息框面临一个问题。“我的代码”仅显示上一个标记的信息框。我不知道为什么。可能是结案问题,但不确定 这是我的密码: var directionsDisplay; var directionsService; var info; var i; function initialize() { var markers=[]; var GeocoderOptions; var myGeocoder;

我正在使用GoogleMapsAPIv3,我的信息框面临一个问题。“我的代码”仅显示上一个标记的信息框。我不知道为什么。可能是结案问题,但不确定

这是我的密码:

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);
var方向显示;
var定向服务;
var信息;
var i;
函数初始化(){
var标记=[];
地理变异;
var-myGeocoder;
无功温度;
信息=[
['57 Avenue Joseph Kessel 78180 Montigny le Bretoneux','Paul VERLAINE','Testinfo'],
['24 Rue du champ d avoine 78180 Mintigny le Bretoneux','Charles PEGUY','Testinfo'],
['21 Rue du Poirier Saint Martin 78180 Mintigny le Bretoneux','Maurice GENEVOIX','Testinfo'],
[‘比利牛斯山脉13街78180蒙蒂尼·勒布雷托内乌斯’、‘尼尔·科萨库’、‘Testinfo’],
['14路比利牛斯山脉78180蒙蒂尼勒布列顿','Louise THEVENOUX','Testinfo'],
[‘布卢埃茨街9号,布列托内乌斯山78180号’,‘梅勒妮·贾内特’,‘Testinfo’,
[‘布卢埃茨街10号,布列托内乌斯山78180号’,‘安东尼·贾内特’,‘测试信息’,
['30 Rue de la Republicue 78180 Montigny le Bretoneux','Erin BARTOUT','Testinfo'],
['31 Rue de la Republicue 78180 Montigny le Bretoneux','Fabien BARTOUT','Testinfo'],
['29 Rue de la Republicue 78180 Montiny le Bretoneux','Christophe BARTOUT','Testinfo']
];
var-pinImage;
var标记;
var loc=new google.maps.LatLng(48.772,2.028);
变量映射选项={
中心:loc,
缩放:14,
mapTypeId:google.maps.mapTypeId.ROADMAP
};
var map=new google.maps.map(document.getElementById(“地图画布”),
地图选项);
directionsDisplay=new google.maps.DirectionsRenderer();
directionsService=new google.maps.directionsService();
方向显示.setMap(地图);
directionsDisplay.setPanel(document.getElementById('directionsPanel');
myGeocoder=新的google.maps.Geocoder();

对于(i=0;i似乎是闭包问题:您将获得上一个索引的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之前

此外,我还注释了marker中的变量
pinImage
,因为它没有定义


请参见

事实上,它在信息框中显示了正确的内容(它会根据所选标记的不同而变化)。但无论单击什么,信息框的位置仍然是最后一个标记的位置。