gpt4 book ai didi

node.js - 使用node.js 谷歌地图客户端库

转载 作者:太空宇宙 更新时间:2023-11-04 00:23:02 24 4
gpt4 key购买 nike

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

希望我说得清楚,提前致谢乔纳森。

最佳答案

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

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

var gc_results = [];

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

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

此后,您将拥有包含所需信息的 gc_results 数组

关于node.js - 使用node.js 谷歌地图客户端库,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43857072/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com