如何进一步处理谷歌距离矩阵api的结果?

时间:2013-02-16 12:09:30

标签: api matrix distance

我是编程的新手..我有这个代码,给出两点之间的距离,但需要进一步乘以整数说10 ..我正在研究的项目是计算两点之间的距离并乘以它票价/公里像Rs.10 / km(印度卢比)一样。因此,如果距离是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>

1 个答案:

答案 0 :(得分:0)

我使用对PHP的Ajax调用,还没有使用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