Find distance between two points using latitude and longitude in mysql -



Find distance between two points using latitude and longitude in mysql -

hi have next 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 want distance between 2 points. user having city 3 , user having city 7. scenario 1 user having city , latitue , longtitude searching other users distance city. illustration user having city 3 searching. wants distance of user of other city 7. have searched , found next 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 ) ) ) ) `distance` `locations` having (distance < 50)

as know query finds distance 1 point other points. want distance 1 point other point.

any guide line much appreciated.

i think question says have city values 2 cities between wish compute distance.

this query job you, yielding distance in km. uses spherical cosine law formula.

notice bring together table can retrieve 2 coordinate pairs computation.

select a.city from_city, b.city to_city, 111.1111 * degrees(acos(cos(radians(a.latitude)) * cos(radians(b.latitude)) * cos(radians(a.longitude - b.longitude)) + sin(radians(a.latitude)) * sin(radians(b.latitude)))) distance_in_km city bring together city b on a.id <> b.id a.city = 3 , b.city = 7

notice constant 111.1111 number of kilometres per grade of latitude, based on old napoleonic definition of metre 1 ten-thousandth of distance equator pole. definition close plenty location-finder work.

if want statute miles instead of kilometres, utilize 69.0 instead.

http://sqlfiddle.com/#!2/abcc8/4/0

mysql latitude-longitude

Comments

Popular posts from this blog

php - Android app custom user registration and login with cookie using facebook sdk -

django - Access session in user model .save() -

php - .htaccess Multiple Rewrite Rules / Prioritizing -