使用node.js谷歌地图客户端库
我认为这与我有限的JavaScript经验有关。 我使用Google的node.js客户端库,在这里find – https://github.com/googlemaps/google-maps-services-js这个例子展示了如何创build一个客户端对象
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); } });
我需要做的是从文件中读取一个地址列表,并为它们build立一个对象数组。
我希望我的代码能够按照以下方式进行操作:
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返回的东西,如果是的话,我如何访问它? 我是否应该按照以下几点来设定一些variables:
var gc_results = googleMapsClient.geocode(param , ...)
希望我清楚,先谢谢乔纳森。
您可以访问callback
函数中的响应。
所以,如果你想要为每个地址调用这个函数,你可以先定义一个空数组:
var gc_results = [];
然后,你应该调用你从文件中得到的每个地址的函数,类似这样的:
addresses.forEach(function(){ googleMapsClient.geocode({ address: }, function(err, res) { gc_results.push(res.json.results); }) })
在此之后,您将有gc_results
arrays充满您需要的信息