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

前端 未结 11 630
傲寒
傲寒 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 04:54

    Since you say that any language is acceptable, the natural choice is PostGIS:

    SELECT * FROM places
    WHERE ST_DistanceSpheroid(geom, $location, $spheroid) < $max_metres;
    

    If you want to use WGS datum, you should set $spheroid to 'SPHEROID["WGS 84",6378137,298.257223563]'

    Assuming that you have indexed places by the geom column, this should be reasonably efficient.

提交回复
热议问题