如何从 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/