Calculation of distances with longitude and latitude has different results

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); ?> 
+4
source share
2 answers

I tried the Formula of the big circle, and the result is 85.5 m (your formula is not quite correct, but close). The difference with Google is that Google is trying to calculate the distance from two points on the road, one way or another a point is projected onto the road. See the figure below. (The red line is a graph from the coordinates, it has a distance of 85.5 m, the blue line is displayed by Google, it somehow "comes off" from the road) The actual line is red has a distance of 85.5m, the blue line is render by Google, is 'snapped' to the road

+4
source

The specified map link has different latitude and longitude.

  "northeast" : { "lat" : 6.913845999999999, "lng" : 79.9733002 }, "southwest" : { "lat" : 6.913794800000001, "lng" : 79.9730488 } 

Using this in your query, it gives the correct distance.

+2
source

Source: https://habr.com/ru/post/1499085/


All Articles