因此,我正在使用Google Map API。
我必须在对象内部插入值lat和lon,但不能执行以下操作:
$.get(url).done(buscaGPS(data, i));
function buscaGPS(data, i) {
}
码
objecto = [{
"address_1": "Avenida da Republica, 15A",
"city": "Lisbon",
"country": "pt",
"id": 1534282,
"name": "Pastelaria Versailles"
}, {
"address_1": "Avenida da Republica, 15A",
"city": "Lisbon",
"country": "pt",
"id": 1534282,
"name": "Pastelaria Versailles"
}];
for (var i = 0; i < objecto.length; i++) {
var url = "http://maps.googleapis.com/maps/api/geocode/json?address=" + objecto[i].address_1 + "+" + objecto[i].city + "+" + objecto[i].country + "&sensor=false";
$.get(url).done(buscaGPS);
};
function buscaGPS(data) {
objecto[i].lat = data.results[0].geometry.location.lat;
objecto[i].lon = data.results[0].geometry.location.lng;
}
最佳答案
听起来您是想做类似...
// create a closure to capture the index
function callback(index){
return function(data) {
objecto[index].lat = data.results[0].geometry.location.lat;
objecto[index].lon = data.results[0].geometry.location.lng;
}
}
for (var i = 0; i < objecto.length; i++) {
var url = "http://maps.googleapis.com/maps/api/geocode/json?address=" + objecto[i].address_1 + "+" + objecto[i].city + "+" + objecto[i].country + "&sensor=false";
$.get(url).done(callback(i));
}