Javascript 如何将回调位置捕获到变量中?
因此,基本上我尝试使用google Geocoder传入一个位置,并能够在调用时将返回的经度捕捉到变量“x”。但是,当我在控制台上记录它时,它返回“undefined”,但它从onSuccess()函数记录经度。任何帮助都很好,谢谢Javascript 如何将回调位置捕获到变量中?,javascript,Javascript,因此,基本上我尝试使用google Geocoder传入一个位置,并能够在调用时将返回的经度捕捉到变量“x”。但是,当我在控制台上记录它时,它返回“undefined”,但它从onSuccess()函数记录经度。任何帮助都很好,谢谢 function onSuccess(position) { getLocationData(position, function(locationData){ y = locationData; console.log("inside onSu
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);
您不能这样做,但可以使用回调方法
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);
});