当使用Geocoder()时,MarkerWithLabel仅显示google地图api中的最后一个值
MarkerWithLabel show only last value in the google map api when using Geocoder()
当我用MarkerWithLabel添加标记时,它显示了不同的标记,但与之关联的值包含数据的最后一个值。就像在每个标记中,当编写代码labelContent:building.value
和title:building.Country_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);
}
});
geocoder.geocode()
函数是异步的,因为在for
循环中没有特殊的作用域,所以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);
}
相关文章:
- 是否可以控制获取哪些Google地图脚本(JavaScript API)
- 单击超链接时,如何使用Google Maps API v3缩放地图
- 在angularjs/google地图中显示分支最近的路线
- 创建类似于Google analytics的分析地图
- Javascript:Google Maps API:如果地图加载在隐藏容器中,则为空白地图
- 使用Google地图API v3显示多个标记
- 我是否应该在网站的每个页面上都包含 Google 地图脚本标签
- 使用自动完成服务()将 Google 地图地点限制为特定国家/地区
- Google 地图信息窗口仅显示首次点击时的信息
- 在 Google 地图 API V3 中启用和禁用大量标记
- 使用JavaScript获取Google地图上显示的所有推文时遇到麻烦
- Google Maps Javascript API v3 地图显示在桌面浏览器上,但不会显示在移动浏览器上
- 如何将waze事件标记添加到带有流量的google地图javascript API生成的地图
- 将PHP集成到Javascript中,使用Google API显示地图标记
- 如何制作Google Maps API v3六边形拼接地图,最好是基于坐标的
- Google Maps v3 API-在地图上绘制圆圈
- 外部SVG作为Google地图api标记-是或否
- Google Map JS API-加载地图分幅,但所有图像(标记、缩放控制等)都不加载;t负载2分钟
- 获取属性(邮政编码)并在Google地图API 3中使用
- 如何在PhoneGap中实现windows平台的地图(Google或Bing)