当从另一个函数调用时,reverseGeocode不会回到它的回调。

时间:2017-03-06 04:28:12

标签: javascript node.js

我有一个函数可以从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");
                }
}




0 个答案:

没有答案