簡體   English   中英

如何從Node.js上的Google地理編碼API返回結果

[英]How do you return the result from the google geocoding API on Node.js

我正在嘗試創建一個函數,該函數使用適用於node.js的Google地理編碼API創建一個名為geodata的變量並將其返回。 由於某種原因,值temp將具有我想要的結果,但是如果我在函數之外使用i,則temp的值未定義。

var temp;
function getInfo(inAddress){
  googleMapsClient.geocode({
    address: inAddress,
    region: 'MY'
  }, function(err, response) {
    if (!err) {
      details = response.json.results;
      var geodata = {
        "status": response.json.status,
        "formattedAddress": details[0].formatted_address,
        "latitude": details[0].geometry.location.lat,
        "longitude": details[0].geometry.location.lng,
        "type": details[0].geometry.location_type,
      };
      for(i=0; details[0].address_components.length; i++){
        if(details[0].address_components[i].types == "postal_code"){
          geodata.postCode = details[0].address_components[i].long_name;
        }
        temp = geodata
        console.log(temp); 
      }
    }
  });
}

getInfo('Nadayu28');

您的函數應該是異步的,您可以使用promise或async / await來實現它,如下所示:

const Promise=require('bluebird');

async function getInfo(inAddress){
  return Promise.promisify(googleMapsClient.geocode)({
    address: inAddress,
    region: 'MY'
  }).then((response)=> {
      details = response.json.results;
      var geodata = {
        "status": response.json.status,
        "formattedAddress": details[0].formatted_address,
        "latitude": details[0].geometry.location.lat,
        "longitude": details[0].geometry.location.lng,
        "type": details[0].geometry.location_type,
      };
      for(i=0; details[0].address_components.length; i++){
        if(details[0].address_components[i].types == "postal_code"){
          geodata.postCode = details[0].address_components[i].long_name;
        }
        return geodata;
      }
    }
  });
}

const data = await getInfo('Nadayu28');

您可以使用簡單的回調使其工作。 我還修復了一些錯誤。 這是您的代碼的工作版本。

var googleMapsClient = require('@google/maps').createClient({
    key: 'Your Key'
});

function getInfo(inAddress, callback) {
    googleMapsClient.geocode({
        address: inAddress,
        region: 'MY'
    }, function(err, response) {
        if (!err) {
            details = response.json.results;
            var geodata = {
                "status": response.json.status,
                "formattedAddress": details[0].formatted_address,
                "latitude": details[0].geometry.location.lat,
                "longitude": details[0].geometry.location.lng,
                "type": details[0].geometry.location_type,
            };
            for (i = 0; i < details[0].address_components.length; i++) {
                if (details[0].address_components[i].types.indexOf("postal_code") !== -1) {
                    geodata.postCode = details[0].address_components[i].long_name;
                }
            }

            callback(null, geodata);
        } else {
            callback(err, null);
        }
    });
}

getInfo('Nadayu28', function(err, response) {
    if (err) {
        console.error(err);
    } else {
        console.log(response);
    }
});

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM