Skip to content

Instantly share code, notes, and snippets.

@adamrobbie
Created March 31, 2016 19:51
Show Gist options
  • Select an option

  • Save adamrobbie/2eb3531d24fa783b2535540f13087803 to your computer and use it in GitHub Desktop.

Select an option

Save adamrobbie/2eb3531d24fa783b2535540f13087803 to your computer and use it in GitHub Desktop.

Revisions

  1. adamrobbie created this gist Mar 31, 2016.
    16 changes: 16 additions & 0 deletions havershine.ex
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,16 @@
    defmodule Havershine do
    def distance(lng1, lat1, lng2, lat2) do
    [rLng1, rLat1, rLng2, rLat2] = for deg <- [lng1, lat1, lng2, lat2], do: deg2rad(deg)

    dLon = rLng2 - rLng1
    dLat = rLat2 - rLat1

    a = :math.pow(:math.sin(dLat/2), 2) + :math.cos(rLat1) * :math.cos(rLat2) * :math.pow(:math.sin(dLon/2), 2)

    c = 2 * :math.asin(:math.sqrt(a))

    km = 6372.8 * c
    end

    defp deg2rad(deg), do: :math.pi()*deg/180
    end