【问题标题】:Using the node.js google maps client library使用 node.js 谷歌地图客户端库
【发布时间】:2017-05-08 20:51:23
【问题描述】:

我认为这与我对 javascript 的有限经验有关。 我正在使用 Google 提供的 node.js 客户端库,可在此处找到 - https://github.com/googlemaps/google-maps-services-js 该示例显示了如何创建客户端对象

var googleMapsClient = require('@google/maps').createClient({
  key: 'your API key here'
});  

然后如何运行地理编码请求并打印出结果:

// Geocode an address.
googleMapsClient.geocode({
  address: '1600 Amphitheatre Parkway, Mountain View, CA'
}, function(err, response) {
  if (!err) {
    console.log(response.json.results);
  }
}); 

我需要做的是从文件中读取地址列表并为所有地址构建一个对象数组。

我希望我的代码按照以下方式做一些事情:

create address-objects array 
create a client object 
open the text files 
for each line in text files
 geocode the line(address)
 add the address and the results into an object and add it to the  address-objects

我不明白 googleMapsClient.geocode 是否返回某些内容,如果是,我该如何访问它?我应该将它设置为一些变量吗:

var gc_results = googleMapsClient.geocode(param , ...)

希望我是清楚的,在此先感谢 乔纳森。

【问题讨论】:

    标签: node.js google-maps client-library


    【解决方案1】:

    您可以在callback 函数中访问响应。

    所以,如果你想让每个地址都调用这个函数,你可以先定义一个空数组:

    var gc_results = [];
    

    然后你应该为你从文件中得到的每个地址调用函数,类似于这样:

    addresses.forEach(function(){
       googleMapsClient.geocode({
          address: 
        }, function(err, res) {
          gc_results.push(res.json.results);
      })
    })
    

    在此之后,您将拥有完整的 gc_results 数组,其中包含您需要的信息

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2018-03-26
      • 1970-01-01
      • 2012-06-08
      • 1970-01-01
      • 2016-03-21
      • 1970-01-01
      • 1970-01-01
      • 2018-07-15
      相关资源
      最近更新 更多