Algorithm to find all Latitude Longitude locations within a certain distance from a given Lat Lng location

前端 未结 11 637
傲寒
傲寒 2020-12-02 04:32

Given a database of places with Latitude + Longitude locations, such as 40.8120390, -73.4889650, how would I find all locations within a given distance of a specific locatio

11条回答
  •  隐瞒了意图╮
    2020-12-02 05:02

    Based on the current user's latitude, longitude and the distance you wants to find,the sql query is given below.

    SELECT * FROM(
        SELECT *,(((acos(sin((@latitude*pi()/180)) * sin((Latitude*pi()/180))+cos((@latitude*pi()/180)) * cos((Latitude*pi()/180)) * cos(((@longitude - Longitude)*pi()/180))))*180/pi())*60*1.1515*1.609344) as distance FROM Distances) t
    WHERE distance <= @distance
    

    @latitude and @longitude are the latitude and longitude of the point. Latitude and longitude are the columns of distances table. Value of pi is 22/7

提交回复
热议问题