我有一个设置如下的功能
function mainFunction() {
function subFunction() {
var str = "foo";
return str;
}
}
var test = mainFunction();
alert(test);
根据我的逻辑,该警报应该返回'foo',而是返回undefined。我做错了什么?
更新:这是我的实际代码(这是使用Google API进行反向地理编码的功能)
function reverseGeocode(latitude,longitude){
var address = "";
var country = "";
var countrycode = "";
var locality = "";
var geocoder = new GClientGeocoder();
var latlng = new GLatLng(latitude, longitude);
return geocoder.getLocations(latlng, function(addresses) {
address = addresses.Placemark[0].address;
country = addresses.Placemark[0].AddressDetails.Country.CountryName;
countrycode = addresses.Placemark[0].AddressDetails.Country.CountryNameCode;
locality = addresses.Placemark[0].AddressDetails.Country.AdministrativeArea.SubAdministrativeArea.Locality.LocalityName;
return country;
});
}
答案 0 :(得分:59)
你必须先调用一个函数才能返回任何内容。
function mainFunction() {
function subFunction() {
var str = "foo";
return str;
}
return subFunction();
}
var test = mainFunction();
alert(test);
或者:
function mainFunction() {
function subFunction() {
var str = "foo";
return str;
}
return subFunction;
}
var test = mainFunction();
alert( test() );
为您的实际代码。返回应该在主函数外部。回调是在getLocations
方法内的某处调用的,因此它的返回值不会在主函数中被收到。
function reverseGeocode(latitude,longitude){
var address = "";
var country = "";
var countrycode = "";
var locality = "";
var geocoder = new GClientGeocoder();
var latlng = new GLatLng(latitude, longitude);
geocoder.getLocations(latlng, function(addresses) {
address = addresses.Placemark[0].address;
country = addresses.Placemark[0].AddressDetails.Country.CountryName;
countrycode = addresses.Placemark[0].AddressDetails.Country.CountryNameCode;
locality = addresses.Placemark[0].AddressDetails.Country.AdministrativeArea.SubAdministrativeArea.Locality.LocalityName;
});
return country
}
答案 1 :(得分:3)
右。传递给getLocations()的函数在数据可用之前不会被调用,因此在设置之前返回“country”对你没有帮助。
您需要这样做的方法是让您传递给geocoder.getLocations()的函数实际执行您想要使用返回值完成的任何操作。
这样的事情:
function reverseGeocode(latitude,longitude){
var geocoder = new GClientGeocoder();
var latlng = new GLatLng(latitude, longitude);
geocoder.getLocations(latlng, function(addresses) {
var address = addresses.Placemark[0].address;
var country = addresses.Placemark[0].AddressDetails.Country.CountryName;
var countrycode = addresses.Placemark[0].AddressDetails.Country.CountryNameCode;
var locality = addresses.Placemark[0].AddressDetails.Country.AdministrativeArea.SubAdministrativeArea.Locality.LocalityName;
do_something_with_address(address, country, countrycode, locality);
});
}
function do_something_with_address(address, country, countrycode, locality) {
if (country==="USA") {
alert("USA A-OK!"); // or whatever
}
}
如果您每次获得该位置时可能想要做一些不同的事情,那么将该函数作为附加参数传递给reverseGeocode:
function reverseGeocode(latitude,longitude, callback){
// Function contents the same as above, then
callback(address, country, countrycode, locality);
}
reverseGeocode(latitude, longitude, do_something_with_address);
如果这看起来有点混乱,那么你可以看看Dojo中的Deferred功能,这使得函数之间的链接更加清晰。
答案 2 :(得分:1)
仅供参考,Geocoder是异步的,所以接受的答案虽然逻辑在这个实例中并不真正起作用。我希望有一个外部对象充当你的更新程序。
var updater = {};
function geoCodeCity(goocoord) {
var geocoder = new google.maps.Geocoder();
geocoder.geocode({
'latLng': goocoord
}, function(results, status) {
if (status == google.maps.GeocoderStatus.OK) {
updater.currentLocation = results[1].formatted_address;
} else {
if (status == "ERROR") {
console.log(status);
}
}
});
};