1

我认为这与我对 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 , ...)

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

4

1 回答 1

1

您可以访问函数内部的响应callback

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

var gc_results = [];

然后,您将为从文件中获取的每个地址调用该函数,类似于以下内容:

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

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

于 2017-05-08T21:51:28.610 回答