因此,基本上,我正在尝试使用Google Geocoder传递位置,并能够在调用时将返回的经度捕获到变量'x'中。但是,当我在控制台上记录它时,它返回“未定义”,但是从onSuccess()函数中它记录了经度。任何帮助将非常感谢。
function onSuccess(position) {
getLocationData(position, function(locationData){
y = locationData;
console.log("inside onSuccess Function: "+y);
return y;
});
}
function getLocationData(position, callback) {
geocoder = new google.maps.Geocoder();
if( geocoder ) {
geocoder.geocode({ 'address': position }, function (results, status) {
if( status == google.maps.GeocoderStatus.OK ) {
callback(results[0].geometry.location.lng());
}
});
}
}
var x = onSuccess('Billings,MT');
console.log("Yayyy: "+x);
答案 0 :(得分:0)
您不能这样做,但是可以使用回调方法
function onSuccess(position) {
getLocationData(position, function(locationData){
y = locationData;
console.log("inside onSuccess Function: "+y);
return y;
});
}
function getLocationData(position, callback) {
geocoder = new google.maps.Geocoder();
if( geocoder ) {
geocoder.geocode({ 'address': position }, function (results, status) {
if( status == google.maps.GeocoderStatus.OK ) {
callback(results[0].geometry.location.lng());
}
});
}
}
var x ;
onSuccess('Billings,MT', function(callBackData){
x = callBackData
console.log("Yayyy: "+x);
});