Google Maps API是否可以使用移动GPS通过不同的旅行方式跟踪距离?

时间:2018-11-02 06:45:32

标签: google-maps

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

1 个答案:

答案 0 :(得分:0)

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

默认情况下,为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