-1

我正在使用 Google API,我需要做的是以英里为单位获得两个不同的距离。目前我有以下内容: -

var start = arrObjLatLngs[0];
var end = arrObjLatLngs[arrObjLatLngs.length - 1];

var base = new google.maps.LatLng(52.781048888889, -1.2110222222222546);

var service = new google.maps.DistanceMatrixService();
service.getDistanceMatrix(
  {
    origins: [base, base],
    destinations: [start, end],
    travelMode: google.maps.TravelMode.DRIVING,
    avoidHighways: false,
    avoidTolls: false
  }, callback);


function callback(response, status) {
  if (status == google.maps.DistanceMatrixStatus.OK) {
    var origins = response.originAddresses;
    var destinations = response.destinationAddresses;

    for (var i = 0; i < origins.length; i++) {
      var results = response.rows[i].elements;
      for (var j = 0; j < results.length; j++) {
        var element = results[j];
        var distance = element.distance.text;
        var duration = element.duration.text;
        var from = origins[i];
        var to = destinations[j];
      }
    }
  }
}

我需要的是两个变量;$runinPickup其中$runinDestination包含 1.) base 和 start(将是 $runinPickup)和 2.) base 和 end(将是 $runinDestination)之间的距离

如果我发出警报distance,我会得到回应:-

13.9公里

5.2公里

13.9公里

5.2公里

base首先,我只是想知道我如何才能获得与start$runinPickup之间的距离。其次,有没有办法我可以做到这一点,所以它不包括小数?因此,第一个值不是 13.9 公里,而是 13。

然后我需要把它转换成英里,我猜一旦我能得到这个值,我会这样做:

var pickup_distance = parseInt(($fieldPickup / 8) * 5); ?

任何帮助将非常感激!:)

4

1 回答 1

0

你读过文档吗?在 origins 数组中两次传递相同的地址:

   origins: [base, base],
   destinations: [start, end],

会给你同样的结果两次。这应该有效(未经测试),并为您提供 2 个结果:

   origins: [base],
   destinations: [start, end],

为了获得里程,我建议将 unitSystem 设置为IMPERIAL(尽管我在文档中找不到这意味着什么的定义):

unitSystem (optional) — The unit system to use when displaying distance. Accepted values are:

google.maps.UnitSystem.METRIC (default)
google.maps.UnitSystem.IMPERIAL

您当然可以将 METRIC 单位转换为英里

显示两个值的示例,距离以 IMPERIAL 单位表示。从文档中的示例修改。

代码片段:

var map;
var geocoder;
var bounds = new google.maps.LatLngBounds();
var markersArray = [];

var base = new google.maps.LatLng(55.930385, -3.118425);
var start = 'Greenwich, England';
var destinationA = 'Stockholm, Sweden';
var end = new google.maps.LatLng(50.087692, 14.421150);

var destinationIcon = 'https://chart.googleapis.com/chart?chst=d_map_pin_letter&chld=D|FF0000|000000';
var originIcon = 'https://chart.googleapis.com/chart?chst=d_map_pin_letter&chld=O|FFFF00|000000';

function initialize() {
  var opts = {
    center: new google.maps.LatLng(55.53, 9.4),
    zoom: 10,
    mapTypeId: google.maps.MapTypeId.ROADMAP
  };
  map = new google.maps.Map(document.getElementById('map'), opts);
  geocoder = new google.maps.Geocoder();
  calculateDistances();
}

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

function calculateDistances() {
  var service = new google.maps.DistanceMatrixService();
  service.getDistanceMatrix({
    origins: [base],
    destinations: [start, end],
    travelMode: google.maps.TravelMode.DRIVING,
    unitSystem: google.maps.UnitSystem.IMPERIAL,
    avoidHighways: false,
    avoidTolls: false
  }, callback);
}

function callback(response, status) {
  if (status != google.maps.DistanceMatrixStatus.OK) {
    alert('Error was: ' + status);
  } else {
    var origins = response.originAddresses;
    var destinations = response.destinationAddresses;
    var outputDiv = document.getElementById('outputDiv');
    outputDiv.innerHTML = '<table border="1">';
    deleteOverlays();
    var stringArray = ['$runinPickup', '$runinDestination'];
    var htmlString = '<table border="1">';
    for (var i = 0; i < origins.length; i++) {
      var results = response.rows[i].elements;
      addMarker(origins[i], false);
      for (var j = 0; j < results.length; j++) {
        addMarker(destinations[j], true);
        htmlString += '<tr><td>' + stringArray[j] + '</td><td>' + results[j].distance.text + '</td></tr>';
        outputDiv.innerHTML += '<tr><td>' + stringArray[j] + '</td><td>' + results[j].distance.text + '</td></tr>';
      }
    }
    htmlString += '</table>';
    // outputDiv.innerHTML += '</table>';
    outputDiv.innerHTML = htmlString;
    // alert(htmlString);
  }
}

function addMarker(location, isDestination) {
  var icon;
  if (isDestination) {
    icon = destinationIcon;
  } else {
    icon = originIcon;
  }
  geocoder.geocode({
    'address': location
  }, function(results, status) {
    if (status == google.maps.GeocoderStatus.OK) {
      bounds.extend(results[0].geometry.location);
      map.fitBounds(bounds);
      var marker = new google.maps.Marker({
        map: map,
        position: results[0].geometry.location,
        icon: icon
      });
      markersArray.push(marker);
    } else {
      alert('Geocode was not successful for the following reason: ' + status);
    }
  });
}

function deleteOverlays() {
  if (markersArray) {
    for (i in markersArray) {
      markersArray[i].setMap(null);
    }
    markersArray.length = 0;
  }
}
body {
  margin: 20px;
  font-family: courier, sans-serif;
  font-size: 12px;
}
#map {
  height: 480px;
  width: 640px;
  border: solid thin #333;
  margin-top: 20px;
}
<script src="https://maps.googleapis.com/maps/api/js"></script>
<div id="outputDiv"></div>
<div id="map"></div>

于 2013-03-14T16:07:38.857 回答