【发布时间】:2014-10-09 04:16:26
【问题描述】:
我正在尝试对 Google 地图进行异步调用以获取 foreach 循环内的地址。
这是函数:
//Function to get address from geographic coordinates
function getAddress(item) {
var address = 'Not fetched';
geocoder = new google.maps.Geocoder();
var latlng = new google.maps.LatLng(parseFloat(item.Latitude), parseFloat(item.Longitude));
geocoder.geocode({ 'latLng': latlng }, function (results, status) {
if (status == google.maps.GeocoderStatus.OK)
address = results[0].formatted_address;
});
return address;
}
这是循环:
recreateMarkers: function (self, data) {
data.vehicleInfo.forEach(function (item) {
self.Location= getAddress(item);
});
}
数据结构: 1.车辆标识 2. 纬度 3. 经度 4. 位置
现在的问题是它为所有车辆提供了相同或未定义的位置。
如果有人可以提供帮助会很高兴。
【问题讨论】:
-
如果没有循环,这甚至都行不通。
标签: javascript jquery asynchronous closures