比较高速公路,避开公路距离

时间:2014-06-02 13:27:43

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

我想比较使用高速公路和避开高速公路时的距离,但我不知道该怎么做。我的代码是计算两点之间的距离和基于距离的价格。如果选中高速公路复选框,则应计算不同的价格,但如果没有高速公路,则价格应保持不变。

这是代码

var directionsDisplay = new google.maps.DirectionsRenderer();
var directionsService = new google.maps.DirectionsService();
var map;

function initialize() {

  var myOptions = {
    center: new google.maps.LatLng(43.521573, 16.441015),
    zoom: 5,
    mapTypeID: google.maps.MapTypeId.ROADMAP
  };

  map = new google.maps.Map(document.getElementById('map-canvas'), myOptions);

  directionsDisplay.setMap(map);

  google.maps.event.trigger(map, 'resize');

}

function calcRoute() {
  var start = document.getElementById('start').value;
  var end = document.getElementById('end').value;
  var vehicle = document.getElementById('vehicle').value;

  //var highway = document.getElementById('highway').checked;
  //var round = document.getElementById('return').checked;

  var distanceInput = document.getElementById('distance');
  var timeInput = document.getElementById('time');
  var priceInput = document.getElementById('price');

  var request, price;

  if (document.getElementById('highway').checked) {
    request = {
      origin: start,
      destination: end,
      travelMode: google.maps.TravelMode.DRIVING,
      avoidHighways: false
    };

  } else {
     request = {
      origin: start,
      destination: end,
      travelMode: google.maps.TravelMode.DRIVING,
      avoidHighways: true
    };

  }

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

      directionsDisplay.setDirections(response);

      if (document.getElementById('highway').checked)
        dist_highway = response.routes[0].legs[0].distance.value / 1000;
      else
        dist = response.routes[0].legs[0].distance.value / 1000;

      distanceInput.value = response.routes[0].legs[0].distance.value / 1000;

      var hours = parseInt( response.routes[0].legs[0].duration.value / 3600) % 24;
      var minutes = parseInt( response.routes[0].legs[0].duration.value / 60) % 60;
      timeInput.value = hours + "h " + minutes + "m";


      if (vehicle == 'van') {
        if ((response.routes[0].legs[0].distance.value / 1000) < '30' ) {
          price = (response.routes[0].legs[0].distance.value / 1000) * 1.19;
        } else {
          price = (response.routes[0].legs[0].distance.value / 1000) * 0.93;
        }
      } else if (vehicle == 'personal') {
        if ((response.routes[0].legs[0].distance.value / 1000) < '30' ) {
          price = (response.routes[0].legs[0].distance.value / 1000) * 1.32;
        } else {
          price = (response.routes[0].legs[0].distance.value / 1000) * 1.06;
        }
      }

      if (document.getElementById('highway').checked && vehicle == 'personal' ) {
        price *= 1.3;
      } else if (document.getElementById('highway').checked && vehicle == 'van' ) {
        price *= 1.5;
      };

      if (document.getElementById('return').checked) {
        price *= 1.7;

      };

      priceInput.value = price.toFixed(2) + "€";

    }
  });
}


google.maps.event.addDomListener(window, 'load', initialize);

在AlexB的解释之后,这是我的功能。问题解决了。

function calcDist(start, end) {
  var requestHighway, request, distHighway, dist;
  var dirService = new google.maps.DirectionsService();

  requestHighway = {
      origin: start,
      destination: end,
      travelMode: google.maps.TravelMode.DRIVING,
      avoidHighways: false
    };

  request = {
      origin: start,
      destination: end,
      travelMode: google.maps.TravelMode.DRIVING,
      avoidHighways: true
    };

  dirService.route(requestHighway, function(response, status) {

    if (status == google.maps.DirectionsStatus.OK) {

      distHighway = response.routes[0].legs[0].distance.value / 1000;
      distHighway = distHighway.toFixed(2);

    }
  });

  dirService.route(request, function(response, status) {

    if (status == google.maps.DirectionsStatus.OK) {

      dist = response.routes[0].legs[0].distance.value / 1000;
      dist = distHighway.toFixed(2);

    }
  });

  if (distHighway - dist == 0) {
    return true;
  } else {
    return false;
  }

}

1 个答案:

答案 0 :(得分:1)

在AlexB的解释之后,这是我的功能。问题解决了。

function calcDist(start, end) {
  var requestHighway, request, distHighway, dist;
  var dirService = new google.maps.DirectionsService();

  requestHighway = {
      origin: start,
      destination: end,
      travelMode: google.maps.TravelMode.DRIVING,
      avoidHighways: false
    };

  request = {
      origin: start,
      destination: end,
      travelMode: google.maps.TravelMode.DRIVING,
      avoidHighways: true
    };

  dirService.route(requestHighway, function(response, status) {

    if (status == google.maps.DirectionsStatus.OK) {

      distHighway = response.routes[0].legs[0].distance.value / 1000;
      distHighway = distHighway.toFixed(2);

    }
  });

  dirService.route(request, function(response, status) {

    if (status == google.maps.DirectionsStatus.OK) {

      dist = response.routes[0].legs[0].distance.value / 1000;
      dist = distHighway.toFixed(2);

    }
  });

  if (distHighway - dist == 0) {
    return true;
  } else {
    return false;
  }

}