javascript - 如何在jquery函数内的函数内传递变量?

标签 javascript jquery google-maps-api-3

所以我正在使用谷歌地图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));
}

关于javascript - 如何在jquery函数内的函数内传递变量?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16697694/

相关文章:

Jquery 自动完成与 knockoutjs

javascript - jquery中添加/删除类的问题

google-maps - 如何在运行时更改 Google map 的语言?

php - 来自 xml 文件的谷歌地图上的多个标记

javascript - 通过 socket.io 与 heroku 应用程序进行通信

javascript - HTML 选择下拉菜单,Jquery 问题

javascript - 为 AngularJS 中的列分配颜色

javascript - UTF-8 字符串比较在 javascript 中返回 false

javascript - 在滚动更改菜单 HTML 的颜色

javascript - map API 3 重置边界