首页 > 解决方案 > 将多个目的地作为数组传递给距离矩阵 api

问题描述

我正在调用具有多个目的地的距离矩阵 api 作为数组,但我只得到一个数组作为响应

destinationrequestsample---> ["0,0", "0,0", "0,0", "0,0", "0,0", "0,0", "0,0", "0,0", "0,0", "0,0", "0,0", "0,0", "0,0", "0,0", "0,0", "0,0", "0,0", "0,0"]
export async function getStoreDistance(locationDetails) {
  console.log('stordistance', locationDetails);
  let destinationRequest = locationDetails.destinations.map(location => {
    return `${location.lat},${location.long}`;
  });
  return await axios
    .get(
      `https://maps.googleapis.com/maps/api/distancematrix/json?units=imperial
      &origins=${locationDetails.origins.map(function(locationDetail) {
        return locationDetail;
      })}
      &destinations=${destinationRequest}&key=************j****c`,
    )
    .then(function(response) {
      // handle success
      return response;
    })
    .catch(function(error) {
      // handle error
      return error;
    });
}

标签: react-nativegoogle-distancematrix-api

解决方案


你必须把'|' 不同纬度和经度之间的这个符号。像这样的东西:

目的地 = 41.43206,-81.38992|-33.86748,151.20699

因此,在您的代码中,您可以借助 join 方法简单地实现这一点:

let destinationRequest = locationDetails.destinations.map(location => {
   return `${location.lat},${location.long}`;
}).join('|');

推荐阅读