How to find nearest location using latitude and longitude from SQL database?

前端 未结 7 1444
离开以前
离开以前 2020-11-30 17:41

I want to find a nearest location from following database table

Address                            Latitude                longitude 

Kathmandu 44600, Nepal          


        
7条回答
  •  慢半拍i
    慢半拍i (楼主)
    2020-11-30 18:03

    When the method should perform it is nicer to first filter on latitude and longitude, and then calculate the squared distance approximative. For Nordic countries, it will be about 0.3 percent off within 1000 km's.

    So instead of calculatinG the distance as:

    dist_Sphere = r_earth * acos ( sin (lat1) * sin (lat2) + cos(lat1)*cos(lat2)*cos(lon 2 - lon 1)
    

    one can calculate the approximate value (assume that lat = lat 1 is close to lat 2) as

    const cLat2 = cos lat ^ 2
    const r_earth2 = r_earth ^ 2
    dist_App ^2 = r_earth2 * ((lat 2 - lat 1) ^2 + clat2 *(lon 2 - lon 1) ^2)
    

    Order by Dist_App 2, and then simply take the square root off the result.

提交回复
热议问题