0
以下是我正在處理的內容: getLocation('123 Foobar Ln');節點Google地理編碼器回調不起作用
function getLocation(location) {
console.log(location); // prints 123 Foobar Ln
getLocationData(location, function(gotLocation) {
console.log('hello?'); // this doesn't print
return gotLocation;
});
}
function getLocationData(location, callback) {
geocoder.geocode(location, function(err, res) {
if (res[0] != undefined) {
geoaddress = (res[0].formattedAddress);
addressmessage = 'Formatted Address: ' + geoaddress;
callback(addressmessage);
} else {
geocoder.geocode(cleanedAddress, function(err, res) {
addressmessage = 'null';
if (res[0] != undefined) {
geoaddress = (res[0].formattedAddress);
addressmessage = 'Formatted Address: ' + geoaddress;
callback(addressmessage);
} else {
addressmessage = 'Address could not be found: ' + location;
callback(addressmessage);
}
});
}
});
}
我掙扎擁有的getLocation做任何事情,從getLocationData回調。
當我運行下面的我得到的唯一的輸出繼電器是:123 Foobar Ln
有人能指出我在做什麼錯在這裏?
你永遠不會打電話給它。你只需將它設置爲等於該字符串。 – 1252748
修正了通過使用'回調(addressmessage),但它仍然沒有返回任何東西=/ –
首先''geocode'方法是aysnc,所以你打電話給你的回調之前,他們完成。將它移到每個回調函數的底部。另外,你沒有使用你的錯誤參數。將來,每個帶有錯誤參數的函數下面會打印錯誤,如果它存在以獲得一些見解。 'if(err){console.log('geocode error',err);}','if(err){console.log('geocode clean error',err);}' – 1252748