我有一个函数可以从google API的reverseGeocode函数中获取位置详细信息,如下所示。
var getLocationDetails = function (callback, latLng) {
console.log("Getting location details using Google API" + latLng.lat + latLng.lng);
var locationDetails = {};
googleMapsClient.reverseGeocode({
latlng: [parseFloat(latLng.lat),parseFloat(latLng.lng)]
}, function(err, response) {
if (!err) {
locationDetails.address = response.json.results[0].formatted_address ;
locationDetails.locality = response.json.results[1].address_components[2].long_name;
locationDetails.area = response.json.results[1].address_components[3].long_name;
locationDetails.state = response.json.results[1].address_components[4].long_name;
locationDetails.country = response.json.results[1].address_components[5].long_name;
locationDetails.zipCode = response.json.results[1].address_components[6].long_name;
console.log("Fetched location is " + locationDetails.address);
callback(err,locationDetails);
} else {
callback(err,locationDetails);
}
});
console.log("blah blah");
};

当我像下面这样调用这个函数时,执行悄悄地来到" console.log(" blah blah");"。但是当我只调用googleMapsClient.reverseGeocode()时,它会进入reverseGeocode内部的回调并打印所有locationDetails。为什么在reverGeocode()完成后没有执行该函数(错误,响应)。
loc.getLocationDetails( function(err, locationDetails) {
if (!err) {
//console.log(response.json.results[1].address_components);
console.log(locationDetails.address);
} else {
console.log("could not fetch location details");
}
}