2020-05-17 22:26:23 +02:00
|
|
|
<?php namespace MapGuesser\Util\Geo;
|
2020-05-17 19:29:06 +02:00
|
|
|
|
|
|
|
class Position
|
|
|
|
{
|
2020-05-19 19:01:35 +02:00
|
|
|
const EARTH_RADIUS_IN_METER = 6371000;
|
|
|
|
|
2020-05-17 19:29:06 +02:00
|
|
|
private float $lat;
|
|
|
|
private float $lng;
|
|
|
|
|
|
|
|
public function __construct(float $lat, float $lng)
|
|
|
|
{
|
|
|
|
$this->lat = $lat;
|
|
|
|
$this->lng = $lng;
|
|
|
|
}
|
|
|
|
|
|
|
|
public function getLat(): float
|
|
|
|
{
|
|
|
|
return $this->lat;
|
|
|
|
}
|
|
|
|
|
|
|
|
public function getLng(): float
|
|
|
|
{
|
|
|
|
return $this->lng;
|
|
|
|
}
|
|
|
|
|
2020-05-19 19:01:35 +02:00
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2020-05-19 03:14:20 +02:00
|
|
|
public function toArray(): array
|
2020-05-17 19:29:06 +02:00
|
|
|
{
|
2020-05-19 03:14:20 +02:00
|
|
|
return [
|
2020-05-17 19:29:06 +02:00
|
|
|
'lat' => $this->lat,
|
|
|
|
'lng' => $this->lng,
|
2020-05-19 03:14:20 +02:00
|
|
|
];
|
|
|
|
}
|
|
|
|
|
|
|
|
public function toJson(): string
|
|
|
|
{
|
|
|
|
return json_encode($this->toArray());
|
2020-05-17 19:29:06 +02:00
|
|
|
}
|
|
|
|
}
|