【发布时间】:2020-09-08 07:40:32
【问题描述】:
使用 Google 地图和地理编码器,我尝试遍历地址对象,为它们返回 LatLng 地址,并在下面的 setMarker 函数中使用原始详细信息和 latlng 地址创建标记。
问题是,response[a] 被对象中的最后一个地址覆盖,因为 for 循环在 AJAX 结果返回之前运行。
如何保存当前循环的response[a] 中的数据,以便以后调用setMarker() 时,它包含正确的信息?
谢谢
var limit = 0;
for (a in response){
if(limit<5){ // limit API calls
var addr = [response[a].Addr1, response[a].City, response[a].Zip];
geo = new google.maps.Geocoder();
geo.geocode({
address: addr.join(", "),
componentRestrictions: {
// country: 'UK'
}
}, function (results, status) {
if (status == google.maps.GeocoderStatus.OK && results) {
var latitude = results[0].geometry.location.lat();
var longitude = results[0].geometry.location.lng();
var latlng = new google.maps.LatLng(latitude, longitude);
if(latitude!="" && longitude!=""){
bounds.extend(latlng);
map.fitBounds(bounds);
_this.setMarker(map, limit, latlng, response[a]);
}
} // if geo results
});
}
limit++;
}
【问题讨论】:
标签: javascript google-maps google-geocoder