Как добавить расстояние как 200+? - PullRequest
0 голосов
/ 23 февраля 2020

Когда вы вводите из одного местоположения в другое, например, из Лахора в Исламабад, и общее расстояние показывает 368. То, что я хочу, это показывает, добавьте 200, а затем отобразите его как 568 в JavaScript. Код дается следующим образом:

<form action="" method="post">
  <div style="width:40%;margin: auto;">

    <div class="row" style="padding:10px;">
      <div class="col-md-3">
        <label for="email">Source:</label>
      </div>
      <div class="col-md-9">
        <div class="form-group">
          <input type="text" class="form-control" id="source" placeholder="Enter the Source">
        </div>
      </div>

      <div class="col-md-3">
        <label for="pwd">Destination:</label>
      </div>
      <div class="col-md-9">
        <div class="form-group">
          <input type="text" class="form-control" id="destination" placeholder="Enter the Destination">
        </div>
      </div>
    </div>
    <div class="row" style="padding:10px;">
      <div class="col-md-12">
        <button type="button" class="site-button" onclick="get_rout()">Get Distance</button>
      </div>
    </div>
    <div class="row" style="padding:10px;">
      <div class="col-md-3"><label for="pwd">Distance(km):</label></div>
      <div class="col-md-9">
        <div class="form-group"><input type="text" class="form-control distance"></div>
      </div>
    </div>
  </div>

  <div class="row">
    <div class='col-md-12' id='maplocation' style="height: 250px;"></div>
  </div>
  <br><br>
</form>

<script type="text/javascript">
  var source, destination;

  var darection = new google.maps.DirectionsRenderer;

  var directionsService = new google.maps.DirectionsService;

  google.maps.event.addDomListener(window, 'load', function() {

    new google.maps.places.SearchBox(document.getElementById('source'));

    new google.maps.places.SearchBox(document.getElementById('destination'));

  });

  function get_rout() {

    var mapOptions = {

      mapTypeControl: false,
      center: {
        lat: -100.8688,
        lng: 151.2195
      },
      zoom: 20
    };

    map = new google.maps.Map(document.getElementById('maplocation'), mapOptions);

    darection.setMap(map);
    darection.setPanel(document.getElementById('panallocation'));

    source = document.getElementById("source").value;
    destination = document.getElementById("destination").value;

    var request = {
      origin: source,
      destination: destination,
      travelMode: google.maps.TravelMode.DRIVING
    };
    directionsService.route(request, function(response, status) {
      if (status == google.maps.DirectionsStatus.OK) {
        darection.setDirections(response);
      }
    });

    var service = new google.maps.DistanceMatrixService();

    service.getDistanceMatrix({
      origins: [source],
      destinations: [destination],
      travelMode: google.maps.TravelMode.DRIVING,
      unitSystem: google.maps.UnitSystem.METRIC,
      avoidHighways: false,
      avoidTolls: false
    }, function(response, status) {

      if (status == google.maps.DistanceMatrixStatus.OK && response.rows[0].elements[0].status != "ZERO_RESULTS") {

        var distance = response.rows[0].elements[0].distance.text;

        var duration = response.rows[0].elements[0].duration.text;

        distancefinel = distance.split(" ");

        $('.distance').val(distancefinel[0]);



      } else {

        alert("Unable to find the distance between selected locations");
      }
    });
  }
</script>

1 Ответ

0 голосов
/ 23 февраля 2020

заменить

$('.distance').val(distancefinel[0]);

на

$('.distance').val(parseInt(distancefinel[0]) + 200);
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...