Google 距离矩阵 API 找不到特定的邮政编码



我正在尝试在JavaScript中获取客户端邮政编码之间的距离。 距离矩阵服务一直运行良好,除了一个特定的邮政编码:68434。 当我尝试使用 ajax 请求执行请求时,它可以工作,但在带有 CORS 的浏览器上受到限制。 我也仅限于客户端调用,因此无需尝试服务器端调用。

法典:

var service = new google.maps.DistanceMatrixService();
service.getDistanceMatrix(
// Build request for driving distance from zip code 1 to zip code 2
{
origins: ["87108"],
destinations: ["68434"],
travelMode: 'DRIVING'
}, 
// Parse the response message to get the distance
function(response, status) {
console.log(response);
//Print any errors to the screen
if (status !== 'OK') {
console.log('Error was: '+status);
}
else {
if (response.rows[0].elements[0].status === "OK") {
console.log("Success");
}
else
console.log("Fail");
}
});

有没有更安全的方式来提出这些请求或我可能搞砸的某些配置?

找到解决方法。 邮政编码本身被视为不明确的地址,因此不太可靠。 在地址字符串中使用完整的城市、州和邮政编码解决了此特定输入的问题,并且我没有遇到任何其他测试地址的问题。 在此处查找这些 API 及其可靠性的最佳实践:https://developers.google.com/maps/documentation/geocoding/best-practices

最新更新