0

我是编程新手..我有这个代码,它给出了两点之间的距离,但需要进一步乘以一个整数,比如 10.. 我正在研究的项目是计算两点之间的距离并将其乘以票价/公里就像 10 卢比/公里(印度卢比)一样。因此,如果距离为 30 公里,票价将为 30*10 = Rs.300

提前致谢

以下是代码

<script>
var map;
var geocoder;
var bounds = new google.maps.LatLngBounds();
var markersArray = [];
  var origin1 = '';
  var destinationA = '';

  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);


var fromText = document.getElementById('FAdd');

var options = {
            componentRestrictions: {country: 'in'}
    };var fromAuto = new google.maps.places.Autocomplete(fromText, options);


fromAuto.bindTo('bound', map);


var toText = document.getElementById('TAdd');

var toAuto = new google.maps.places.Autocomplete(toText, options);


toAuto.bindTo('bound', map);


    geocoder = new google.maps.Geocoder();
  }

  function calculateDistances() {
    var service = new google.maps.DistanceMatrixService();
    service.getDistanceMatrix(
      {
        origins: [document.getElementById("FAdd").value],
        destinations: [document.getElementById("TAdd").value],
        travelMode: google.maps.TravelMode.DRIVING,
        unitSystem: google.maps.UnitSystem.METRIC,
        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 = '';
      deleteOverlays();


      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);

        outputDiv.innerHTML += results[j].distance.text  + '<br>'; 

        }
      }
   }
  }

  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;
    }
  }

</script>
4

1 回答 1

0

我使用 Ajax 调用 PHP,但尚未使用 getDistanceMatrix(),但这应该是一个简单的修复。首先,如果你知道你总是只有一个起点和一个目的地,你就不需要回调函数中的“for”循环。其次,您使用的是距离文本而不是距离值。

function callback(response, status) {
    if (status != google.maps.DistanceMatrixStatus.OK) {
       [...]
    } else {
        deleteOverlays();
        var outputDiv = document.getElementById('outputDiv'),
            origin = response.originAddresses[0],
            destination = response.destinationAddresses[0],
            result = response.rows[0].elements[0],
            distance = result.distance.value,
            text = result.distance.text,
            price = 10 * distance;
        outputDiv.innerHTML = '<p>' + text + ': Rs.' + price + '</p>';
        addMarker(origin, false);
        addMarker(destination, false);
    }

}

我没有对此进行测试,因此可能需要对其进行调整。(见https://developers.google.com/maps/documentation/distancematrix/#DistanceMatrixResponses

于 2013-02-28T15:25:32.743 回答