-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Added a math utility class to do distance and direction calculations …
…between two lat lon coordinates
- Loading branch information
1 parent
2406a9d
commit 092554b
Showing
1 changed file
with
46 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
<?php | ||
|
||
namespace App\Utility; | ||
|
||
class Math | ||
{ | ||
public static $earthRadius = 6371; | ||
|
||
public static function distance($lat1, $lon1, $lat2, $lon2) | ||
{ | ||
$lat1 = deg2rad($lat1); | ||
$lon1 = deg2rad($lon1); | ||
$lat2 = deg2rad($lat2); | ||
$lon2 = deg2rad($lon2); | ||
|
||
$latDiff = $lat2 - $lat1; | ||
$lonDiff = $lon2 - $lon1; | ||
|
||
$a = sin($latDiff / 2) ** 2 + cos($lat1) * cos($lat2) * sin($lonDiff / 2) ** 2; | ||
$c = 2 * atan2(sqrt($a), sqrt(1 - $a)); | ||
|
||
$distance = self::$earthRadius * $c; | ||
|
||
return $distance; | ||
} | ||
|
||
public static function direction($lat1, $lon1, $lat2, $lon2) | ||
{ | ||
$lat1 = deg2rad($lat1); | ||
$lon1 = deg2rad($lon1); | ||
$lat2 = deg2rad($lat2); | ||
$lon2 = deg2rad($lon2); | ||
|
||
$lonDiff = $lon2 - $lon1; | ||
|
||
$y = sin($lonDiff) * cos($lat2); | ||
$x = cos($lat1) * sin($lat2) - sin($lat1) * cos($lat2) * cos($lonDiff); | ||
|
||
$bearing = atan2($y, $x); | ||
$bearing = rad2deg($bearing); | ||
|
||
$bearing = ($bearing + 360) % 360; | ||
|
||
return $bearing; | ||
} | ||
} |