gpt4 book ai didi

javascript - 返回了 Google 距离矩阵 ZERO_RESULTS

转载 作者:塔克拉玛干 更新时间:2023-11-02 22:46:29 25 4
gpt4 key购买 nike

前言

我使用 Google Maps API 已经有一段时间了,之前能够成功使用距离矩阵服务 here .然后我决定进行一些重构,结果出现了问题......

当前问题

当尝试获取距离矩阵服务的结果时, 对象包含一个空的(状态:ZERO_RESULTS)对象数组。

下面运行时的空对象:

the empty object during runtime

通常如下所示:

normal object during runtime

代码

coords 数组被传递到 distanceMatrixCoords() 函数中。它们看起来像这样:

the coords array

而其他变量,例如 var origins = pos; 则使用坐标字符串正确填充。

包含此代码的整个文件是 available here

function distanceMatrixCoords(coords) {
var origins = pos;
var destinations = coords;
var service = new google.maps.DistanceMatrixService();

service.getDistanceMatrix({
origins: [origins],
destinations: destinations,
travelMode: google.maps.TravelMode.DRIVING,
unitSystem: google.maps.UnitSystem.IMPERIAL,
avoidHighways: false,
avoidTolls: false
},
callback
);

function callback(response, status) {
if (status == google.maps.DistanceMatrixStatus.OK) {
var origins = response.originAddresses;
var destinations = response.destinationAddresses;
var distanceElement = [];
var theRow = null;

for (var i = 0; i < origins.length; i++) {

var results = response.rows[i].elements;

for (var j = 0; j < results.length; j++) {
var element = results[j];
var distance = element.distance.text;
var duration = element.duration.text;
var from = origins[i];
var to = destinations[j];

distanceElement[j] = document.createElement('p');
distanceElement[j].innerHTML = results[j].distance.text;
distanceElement[j].className = 'distance-cell';
}
}
}
}

}

最终,根据上面的代码,行 var results = response.rows[i].elements; 是代码中断的地方。 response.rows 是数组中具有多个 ZERO_RESULTS 的对象。与我以前的代码(上面链接)相比,distanceMatrixCoords() 函数接收与以前完全相同类型的数据。

传递到 callback() 函数的 response 对象包含以下值: response object

destinationAddressesoriginAddresses 已正确填充看起来像这个逗号分隔值 "41.0259285,81.5899887" 的坐标。 rows 数组再次成为问题所在。

我查看了许多其他来源以寻求答案,但 Google Maps API 的距离矩阵服务似乎没有太多可用资源。我看到了this question , 但尚未记录任何答案。

最佳答案

问题是我为获取数据而构建的服务错误地返回了我的坐标。例如,坐标是 41.0259285,81.5899887,而它们应该是 41.0259285,-81.5899887。经度前面的减号丢失,因此坐标无效,距离矩阵服务不知道如何处理它们。

关于javascript - 返回了 Google 距离矩阵 ZERO_RESULTS,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26453387/

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