I'm trying to distance myself from two locations, I really need this to be done with mySQL, since I have to filter many records. Unfortunately, the mysql statement results are different from the Google Maps results. What could be the reason.
My Mysql statement (Values ββare hard-coded)
SELECT ((ACOS(SIN(6.914556 * PI() / 180) * SIN(6.913794 * PI() / 180) + COS(6.914556 * PI() / 180) * COS(6.913794 * PI() / 180) * COS((79.973194- 79.97330) * PI() / 180)) * 180 / PI()) * 60 * 1.609344 * 1000) AS `distance`
As a distance, I get 74.27 meters.
Then I used another SQL statement that I found, and it gives 85.53 meters.
SELECT (1.609344 * 1000 * 3959 * acos( cos( radians(6.914556) ) * cos( radians( 6.913794 ) ) * cos( radians( 79.97330 ) - radians(79.973194) ) + sin( radians(6.914556) ) * sin( radians( 6.913794) ) ) ) AS distance
But if I use the Google API
http://maps.googleapis.com/maps/api/directions/json?origin=6.914556,79.973194&destination=6.913794,79.97330&sensor=false
I get 28 meters as a distance.
Anyway, I can fix this problem. I need a solution for working with MySQL. Assign all your support.
Edit:
I tried with PHP, but I have a difference in distance.
<?php function distance($lat1, $lon1, $lat2, $lon2, $unit) { $theta = $lon1 - $lon2; $dist = sin(deg2rad($lat1)) * sin(deg2rad($lat2)) + cos(deg2rad($lat1)) * cos(deg2rad($lat2)) * cos(deg2rad($theta)); $dist = acos($dist); $dist = rad2deg($dist); $miles = $dist * 60 * 1.1515; $unit = strtoupper($unit); if ($unit == "K") { return ($miles * 1.609344); } else if ($unit == "M") { return ($miles * 1.609344 * 1000); }else if ($unit == "N") { return ($miles * 0.8684); } else { return $miles; } } function getDrivingDistance($inLatitude,$inLongitude,$outLatitude,$outLongitude) { if(empty($inLatitude) || empty($inLongitude) ||empty($outLatitude) ||empty($outLongitude)) return 0; // Generate URL $url = "http://maps.googleapis.com/maps/api/directions/json?origin=$inLatitude,$inLongitude&destination=$outLatitude,$outLongitude&sensor=false"; // Retrieve the URL contents $c = curl_init(); curl_setopt($c, CURLOPT_RETURNTRANSFER, 1); curl_setopt($c, CURLOPT_URL, $url); $jsonResponse = curl_exec($c); curl_close($c); $dataset = json_decode($jsonResponse); if(!$dataset) return 0; if(!isset($dataset->routes[0]->legs[0]->distance->value)) return 0; $distance = $dataset->routes[0]->legs[0]->distance->value; return $distance; } echo distance(6.914556,79.973194,6.913794,79.97330,'M') . "<br>"; echo getDrivingDistance(6.914556,79.973194,6.913794,79.97330); ?>