-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathGeoDistance.php
33 lines (29 loc) · 1.01 KB
/
GeoDistance.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
<?php
namespace App\Module\Distance\Application\Math;
use App\Module\Distance\Domain\Constant;
use App\Shared\Domain\Location;
use PhpUnitConversion\Unit\Length\KiloMeter;
/**
* @see https://pl.wikibooks.org/wiki/Astronomiczne_podstawy_geografii/Odleg%C5%82o%C5%9Bci
* @see https://community.fabric.microsoft.com/t5/Desktop/How-to-calculate-lat-long-distance/td-p/1488227
*/
class GeoDistance
{
public static function calcDistanceV1(Location $a, Location $b): KiloMeter
{
return new KiloMeter(sqrt(
pow($b->lat - $a->lat, 2) +
pow(cos(($a->lat * M_PI) / 180) * ($b->lng - $a->lng), 2)
) * Constant::GEO_DEG_KM);
}
public static function calcDistanceV2(Location $a, Location $b): KiloMeter
{
return new KiloMeter(acos(
sin(deg2rad($a->lat)) *
sin(deg2rad($b->lat)) +
cos(deg2rad($a->lat)) *
cos(deg2rad($b->lat)) *
cos(deg2rad($b->lng) - deg2rad($a->lng))
) * Constant::EARTH_RADIUS_KM);
}
}