Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/365.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 Api 3 - Fatal编程技术网

谷歌地图Javascript API V3:标记具有相同的标题

谷歌地图Javascript API V3:标记具有相同的标题,javascript,google-maps-api-3,Javascript,Google Maps Api 3,我在使用谷歌地图API V3时遇到了困难。我可以在地图上显示几个标记(3个标记),但每个标记都有相同的标题(莫里斯·杰奈沃伊)。你能帮我/告诉我怎么做吗?我不明白。我的代码如下: function initialize() { var myMarker=null; var i=0; var GeocoderOptions; var myGeocoder; var nom; var temp; var info = [ ['57 Av

我在使用谷歌地图API V3时遇到了困难。我可以在地图上显示几个标记(3个标记),但每个标记都有相同的标题(莫里斯·杰奈沃伊)。你能帮我/告诉我怎么做吗?我不明白。我的代码如下:

function initialize() {
    var myMarker=null;
    var i=0;
    var GeocoderOptions;
    var myGeocoder;
    var nom;
    var temp;
    var info = [
    ['57 Avenue Joseph Kessel 78180 Montigny-le-Bretonneux','Paul VERLAINE'],
    ['24 Rue du champ d avoine 78180 Mintigny-le-Bretonneux','Pauline VERLAINE'],
    ['21 Rue du Poirier Saint Martin 78180 Mintigny-le-Bretonneux','Maurice GENEVOIX']
    ];

    var mapOptions = {
      center: new google.maps.LatLng(48.772, 2.028),
      zoom: 14,
      mapTypeId: google.maps.MapTypeId.ROADMAP
    };

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

        function GeocodingResult( results , status , i)
        {


            if(status == google.maps.GeocoderStatus.OK){

                myMarker = new google.maps.Marker({
                    position: results[0].geometry.location,
                    map: map,
                    title: nom,
                    icon: 'http://maps.google.com/mapfiles/ms/icons/green-dot.png'
                }); 

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

    for(i=0;i<info.length;i++){ 
    GeocoderOptions={
        'address' : info[i][0],
        'region':'FR'       
    };

        nom=info[i][1];

        myGeocoder = new google.maps.Geocoder();
        myGeocoder.geocode( GeocoderOptions, GeocodingResult );

    }

  }
  google.maps.event.addDomListener(window, 'load', initialize);
函数初始化(){
var myMarker=null;
var i=0;
地理变异;
var-myGeocoder;
var-nom;
无功温度;
变量信息=[
['57 Avenue Joseph Kessel 78180 Montigny le Bretoneux','Paul VERLAINE'],
['24 Rue du champ d avoine 78180 Mintigny le Bretoneux','Pauline VERLAINE'],
[“圣马丁警察街21号78180 Mintigny le Bretoneux',“莫里斯·杰纳沃伊”]
];
变量映射选项={
中心:新google.maps.LatLng(48.772,2.028),
缩放:14,
mapTypeId:google.maps.mapTypeId.ROADMAP
};
var map=new google.maps.map(document.getElementById(“地图画布”),
地图选项);
功能GeocodingResult(结果、状态、i)
{
if(status==google.maps.GeocoderStatus.OK){
myMarker=新的google.maps.Marker({
位置:结果[0]。geometry.location,
地图:地图,
标题:nom,
图标:'http://maps.google.com/mapfiles/ms/icons/green-dot.png'
}); 
}否则{
警报(“地址和地理编码成功”);
}
}

对于(i=0;i这是一个JavaScript闭包问题,而不是Google Maps JS API的问题。下面是您应该如何定义回调,以便可以获得
i
的正确值(我冒昧地使用了
GeocodingResult
函数并将其内联。)


感谢您的回答和演示,这很有帮助!我对问题的来源有或多或少的了解,但无法正确解决。现在它工作正常!那么,您能将此答案标记为“正确答案”吗?:)
myGeocoder.geocode( GeocoderOptions, function(i){
    return function(results, status){
    if(status == google.maps.GeocoderStatus.OK){
        markers.push( new google.maps.Marker({
            position: results[0].geometry.location,
            map: map,
            title: info[i][1],
            icon: 'http://maps.google.com/mapfiles/ms/icons/green-dot.png'
        })); 

    } else {
        alert("L'adresse n'a pas pu etre geocodee avec succes.");
    }
}
}(i)); // this sends the current i into the closure of the callback.