如果... else语句打印距离

时间:2018-03-07 17:33:02

标签: javascript jquery google-maps if-statement

根据距离结果有多大,有没有人知道如何让这个伟大的fiddle读取不同的东西?

如果距离大于10公里,20公里,30公里等,我希望打印出一个额外的句子来说明不同的事情。

我似乎无法弄清楚if ... else在现有代码中的位置。

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>

1 个答案:

答案 0 :(得分:2)

我相信如果你在html里面添加一个元素,比如

<label>distance:</label>
  <input id="distance" name="km">
  <br>
  <span id="custommessage"></span>

然后,你可以添加js

$('#distance').val(distance);
$("#result").text(getCustomMessage(distance))

你可以添加你需要的所有if或switch的功能

function getCustomMessage(distance) {
    if (distance > 200) {
      return 'thats really far'
    }
   return `it's ${distance} miles away`
}
希望它有所帮助!祝你好运:)