谷歌距离矩阵API无法找到特定的邮政编码。

问题描述 投票:0回答:1

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

有没有一种更安全的方式来进行这些请求 或者是我可能弄错了一些配置?

javascript zipcode google-distancematrix-api
1个回答
0
投票

找到了一个解决方法。 单纯的邮政编码被认为是模棱两可的地址,因此不太可靠。 在地址字符串中使用完整的城市、州和邮政编码解决了这个特定输入的问题,而且我在测试的其他地址中没有遇到任何问题。 在这里找到了这些API的最佳实践及其可靠性。 https:/developers.google.commapsdocumentationgeocodingbest-practices。

© www.soinside.com 2019 - 2024. All rights reserved.