如何从Node.js上的Google地理编码API返回结果

时间:2017-12-20 05:56:08

标签: javascript node.js google-maps google-geocoder google-geocoding-api

我尝试创建一个函数,该函数使用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');

2 个答案:

答案 0 :(得分:0)

你的函数应该是异步的,你可以使用promises或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');

答案 1 :(得分:0)

您可以使用简单的回调来使其工作。我还修复了一些错误。这是您的代码的工作版本。

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);
    }
});