JavaScript代码似乎跳过了对另一个函数的回调
我正在JavaScript文件中编写函数以输出地址。它不是最干净的,但在我当前的问题出现之前它就已经起作用了。我正在尝试JavaScript代码似乎跳过了对另一个函数的回调,javascript,callback,Javascript,Callback,我正在JavaScript文件中编写函数以输出地址。它不是最干净的,但在我当前的问题出现之前它就已经起作用了。我正在尝试回调并获取地址,但当我将地址记录到控制台时,它是未定义的。我不确定我做错了什么 function calculateDistance(vnames, vlocations) { // PROGRAM NEVER GOES THROUGH THIS??? clientLoc((address) => { var origin = addres
回调
并获取地址,但当我将地址记录到控制台
时,它是未定义的
。我不确定我做错了什么
function calculateDistance(vnames, vlocations) {
// PROGRAM NEVER GOES THROUGH THIS???
clientLoc((address) => {
var origin = address;
alert("Address: " + address);
});
// PROGRAM NEVER GOES THROUGH THIS???
console.log("my location is: " + origin);
var venueNames = vnames,
venueLocs = vlocations,
service = new google.maps.DistanceMatrixService();
// 5. Output band name and distance
// Matrix settings
service.getDistanceMatrix(
{
origins: [origin],
destinations: venueLocs,
travelMode: google.maps.TravelMode.DRIVING, // Calculating driving distance
unitSystem: google.maps.UnitSystem.IMPERIAL, // Calculate distance in mi, not km
avoidHighways: false,
avoidTolls: false
},
callback
);
// Place the values into the appropriate id tags
function callback(response, status) {
// console.log(response.rows[0].elements)
// dist2 = document.getElementById("distance-result-2"),
// dist3 = document.getElementById("distance-result-3");
for(var i = 1; i < response.rows[0].elements.length + 1; i++) {
var name = document.getElementById("venue-result-" + i.toString()),
dist = document.getElementById("distance-result-" + i.toString());
// In the case of a success, assign new values to each id
if(status=="OK") {
// dist1.value = response.rows[0].elements[0].distance.text;
name.innerHTML = venueNames[i-1];
dist.innerHTML = response.rows[0].elements[i-1].distance.text;
} else {
alert("Error: " + status);
}
}
}
}
具有回调的函数不会阻止执行,因此会调用函数clientLoc,如果该代码有效,则可能会设置原始变量,并触发警报调用。。。但是clientLoc下面的代码没有等待clientLoc调用完成。。。它将继续执行函数的其余部分。。。当然,我对es6语法不太熟悉,但概念是一样的。您可能想要移动console.log(“我的位置是:“+origin”);任何在回调中设置的原始变量上的代码,为了使其更干净,请使用一些承诺谢谢!我最初担心代码的其余部分没有等待位置请求准备就绪。我将代码移到clientLoc回调之外的内部。你会如何用承诺让它变得更干净?
// Find the location of the client
function clientLoc (callback) {
// Initialize variables
var lat, lng, location
// Check for Geolocation support
if (navigator.geolocation) {
console.log('Geolocation is supported!');
// Use geolocation to find the current location of the client
navigator.geolocation.getCurrentPosition(function(position) {
console.log(position);
lat = position.coords.latitude;
lng = position.coords.longitude;
// Client location coordinates (latitude and then longitude)
location = position.coords.latitude + ', ' + position.coords.longitude
// console.log(location)
// Use Axios to find the address of the coordinates
axios.get('https://maps.googleapis.com/maps/api/geocode/json?key=AIzaSyAg3DjzKVlvSvdrpm1_SU0c4c4R017OIOg', {
params: {
address: location,
key: 'AIzaSyBH6yQDUxoNA3eV81mTYREQkxPIWeZ83_w'
}
})
.then(function(response) {
// Log full response
console.log(response);
var address = response.data.results[0].formatted_address;
// Return the address
console.log(address);
//return clientLoc.address;
// CALLBACK
callback(address);
})
});
}
else {
console.log('Geolocation is not supported for this Browser/OS version yet.');
return null;
}
}