在距离计算中增加驾驶时间

时间:2018-02-23 13:51:28

标签: javascript google-maps google-direction google-distancematrix-api

我使用此代码作为起点,但它只计算两个地方之间的行车距离,而不是驾驶时间。

我还需要添加到js以获得驾驶时间?

我已经看过谷歌开发者指南了,但是还没有能够解决这个问题。

https://codepen.io/youfoundron/pen/GIlvp

JS:

$(function() {

 function calculateDistance(origin, destination) {
    var service = new google.maps.DistanceMatrixService();
    service.getDistanceMatrix(
    {
      origins: [origin],
      destinations: [destination],
      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) {
      $('#result').html(err);
    } else {
      var origin = response.originAddresses[0];
      var destination = response.destinationAddresses[0];
      if (response.rows[0].elements[0].status === "ZERO_RESULTS") {
        $('#result').html("Better get on a plane. There are no roads between " 
                          + origin + " and " + destination);
      } else {
        var distance = response.rows[0].elements[0].distance;
        var distance_value = distance.value;
        var distance_text = distance.text;
        var kilometer = distance_text.substring(0, distance_text.length - 3);
        $('#result').html("It is " + kilometer + " kilometer from " + origin + " to " + destination + " and it takes " + " to drive.");
      }
    }
  }

  $('#distance_form').submit(function(e){
      event.preventDefault();
      var origin = $('#origin').val();
      var destination = $('#destination').val();
      var distance_text = calculateDistance(origin, destination);
  });

});

2 个答案:

答案 0 :(得分:1)

您错过了duration.text中的DistanceMatrixResponse' Object,因此请更改您的else block

 else {
    var distance = response.rows[0].elements[0].distance;
    var distance_value = distance.value;
    var distance_text = distance.text;
    var kilometer = distance_text.substring(0, distance_text.length - 3);
    $('#result').html("It is " + kilometer + " kilometer from " + origin + " to " + destination + " and it takes " + " to drive.");
  }

致:

 else {
    var distance = response.rows[0].elements[0].distance;
    var distance_value = distance.value;
    var distance_text = distance.text;

    // Add a variable here to store the duration
    var duration_time = duration.text; 

    var kilometer = distance_text.substring(0, distance_text.length - 3);
    $('#result').html("It is " + kilometer + " kilometer from " + origin + " to " + destination + " and it takes " + duration_time " to drive.");
  }

duration.text必须添加到ResponseObject中: result[0].duration.text

答案 1 :(得分:1)

the documentation您获得距离的响应元素还包括持续时间:

  

持续时间
  类型:持续时间
  此始发地 - 目的地配对的持续时间。此属性可能未定义,因为持续时间可能未知。

该属性具有以下属性:

  

属性
  文字
  类型:字符串
  持续时间值的字符串表示   价值
  类型:编号
  持续时间以秒为单位。

将您喜欢的版本添加到回调函数中(可能需要文本):

  function callback(response, status) {
    if (status != google.maps.DistanceMatrixStatus.OK) {
      $('#result').html(err);
    } else {
      var origin = response.originAddresses[0];
      var destination = response.destinationAddresses[0];
      if (response.rows[0].elements[0].status === "ZERO_RESULTS") {
        $('#result').html("Better get on a plane. There are no roads between " 
                          + origin + " and " + destination);
      } else {
        var distance = response.rows[0].elements[0].distance;
        var duration = response.rows[0].elements[0].duration;
        var distance_value = distance.value;
        var distance_text = distance.text;
        var duration_value = duration.value;
        var duration_text = duration.text;
        var kilometer = distance_text.substring(0, distance_text.length - 3);
        $('#result').html("It is " + kilometer + " kilometer from " + origin + " to " + destination + " and it takes " + duration_text + " to drive.");
      }
    }
  }

proof of concept fiddle