distance matrix google maps gives wrong distance between two locations while using in curl php code -
$userlat1=51.509904342252; $userlong1= -0.13413459062576; $userlat2=51.517618; $userlong2= -0.096778; $userlat3=51.5017863; $userlong4= -0.0536478; $userlat=51.509904342252; $userlong= -0.13413459062576; $lat2=51.495042; $long2= -0.131382; $url = "https://maps.googleapis.com/maps/api/distancematrix/json?origins=".$userlat.",".$userlong."&destinations=".$lat2.",".$long2."&mode=driving&sensor=false"; //$response = file_get_contents($url); $ch = curl_init(); curl_setopt($ch, curlopt_url, $url); curl_setopt($ch, curlopt_returntransfer, 1); curl_setopt($ch, curlopt_proxyport, 3128); curl_setopt($ch, curlopt_ssl_verifyhost, 0); curl_setopt($ch, curlopt_ssl_verifypeer, 0); $response = curl_exec($ch); curl_close($ch); $response_a = json_decode($response, true);
when mark pins on map @ locations
$userlat1=51.509904342252; $userlong1= -0.13413459062576; $userlat2=51.517618; $userlong2= -0.096778; $userlat3=51.5017863; $userlong4= -0.0536478; $userlat=51.509904342252; $userlong= -0.13413459062576; $lat2=51.495042; $long2= -0.131382;
,i can see on map marker near $lat2 &$long2.
but when use above code distance between 2 positions mentioned above,it gives me incorrect data.. in way gives more distance value nearest station ,which seeing on map nearest lat2 & long2 position on map.
similarly if use above code distance between
$userextralat2=21.118692, $userextralong2=73.117554
&
$lat2=51.495042; $long2= -0.131382;
google provides distance in days & hours..
how distance in km using distancematrix api ?
what distance matrix (modifying this example):
origin[0]:51.509904,-0.134135 origin[0]:a4, london w1j, uk destination[0]:51.509904,-0.134135: 1 m in 1 min origin[0]:a4, london w1j, uk destination[1]:51.517618,-0.096778: 4.1 km in 15 mins origin[0]:a4, london w1j, uk destination[2]:51.501786,-0.053648: 7.4 km in 23 mins origin[0]:a4, london w1j, uk destination[3]:51.495042,-0.131382: 2.8 km in 9 mins origin[0]:a4, london w1j, uk destination[4]:21.118692,73.117554: 11,411 km in 7 days 5 hours
the distance returned in results (at least in google maps javascript api v3, should in web service well): results[j].distance.text
code snippet:
var geocoder; var map; var bounds = new google.maps.latlngbounds(); var markersarray = []; 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'; var locations; function initialize() { map = new google.maps.map( document.getelementbyid("map_canvas"), { center: new google.maps.latlng(37.4419, -122.1419), zoom: 13, maptypeid: google.maps.maptypeid.roadmap }); geocoder = new google.maps.geocoder(); $userlat1 = 51.509904342252; $userlong1 = -0.13413459062576; $userlat2 = 51.517618; $userlong2 = -0.096778; $userlat3 = 51.5017863; $userlong4 = -0.0536478; // $userlat = 51.509904342252; // $userlong = -0.13413459062576; $lat2 = 51.495042; $long2 = -0.131382; $userextralat2 = 21.118692, $userextralong2 = 73.117554 locations = [new google.maps.latlng($userlat1, $userlong1), new google.maps.latlng($userlat2, $userlong2), new google.maps.latlng($userlat3, $userlong4), // new google.maps.latlng($userlat, $userlong), new google.maps.latlng($lat2, $long2), new google.maps.latlng($userextralat2, $userextralong2) ]; } function calculatedistances() { var service = new google.maps.distancematrixservice(); service.getdistancematrix({ origins: locations, destinations: locations, 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(); outputdiv.innerhtml += "origin[0]:" + locations[0].tourlvalue(6) + '<br>'; (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 += "origin[" + + "]:" + origins[i] + ' destination[' + j + ']:' + locations[j].tourlvalue(6) /* destinations[j] */ + ': ' + results[j].distance.text + ' in ' + results[j].duration.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 following reason: ' + status); } }); } function deleteoverlays() { (var = 0; < markersarray.length; i++) { markersarray[i].setmap(null); } markersarray = []; } google.maps.event.adddomlistener(window, "load", initialize);
html, body, #map_canvas { height: 500px; width: 500px; margin: 0px; padding: 0px } #content-pane { width: 100%; padding-left: 2%; } #outputdiv { font-size: 11px; }
<script src="https://maps.googleapis.com/maps/api/js"></script> <div id="map_canvas" style="border: 2px solid #3872ac;"></div> <div id="content-pane"> <div id="inputs"> <p> <button type="button" onclick="calculatedistances();">calculate distances </button> </p> </div> <div id="outputdiv"></div> </div>
Comments
Post a Comment