Skip to content

Instantly share code, notes, and snippets.

@oleg-andreyev
Created February 22, 2020 19:26
Show Gist options
  • Select an option

  • Save oleg-andreyev/82ef8b710308c3b2b2fa56712a246493 to your computer and use it in GitHub Desktop.

Select an option

Save oleg-andreyev/82ef8b710308c3b2b2fa56712a246493 to your computer and use it in GitHub Desktop.

Revisions

  1. oleg-andreyev created this gist Feb 22, 2020.
    22 changes: 22 additions & 0 deletions hf.php
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,22 @@
    /**
    * Calculates the geodesic distance between two points specified by radian latitude/longitude using the
    * Haversine formula (hf)
    */
    function hf($lat1, $lng1, $lat2, $lng2)
    {
    $R = 6371e3;

    $lng1 = deg2rad($lng1);
    $lat1 = deg2rad($lat1);
    $lng2 = deg2rad($lng2);
    $lat2 = deg2rad($lat2);

    $deltaLat = $lng2 - $lng1;
    $deltaLng = $lat2 - $lat1;

    $a = (sin($deltaLat / 2) ** 2) + (cos($lng1) * cos($lng2) * (sin($deltaLng / 2) ** 2));
    $c = 2 * asin(min(1, sqrt($a)));
    $d = $R * $c;

    return $d;
    }