【问题标题】:Does Google Maps API Track Distance By Different Modes of Travel using Mobile GPS?Google Maps API 是否使用移动 GPS 通过不同的旅行方式跟踪距离?
【发布时间】:2018-11-02 06:45:32
【问题描述】:

Google 地图时间线报告显示我们去过的位置以及我们在车辆中步行或行驶的距离。这是通过使用移动 GPS 进行跟踪的。我们能否通过 Google Maps API 获取这些详细信息 - 不同出行方式的总行驶距离?

【问题讨论】:

    标签: google-maps


    【解决方案1】:

    对于计算两个位置之间的距离,您可以在 Google Maps distance-matrix API 中指定 travel mode。

    默认情况下,为driving mode 计算距离。

    function initMap() {
            var bounds = new google.maps.LatLngBounds;
    
            var origin = {lat: -25.363, lng: 131.044};
            var destination = {lat: -25.34470, lng: 131.05121};
    
            var map = new google.maps.Map(document.getElementById('map'), {
              center: {lat: -25.363, lng: 131.044},
              zoom: 12
            });
    
            var service = new google.maps.DistanceMatrixService;
            service.getDistanceMatrix({
              origins: [origin],
              destinations: [destination],
              travelMode: 'DRIVING',
              unitSystem: google.maps.UnitSystem.METRIC,
              avoidHighways: false,
              avoidTolls: false
            }, function(response, status) {          
                var originList = response.originAddresses;
                var destinationList = response.destinationAddresses;
                var outputDiv = document.getElementById('output');
                outputDiv.innerHTML = '';
              }
            });
    

    }

    回复

    对Distance Matrix service 的成功调用会返回DistanceMatrixResponse

    样本

       {
          "originAddresses": [ "Greenwich, Greater London, UK"],
          "destinationAddresses": [ "Stockholm County, Sweden"],
          "rows": [{
            "elements": [{
              "status": "OK",
              "duration": {
                "value": 70778,
                "text": "19 hours 40 mins"
              },
              "distance": {
                "value": 1887508,
                "text": "1173 mi"
              }
            }]
          }]
        }
    

    参考:https://developers.google.com/maps/documentation/javascript/examples/distance-matrix https://developers.google.com/maps/documentation/javascript/distancematrix

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2018-08-17
      • 2016-01-22
      • 2012-06-04
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多