Google Distance Matrix API找不到特定的邮政编码

时间:2020-06-04 04:45:43

标签: javascript zipcode google-distancematrix-api

我正在尝试获取JavaScript客户端中邮政编码之间的距离。除了一个特定的邮政编码:68434,Distance Matrix服务一直运行良好。当我尝试使用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");
    }
});

是否有更安全的方式发出这些请求,或者可能是我搞砸了一些配置?

1 个答案:

答案 0 :(得分:0)

找到了解决方法。单独的邮政编码被认为是模棱两可的地址,因此可靠性较低。在地址字符串中使用完整的城市,州和邮政编码可以解决此特定输入的问题,而我测试的任何其他地址都没有遇到任何问题。在以下位置找到了这些API及其可靠性的最佳实践:https://developers.google.com/maps/documentation/geocoding/best-practices