【问题标题】:How do you return the result from the google geocoding API on Node.js如何从 Node.js 上的谷歌地理编码 API 返回结果
【发布时间】:2017-12-20 05:56:07
【问题描述】:

我正在尝试创建一个函数,该函数使用 node.js 的谷歌地理编码 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');

【问题讨论】:

  • 异步代码是异步的
  • 我可以使用承诺吗?那会有帮助吗?我正在尝试学习如何使用它们,但我想知道我是否只是在浪费时间
  • promises 也无济于事,因为它们也是异步的,所以如果你不知道如何使用异步代码,Promises 也无济于事。但是,它们的下一个演变,async/await,可以使异步代码“看起来”同步,但是您仍然需要知道如何使用异步才能正确使用它们……您不会浪费时间,只需停止寻找让异步任务同步工作的灵丹妙药——这是不可能的
  • 好的,请您指出正确的方向。我应该搜索的关键词或帮助链接。你会怎么做

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


【解决方案1】:

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

【讨论】:

    【解决方案2】:

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

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

    【讨论】:

      猜你喜欢
      • 2012-04-10
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-04-10
      • 1970-01-01
      • 1970-01-01
      • 2013-08-28
      • 1970-01-01
      相关资源
      最近更新 更多