mercredi 7 mars 2018

If...else statement to print different results from distances

Does anyone know how I could make the result of this great fiddle read different things according to how big the distance result is?

I would like it to be printed an extra sentence saying different things according to if the distance is bigger than 10 km, 20 km, 30 km, etc.

I can't seem to figure out where to place the if...else in the existing code.

The JS:

var geocoder;
var map;
var polyline;
var markers = [];

function initialize() {
  map = new google.maps.Map(
    document.getElementById("map_canvas"), {
      center: new google.maps.LatLng(37.4419, -122.1419),
      zoom: 13,
      mapTypeId: google.maps.MapTypeId.ROADMAP
    });
  calculateDistance();
  google.maps.event.addDomListener(document.getElementById('btn'), 'click', calculateDistance)
}

function calculateDistance() {
  if (polyline && polyline.setMap) {
    polyline.setMap(null);
  }
  for (var i = 0; i < markers.length; i++) {
    markers[i].setMap(null);
  }
  markers = [];
  var bounds = new google.maps.LatLngBounds();
  var path = [];
  var startPoint = $('#main_from_route_input').val();
  var endPoint = $('#main_to_route_input').val();
  var geocoderStart = new google.maps.Geocoder();
  var geocoderEnd = new google.maps.Geocoder();
  var coordsStart, coordsEnd;

  geocoderStart.geocode({
    'address': startPoint
  }, function(response, status) {
    if (status != google.maps.GeocoderStatus.OK) {
      alert('Geocode of first address failed: ' + status);
    }
    coordsStart = response[0].geometry.location;
    bounds.extend(coordsStart);
    var startMark = new google.maps.Marker({
      position: coordsStart,
      map: map,
      title: "start"
    });
    markers.push(startMark);
    path.push(coordsStart);
    geocoderEnd.geocode({
      'address': endPoint
    }, function(response, status) {
      if (status != google.maps.GeocoderStatus.OK) {
        alert('Geocode of second address failed: ' + status);
      }
      coordsEnd = response[0].geometry.location;
      bounds.extend(coordsEnd);
      var endMark = new google.maps.Marker({
        position: coordsEnd,
        map: map,
        title: "end"
      });
      markers.push(endMark);
      path.push(coordsEnd);
      polyline = new google.maps.Polyline({
        path: path,
        map: map
      });
      var distance = (google.maps.geometry.spherical.computeDistanceBetween(coordsStart, coordsEnd) / 1000).toFixed(2);
      $('#distance').val(distance);
      map.fitBounds(bounds);
    });
  });
}

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

HTML:

<form>
  <label>distance:</label>
  <input id="distance" name="km">
  <br>
  <label>from</label>
  <input id="main_from_route_input" value="Copenhagen, Denmark" />
  <br>
  <label>to</label>
  <input id="main_to_route_input" value="Berlin, Germany" />
  <input id="btn" type="button" value="calculate distance" />
</form>
<div id="map_canvas" style="width:750px; height:450px; border: 2px solid #3872ac;"></div>

Aucun commentaire:

Enregistrer un commentaire