Find nearest geographic coordinates

Anders Dahlin anders@REDACTED
Mon Nov 30 04:07:02 CET 2020


I don't know how well this will suite your needs, but it's what I use to
calculate the distance between to {Lat, Lng} points.

-define(EarthRadius, 6372.8).

haversine({Lat1, Lng1}, {Lat2, Lng2}) ->
    RLat1 = deg2rad(Lat1),
    RLat2 = deg2rad(Lat2),
    DLat  = RLat2 - RLat1,
    DLng  = deg2rad(Lng2) - deg2rad(Lng1),
    A     = math:pow(math:sin(DLat/2), 2) +
	    math:pow(math:sin(DLng/2), 2) * math:cos(RLat1) * math:cos(RLat2),
    C     = 2 * math:asin(math:sqrt(A)),
    ?EarthRadius * C.

deg2rad(Deg) ->
    math:pi() * Deg / 180.


On 2020-11-28 20:09, Joa Gre wrote:
> This might come in handy:
> 
> %% https://en.m.wikipedia.org/wiki/Geographic_coordinate_conversion#From_geodetic_to_ECEF_coordinates <https://en.m.wikipedia.org/wiki/Geographic_coordinate_conversion#From_geodetic_to_ECEF_coordinates> 
> %% https://en.m.wikipedia.org/wiki/Geodetic_datum#World_Geodetic_System_1984_(WGS_84) <https://en.m.wikipedia.org/wiki/Geodetic_datum#World_Geodetic_System_1984_(WGS_84)>
> 
> geodetic_to_ecef_coordinates(Latitude, Longitude, H) ->
>     CLatitude = math:cos(Latitude * ?RADIANS_PER_DEGREE),
>     SLatitude = math:sin(Latitude *  ?RADIANS_PER_DEGREE),
>     CLongitude = math:cos(Longitude * ?RADIANS_PER_DEGREE),
>     SLongitude = math:sin(Longitude  * ?RADIANS_PER_DEGREE),
>     %% Semi-major axis
>     A = 6378137.0,
>     A2 = math:pow(A, 2),
>     %% Semi-minor axis
>     B = 6356752.3142,
>     B2 = math:pow(B, 2),
>     %% Prime vertical radius of curvature
>     N = A2 / math:sqrt(
>                math:pow(CLatitude, 2) * A2 + math:pow(SLatitude, 2) * B2),
>     X = (N + H) * CLatitude * CLongitude, 
>     Y = (N + H) * CLatitude * SLongitude, 
>     Z  = (B2 / A2 * N + H) * SLatitude,
>     {X, Y, Z}.
> 
> 
> Den ons 25 nov. 2020 22:39Frank Muller <frank.muller.erl@REDACTED
> <mailto:frank.muller.erl@REDACTED>> skrev:
> 
>     Hi guys,
> 
>     I've a list of geographic coordinates: 
> 
>     L = [ {{<<"longitude">>,6.1457}, {<<"latitude">>,46.2022}},
>           {{<<"longitude">>,2.3387}, {<<"latitude">>,48.8582}},
>           ... ]
> 
>     and a specific coordinate X = {{<<"longitude">>,-73.5848},
>     {<<"latitude">>,45.4995}}.
> 
>     Question: how can i find the nearest coordinates to X from L (sorted
>     from the nearest to the farest)?
> 
>     /Frank
> 


More information about the erlang-questions mailing list