【问题标题】:google maps directionsService giving wrong results谷歌地图方向服务给出错误的结果
【发布时间】:2023-03-04 13:37:01
【问题描述】:

我正在尝试使用谷歌的方向服务来获取以英里为单位的距离和估计时间。它有点工作,但我知道它给我的结果是不正确的。距离和时间太短?我需要驾驶模式下的结果。代码示例是:

HTML

<input id="s_lat" value="52.441334" />
<input id="s_lng" value="-1.654737" />
<input id="d_lat" value="52.450439" />
<input id="d_lng" value="-1.729660" />

JS

var n_start = s_lat + ',' + s_lng;
var n_end = d_lat + ',' + d_lng;

function getdistance() {
    var directionsService = new google.maps.DirectionsService();

    var request = {
        origin      : n_start,
        destination : n_end,
        travelMode  : google.maps.TravelMode.DRIVING,
        unitSystem: google.maps.UnitSystem.METRIC,
        durationInTraffic: true
    };

    directionsService.route(request, function(response, status) {
        if ( status == google.maps.DirectionsStatus.OK ) {

            alert (response.routes[0].legs[0].duration.value);
            alert (response.routes[0].legs[0].distance.value);
        }
        else {
            // oops, there's no route between these two locations

        }
    });

}

【问题讨论】:

    标签: google-maps-api-3 map-directions


    【解决方案1】:

    我通过发布的代码得到的结果是NOT_FOUND,我没有得到距离。逗号分隔的字符串不是google.maps.LatLnggoogle.maps.LatLngLiteral,它被视为地址并在返回结果之前进行地理编码。

    这个:

    var n_start = s_lat + ',' + s_lng;
    var n_end = d_lat + ',' + d_lng;
    

    应该是(google.maps.LatLngLiteral):

    var n_start = {lat: parseFloat(s_lat.value), lng: parseFloat(s_lng.value)}; 
    var n_end = {lat: parseFloat(d_lat.value), lng: parseFloat(d_lng.value)};
    

    或者(google.maps.LatLng):

    var n_start = new google.maps.LatLng(s_lat.value,s_lng.value);
    var n_end = new google.maps.LatLng(d_lat.value,d_lng.value);
    

    Proof of concept fiddle

    代码 sn-p:

    function initMap() {
      var n_start = new google.maps.LatLng(s_lat.value,s_lng.value);
      var n_end = new google.maps.LatLng(d_lat.value,d_lng.value);
     
    
      function getdistance() {
        var directionsService = new google.maps.DirectionsService();
    
        var request = {
          origin: n_start,
          destination: n_end,
          travelMode: google.maps.TravelMode.DRIVING,
          unitSystem: google.maps.UnitSystem.METRIC,
          durationInTraffic: true
        };
        console.log(JSON.stringify(request));
        directionsService.route(request, function(response, status) {
          if (status == google.maps.DirectionsStatus.OK) {
    
            console.log("duration=" + response.routes[0].legs[0].duration.value + " seconds");
            console.log("distance=" + response.routes[0].legs[0].distance.value + " meters");
            document.getElementById('result').innerHTML = "distance=" + (response.routes[0].legs[0].distance.value / 1000).toFixed(2) + " km<br>duration=" + (response.routes[0].legs[0].duration.value / 60).toFixed(2) + " minutes";
            new google.maps.DirectionsRenderer({
              map: new google.maps.Map(document.getElementById('map')),
              directions: response
            })
          } else {
            window.alert('Directions request failed due to ' + status);
    
          }
        });
    
      }
      getdistance();
    }
    #map {
      height: 100%;
    }
    
    html,
    body {
      height: 100%;
      margin: 0;
      padding: 0;
    }
    <input id="s_lat" value="52.441334" />
    <input id="s_lng" value="-1.654737" />
    <input id="d_lat" value="52.450439" />
    <input id="d_lng" value="-1.729660" /><br>
    <div id="result"></div>
    <div id="map"></div>
    <!-- Replace the value of the key parameter with your own API key. -->
    <script async defer src="https://maps.googleapis.com/maps/api/js?key=AIzaSyCkUOdZ5y7hMm0yrcCQoCvLwzdM6M8s5qk&callback=initMap"></script>

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2020-12-03
      • 1970-01-01
      • 2015-08-22
      • 1970-01-01
      • 1970-01-01
      • 2017-02-23
      • 2016-09-18
      • 2015-10-08
      相关资源
      最近更新 更多