2013-02-16 47 views
0

我是新手编程..我有这个代码给出两点之间的距离,但需要进一步乘以一个整数说10 ..我正在工作的项目是abt计算两点之间的距离并乘以票价/ Km,例如Rs.10/km(印度卢比)。因此,如果距离为30公里,车费是30 * 10 = Rs.300如何进一步处理google距离矩阵api的结果?

在此先感谢

以下是代码

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

回答

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