Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Find distance between two points using latitude and longitude in mysql

Hi I have the following table

 --------------------------------------------  |  id  |  city  |  Latitude  |  Longitude  |  --------------------------------------------  |  1   |   3    |   34.44444 |   84.3434   |  --------------------------------------------  |  2   |   4    | 42.4666667 | 1.4666667   |  --------------------------------------------  |  3   |   5    |  32.534167 | 66.078056   |  --------------------------------------------  |  4   |   6    |  36.948889 | 66.328611   |  --------------------------------------------  |  5   |   7    |  35.088056 | 69.046389   |  --------------------------------------------  |  6   |   8    |  36.083056 |   69.0525   |  --------------------------------------------  |  7   |   9    |  31.015833 | 61.860278   |  -------------------------------------------- 

Now I want to get distance between two points. Say a user is having a city 3 and a user is having a city 7. My scenario is one user having a city and latitue and longtitude is searching other users distance from his city. For example user having city 3 is searching. He wants to get distance of user of any other city say it is 7. I have searched and found following query

SELECT `locations`.`city`, ( 3959 * acos ( cos ( radians(31.589167) ) * cos( radians( Latitude ) ) * cos( radians( Longitude ) - radians(64.363333) ) + sin ( radians(31.589167) ) * sin( radians( Latitude ) ) ) ) AS `distance` FROM `locations` HAVING (distance < 50) 

As for as I know this query finds distance from one point to all other points. Now I want to get distance from one point to other point.

Any guide line will be much appreciated.

like image 690
Enthusiast Avatar asked Jun 23 '14 16:06

Enthusiast


People also ask

How do you find the distance between two latitude longitude points in MySQL?

Heres is MySQL query and function which use to get distance between two latitude and longitude and distance will return in KM. SELECT getDistance($lat1,$lng1,$lat2,$lng2) as distance FROM your_table. Almost a decade later, this function gives THE SAME results as Google Maps distance measurement. Thank you!

How do you find the distance between two points using latitude and longitude?

For this divide the values of longitude and latitude of both the points by 180/pi. The value of pi is 22/7. The value of 180/pi is approximately 57.29577951. If we want to calculate the distance between two places in miles, use the value 3, 963, which is the radius of Earth.

How can I find the distance between two points using latitude and longitude in PHP?

php function distance($lat1, $lon1, $lat2, $lon2, $unit) { if (($lat1 == $lat2) && ($lon1 == $lon2)) { return 0; } else { $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 = $ ...


1 Answers

I think your question says you have the city values for the two cities between which you wish to compute the distance.

This query will do the job for you, yielding the distance in km. It uses the spherical cosine law formula.

Notice that you join the table to itself so you can retrieve two coordinate pairs for the computation.

SELECT a.city AS from_city, b.city AS to_city,     111.111 *     DEGREES(ACOS(LEAST(1.0, COS(RADIANS(a.Latitude))          * COS(RADIANS(b.Latitude))          * COS(RADIANS(a.Longitude - b.Longitude))          + SIN(RADIANS(a.Latitude))          * SIN(RADIANS(b.Latitude))))) AS distance_in_km   FROM city AS a   JOIN city AS b ON a.id <> b.id  WHERE a.city = 3 AND b.city = 7 

Notice that the constant 111.1111 is the number of kilometres per degree of latitude, based on the old Napoleonic definition of the metre as one ten-thousandth of the distance from the equator to the pole. That definition is close enough for location-finder work.

If you want statute miles instead of kilometres, use 69.0 instead.

http://sqlfiddle.com/#!9/21e06/412/0

If you're looking for nearby points you may be tempted to use a clause something like this:

   HAVING distance_in_km < 10.0    /* slow ! */     ORDER BY distance_in_km DESC 

That is (as we say near Boston MA USA) wicked slow.

In that case you need to use a bounding box computation. See this writeup about how to do that. http://www.plumislandmedia.net/mysql/haversine-mysql-nearest-loc/

The formula contains a LEAST() function. Why? Because the ACOS() function throws an error if its argument is even slightly greater than 1. When the two points in question are very close together, the expression with the COS() and SIN() computations can sometimes yield a value slightly greater than 1 due to floating-point epsilon (inaccuracy). The LEAST(1.0, dirty-great-expression) call copes with that problem.

There's a better way, a formula by Thaddeus Vincenty. It uses ATAN2() rather than ACOS() so it's less susceptible to epsilon problems.


Edit 2022 (by Alexio Vay): As of today the modern solution should be the following short code:

   select ST_Distance_Sphere(     point(-87.6770458, 41.9631174),     point(-73.9898293, 40.7628267))  

Please check out the answer of Naresh Kumar.

like image 51
O. Jones Avatar answered Sep 28 '22 03:09

O. Jones