【问题标题】:Google Distance Matrix API can't find a specific zip codeGoogle 距离矩阵 API 找不到特定的邮政编码
【发布时间】:2020-06-04 04:45:43
【问题描述】:

我正在尝试在 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");
    }
});

是否有更安全的方式来发出这些请求或我可能会搞砸的某些配置?

【问题讨论】:

    标签: javascript zipcode google-distancematrix-api


    【解决方案1】:

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

    【讨论】:

      猜你喜欢
      • 2023-03-06
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-11-30
      • 1970-01-01
      • 2011-04-01
      相关资源
      最近更新 更多