react-native - 将多个目的地作为数组传递给距离矩阵 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;
});
}
解决方案
你必须把'|' 不同纬度和经度之间的这个符号。像这样的东西:
目的地 = 41.43206,-81.38992|-33.86748,151.20699
因此,在您的代码中,您可以借助 join 方法简单地实现这一点:
let destinationRequest = locationDetails.destinations.map(location => {
return `${location.lat},${location.long}`;
}).join('|');
推荐阅读
- c++ - 为什么派生类不能在其初始化列表中调用祖父构造函数?
- php - 我可以在使用 PHP 的 for 循环中使用什么样的条件?
- instagram-graph-api - 我应该在业务发现 API 上使用什么 ID?
- sql - 在没有循环的 SQL 中创建年/季度表
- windows - 组织议程“参数数量错误”错误
- elasticsearch - 如何查询 ElasticSearch 以查找至少有一个术语不在数组中的对象
- java - 按属性值动态过滤 ObservableList
- c# - Aurelia CLI 新后 VS2017 大纲缩进关闭
- kotlin - 无法使用 kotlin 频道接收少量消息
- javascript - 在 React 中使用 getFieldDecorator 嵌套 Antdesign 表单