How to calculate distance using latitude and longitude?

后端 未结 1 1120
梦毁少年i
梦毁少年i 2021-01-07 03:40

I have table having latitude and longitude


     Lat            Longitude

40.8151 -73.0455    U   36103
40.8132          


        
1条回答
  •  别那么骄傲
    2021-01-07 04:20

    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.

    0 讨论(0)
提交回复
热议问题