how to to process result of google distance matrix api further? -
i new programming.. have code gives distance between 2 points need farther multiply integer 10.. project working on abt calculating distance between 2 points , multiplying fare/km rs.10/km (indian rupees) same. if distance 30 km fare 30*10 = rs.300
thanks in advance
following code
<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(); (var = 0; < origins.length; i++) { var results = response.rows[i].elements; addmarker(origins[i], false); (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 not successful next reason: ' + status); } }); } function deleteoverlays() { if (markersarray) { (i in markersarray) { markersarray[i].setmap(null); } markersarray.length = 0; } } </script>
i utilize ajax phone call php, , haven't yet used getdistancematrix(), should easy fix. first, if know have 1 origin , 1 destination, don't need "for" loop in callback function. second, you're taking distance text rather distance value.
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, cost = 10 * distance; outputdiv.innerhtml = '<p>' + text + ': rs.' + cost + '</p>'; addmarker(origin, false); addmarker(destination, false); } }
i haven't tested this, needs tweaked. ( see https://developers.google.com/maps/documentation/distancematrix/#distancematrixresponses )
api matrix distance
No comments:
Post a Comment