MAPG-6 introduce and unify geo calculations
This commit is contained in:
1 parent
1d4bfd75d8
commit
e15421af8b
3 files changed
+42
-9
No files matched your search
@@ -2,6 +2,8 @@
|
||||
|
||||
class Position
|
||||
{
|
||||
const EARTH_RADIUS_IN_METER = 6371000;
|
||||
|
||||
private float $lat;
|
||||
private float $lng;
|
||||
|
||||
@@ -21,6 +23,24 @@ class Position
|
||||
return $this->lng;
|
||||
}
|
||||
|
||||
public function calculateDistanceTo(Position $otherPosition): float
|
||||
{
|
||||
$lat1 = deg2rad($this->lat);
|
||||
$lng1 = deg2rad($this->lng);
|
||||
$lat2 = deg2rad($otherPosition->lat);
|
||||
$lng2 = deg2rad($otherPosition->lng);
|
||||
|
||||
$angleCos = cos($lat1) * cos($lat2) * cos($lng2 - $lng1) + sin($lat1) * sin($lat2);
|
||||
|
||||
if ($angleCos > 1.0) {
|
||||
$angleCos = 1.0;
|
||||
}
|
||||
|
||||
$angle = acos($angleCos);
|
||||
|
||||
return $angle * static::EARTH_RADIUS_IN_METER;
|
||||
}
|
||||
|
||||
public function toArray(): array
|
||||
{
|
||||
return [
|
||||
|
||||
Reference in new issue
Block a user