Javascript 使用Geocoder()时,MarkerWithLabel仅显示google地图api中的最后一个值

Javascript 使用Geocoder()时,MarkerWithLabel仅显示google地图api中的最后一个值,javascript,google-maps,google-maps-api-3,Javascript,Google Maps,Google Maps Api 3,添加带有MarkerWithLabel的标记时,它会显示不同的标记,但与关联的值包含数据中的最后一个值。。就像在每个标记中写入代码labelContent:building.value和title:building.Country\u Name时一样,它显示每个制造商位置的最后一个国家名称,比如尼泊尔,它显示数据中的最后一个值,比如。。63在这种情况下。我有以下JSON格式的数据 var recipient_country = [{"Country_Name":

添加带有MarkerWithLabel的标记时,它会显示不同的标记,但与关联的值包含数据中的最后一个值。。就像在每个标记中写入代码
labelContent:building.value
title:building.Country\u Name
时一样,它显示每个制造商位置的最后一个国家名称,比如尼泊尔,它显示数据中的最后一个值,比如。。63在这种情况下。我有以下JSON格式的数据

       var recipient_country = [{"Country_Name": "MYANMAR", "value": 123},
       {"Country_Name": "MONGOLIA", "value": 11},
       {"Country_Name": "ZIMBABWE", "value": 22},
       {"Country_Name": "Bahrain", "value": 45},
       {"Country_Name": "Nepal", "value": 63}];



  for(var i= 0; i < recipient_country.length; i++) {
        console.log(i);
        var building = recipient_country[i];
        console.log(building.Country_Name);

        geocoder.geocode({'address':building.Country_Name}, function(results,status){
          console.log(results);
          if(status == google.maps.GeocoderStatus.OK){
            var marker = new MarkerWithLabel({
              position:results[0].geometry.location,
              title:building.Country_Name,

              map:map,
              labelContent:building.value,
              labelAnchor:new google.maps.Point(6,22),
              labelClass:"labels",
              labelInBackground:false,
              icon:"circle2.png"
            });
             console.log(building.Country_Name)
          }
          else{
                 console.log("Geocode was not  succcessful for the following reason:" + status);
             }
        });
      
var recipient_country=[{“country_Name”:“缅甸”,“value”:123},
{“国家名称”:“蒙古”,“价值”:11},
{“国家名称”:“津巴布韦”,“价值”:22},
{“国家名称”:“巴林”,“价值”:45},
{“国家名称”:“尼泊尔”,“价值”:63}];
对于(变量i=0;i
函数是异步的,
geocoder.geocode()
函数在
循环中没有特殊的作用域,因此每次迭代都会覆盖
building
变量,在以后执行
geocode()
函数时,最后一个值会被迭代

您必须使用新范围锁定该值:

for (var j = 0; j < recipient_country.length; j++) {
    (function(i) {
        var building = recipient_country[i];

        geocoder.geocode({
            'address': building.Country_Name
        }, function (results, status) {
            console.log(results);
            if (status == google.maps.GeocoderStatus.OK) {
                var marker = new MarkerWithLabel({
                    position: results[0].geometry.location,
                    title: building.Country_Name,

                    map: map,
                    labelContent: building.value,
                    labelAnchor: new google.maps.Point(6, 22),
                    labelClass: "labels",
                    labelInBackground: false,
                    icon: "circle2.png"
                });
                console.log(building.Country_Name)
            } else {
                console.log("Geocode was not  succcessful for the following reason:" + status);
            }
        });
    })(j);
}
for(var j=0;j
如果我想在标题中显示Nepal=63,而不是在标题中显示:building.Country\u Name=+“building.value”,但它说的是InvalidValueError:setTitle:不是字符串..如何获得带有Country\u Name并等于其值的标题