如何在 NodeJS 循环内处理多个 Google Places API 请求?

基莫

我有一个循环来分析一长串 GPS 点,然后我根据需要选择一些点。
对于每个 GPS 点,我想找到周围的地方。

如何确保将每个响应与其他响应分开?

这是循环内的代码,当我有 1 个 GPS 点时它可以工作,但更多的不是:

循环 GPS 路径,保存在哈希表中:

for (let indexI = 0; indexI < path_hash.length; indexI++) {
    for (let indexJ = 0; indexJ < path_hash[indexI].length - 2; indexJ++) {

...
准备 URL 请求:

location = path_hash[indexI][indexJ].data.coords.latitude + "," + path_hash[indexI][indexJ].data.coords.longitude;
var url = "https://maps.googleapis.com/maps/api/place/nearbysearch/json?" + "key=" + key + "&location=" + location + "&radius=" + radius + "&sensor=" + sensor + "&types=" + types + "&keyword=" + keyword;

...
执行请求:

https.get(url, function (response) {
    var body = '';
    response.on('data', function (chunk) {
        body += chunk;
    });

    response.on('end', function () {
        var places = places + JSON.parse(body);
        var locations = places.results;
        console.log(locations);
    });

}).on('error', function (e) {
    console.log("Got error: " + e.message);
})  
狒狒_

使用您的功能,您可以这样做

// Turn the callback function into a Promise
const fetchUrl = (url) => {
  return new Promise((resolve, reject) => {
    https.get(url, function (response) {
      var body = '';
      response.on('data', function (chunk) {
        body += chunk;
      });

      response.on('end', function () {
        var places = places + JSON.parse(body);
        var locations = places.results;
        resolve(locations) // locations is returned by the Promise
      });

    }).on('error', function (e) {
      console.log("Got error: " + e.message);
      reject(e); // Something went wrong, reject the Promise
    });
  });
}

// Loop the GPS path, saved in hash table
...

// Prepare the urls
...

const GPSPoints = [
  'url1',
  'url2',
  ...
];

// Fetch the locations for all the GPS points
const promises = GPSPoints.map(point => fetchUrl(point));

// Execute the then section when all the Promises have resolved
// which is when all the locations have been retrieved from google API
Promise.all(promises).then(all_locations => {
  console.log(all_locations[0]); // Contains locations for url1
  console.log(all_locations[1]); // Contains locations for url2
  ...
});

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章

如何使用Google Places API处理有位置和无位置的搜索请求?

如何使用CORS实施JavaScript Google Places API请求

Google Places API查询请求被拒绝的IOS

如何在NodeJs中为Google索引批处理请求发送多部分/混合请求?

如何在不重复请求的情况下使用Google Places API获取某种类型的Lat Longs列表?

如何查询Google Places API?

NodeJS请求多个api端点

Google Places API获取详细信息-无效请求

使用 Google Places API 删除 GPS 请求周围的额外字符

如何在iOS Swift中从Google Places API获取坐标

如何在Google Places API中使用SKU:Basic?

如何在React Native中加载Google Places API?

Google Places API-每天的请求限制是针对唯一请求,还是可以连续多次请求?

NativeScript Google-Places API --> 如何使用?

如何计算Google Maps Places API的配额?

Google Places API,如何获取评论?

如何从Google Places API获取地点说明

多个Google Places请求功能(药房定位器)

如何在nodejs中结合API GET请求?

如何在哪里获取photo_reference值以发出Google Places Photos请求?

Google时区API:请求循环

如何在Google Api Java客户端(驱动器)中执行批处理请求

如何在Google Apps脚本中使用UrlFetchApp发出Drive API批处理请求

如何在Google Speech-to-Text API中进行多个streamingRecognize请求?

Google Places APi照片

Google Places API替代

Google Places 活动 API

违反Google Places API

Python,Google Places API