javascript - 如何将变量从 Google map 地理编码器传递到另一个函数?

标签 javascript

如何从 test2 变量中获取传递给另一个函数的值?我一直在尝试使用全局变量和链接函数,正如我发现的另一个线程中所建议的那样,但无法让它们工作。

function codeAddress(addresstocode) {

var test2 = ['123','321'];

geocoder.geocode( { 'address': addresstocode}, function(results, status) {

    if(status == google.maps.GeocoderStatus.OK) {
        map.setCenter(results[0].geometry.location);

        coded_lat = results[0].geometry.location.Ka;
        coded_long = results[0].geometry.location.La;

        //coded_location = [{"latitude" : coded_lat, "longitude" : coded_long}];

        //test2 = returnAddress(coded_location);

        test2 = [coded_lat,coded_long];

        //coded_lat = coded_location[0].latitude;
        //coded_long = coded_location[0].longitude;
        //returnAddress(coded_location);
    }

});

console.log('test2: '+test2);

return test2;


}

最佳答案

您必须创建一个函数来接收如下值:

function geocodeOK(lat, lng){
    // Do what you want with lat lng
}

在你的代码中:

function codeAddress(addresstocode) {

    var test2 = ['123', '321'];

    geocoder.geocode({
        'address': addresstocode
    }, function(results, status) {

        if (status == google.maps.GeocoderStatus.OK) {
            map.setCenter(results[0].geometry.location);

            coded_lat = results[0].geometry.location.Ka;
            coded_long = results[0].geometry.location.La;
            geocodeOK(coded_lat, coded_long);
        }

    });

}

关于javascript - 如何将变量从 Google map 地理编码器传递到另一个函数?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6809640/

相关文章:

javascript - 如何解析一个URL?

javascript - 确保 AMD 模块在内容脚本之前加载

javascript - 谷歌地图 API : Move map center to a position on my page?

javascript - 在 JCarousel 中获取当前项目的索引

javascript - 字段集的动态命名

javascript - 当响应式导航折叠开始时引发 JavaScript 事件

javascript - 自适应 AngularJS 指令

javascript - Jquery UI timepicker 附加触发解析错误

javascript - async/await 如何与 async 函数中的回调一起工作?

javascript - 隐藏列表 jQuery 中的最后 3 个元素