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

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

I want to find a nearest location from following database table

Address                            Latitude                longitude 

Kathmandu 44600, Nepal          


        
7条回答
  •  挽巷
    挽巷 (楼主)
    2020-11-30 17:59

    SELECT latitude, longitude, SQRT(
        POW(69.1 * (latitude - [startlat]), 2) +
        POW(69.1 * ([startlng] - longitude) * COS(latitude / 57.3), 2)) AS distance
    FROM TableName HAVING distance < 25 ORDER BY distance;
    

    where [starlat] and [startlng] is the position where to start measuring the distance and 25 is the distance in kms.

    It is advised to make stored procedure to use the query because it would be checking a lots of rows to find the result.

提交回复
热议问题