javascript - Google map 距离矩阵 API 舍入 JS API v3

标签 javascript google-maps google-maps-api-3 distance

我遇到了如标题中提到的问题。请先查看底部的图像。 130 METERS

在这张图片中,您可以轻松看到它是 130 米,但如果我使用距离矩阵 API,它会显示 0.1 公里,

180 METERS

这里是 180 米,但在距离矩阵 API 中显示 0.2 公里。都是谷歌怎么会有不同..

这是我获取距离的代码:

 var service = new google.maps.DistanceMatrixService();
    service.getDistanceMatrix(
      {
          origins: startArray, //LatLng Array
          destinations: finishArray, //LatLng Array
          travelMode: google.maps.TravelMode.DRIVING,
          unitSystem: google.maps.UnitSystem.METRIC,
          avoidHighways: false,
          avoidTolls: false
      }, callback);

function callback(response, status) {
    if (status != google.maps.DistanceMatrixStatus.OK) {
        alert('Error was: ' + status);
    } else {
        var origins = response.originAddresses;
        var destinations = response.destinationAddresses;

        for (var i = 0; i < origins.length; i++) {
            var results = response.rows[i].elements;
                console.log(results[i].distance.text);
                document.getElementById('ctl00_MainContent_hidSpeed').value += results[i].distance.text+"&";
        }
}
}

任何帮助将不胜感激

最佳答案

只需使用results[i].distance.value 而不是文本

参见https://developers.google.com/maps/documentation/javascript/distancematrix#distance_matrix_responses例如,在文本中,它是四舍五入的,但该值以米为单位准确显示距离。

关于javascript - Google map 距离矩阵 API 舍入 JS API v3,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17294013/

相关文章:

javascript - 某些 Google map 功能在 Blogger 中使用时未显示

html - iframe 在 XHTML Strict 中无效

google-maps - 使用标记链接到卫星 map 并尊重缩放的 Google map URL

javascript - 缩小时如何隐藏标记?

google-maps - 在没有谷歌地图的情况下使用纬度/经度信息自动生成屏幕截图?

javascript - 使用 underscore.js 在 javascript 中分组/计数

javascript - jqGrid : Need to make a field nonEditable in Edit Window and editable in Add Window

javascript - 嵌套在 while 循环中的 for 循环的时间复杂度是多少?

javascript - Jquery 标签删除导致换行?

javascript - 谷歌驱动器中包含的 KML 文件无法识别