2017-05-08 42 views
1

我认为这与我有限的JavaScript经验有关。 我使用由谷歌Node.js的客户端库,在这里找到 - https://github.com/googlemaps/google-maps-services-js 的例子展示了如何创建一个客户对象使用node.js谷歌地图客户端库

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

再怎么跑gecode请求,并打印出结果:

// 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 , ...) 

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

回答

1

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

所以,如果你想拥有这个功能被称为为每个地址,你可以先定义一个空数组:

var gc_results = []; 

然后你要调用的函数为每个地址你出来的文件,类似这样的东西:

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

在此之后,您将有gc_results阵列满您需要的信息

相关问题