How to calculate distance using latitude and longitude?

老子叫甜甜 提交于 2019-12-01 07:26:59

问题


I have table having latitude and longitude


     Lat            Longitude

40.8151 -73.0455    U   36103
40.8132 -73.0476    U   36103

So what i want is I am passing two Longitude and latitude from my page How can i find nearest place using store procedure i don't have a much hand on it so i have to ask here

Tried using this one

( 3959 * acos( cos( radians(37) ) * cos( radians( @latitude ) ) * cos( radians( @longtitude ) - radians(-122) ) + sin( radians(37) ) * sin( radians( @latitude ) ) ) )

But i have to match it with the table values so how can i achieve this?


回答1:


SELECT ROUND(6371 * acos(cos(radians('lat')) * cos(radians(latitude)) * cos(radians(longitude) - radians('long')) + sin(radians('lat')) * sin(radians(latitude)))) as distance,latitude,longitude, from your_table HAVING distance<=20  order by distance

You can use a query similar to shown above in your SQL where 'lat' and 'long' are the variable values that you are passing to search for. The 'longitude' and 'latitude' are column names from your table. And btw, this is called Haversine formula and the constant 6371 is used to get distance in KM, while 3959 is used to get distance in miles. You can use either of them as per your requirement.



来源:https://stackoverflow.com/questions/21084886/how-to-calculate-distance-using-latitude-and-longitude

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!