Calculate distance between PLZs if given
This commit is contained in:
parent
853b570f0f
commit
d33e28467b
7 changed files with 716 additions and 472 deletions
20
src/Service/DistanceCalculator.php
Normal file
20
src/Service/DistanceCalculator.php
Normal file
|
@ -0,0 +1,20 @@
|
|||
<?php
|
||||
|
||||
namespace App\Service;
|
||||
|
||||
use Location\Coordinate;
|
||||
use Location\Distance\Vincenty;
|
||||
|
||||
class DistanceCalculator
|
||||
{
|
||||
public function calculateDistance(Coordinate $coordinate1, Coordinate $coordinate2)
|
||||
{
|
||||
$calculator = new Vincenty();
|
||||
|
||||
$distance = $calculator->getDistance($coordinate1, $coordinate2);
|
||||
|
||||
$distance = round($distance / 1000);
|
||||
|
||||
return $distance;
|
||||
}
|
||||
}
|
20
src/Service/PlzToCoordinate.php
Normal file
20
src/Service/PlzToCoordinate.php
Normal file
|
@ -0,0 +1,20 @@
|
|||
<?php
|
||||
|
||||
namespace App\Service;
|
||||
|
||||
use Location\Coordinate;
|
||||
|
||||
class PlzToCoordinate
|
||||
{
|
||||
public function getCoordinates(int $plz)
|
||||
{
|
||||
$content = file_get_contents("https://swisspost.opendatasoft.com/api/records/1.0/search/?dataset=plz_verzeichnis_v2&q=postleitzahl%3D" . $plz);
|
||||
$result = json_decode($content);
|
||||
|
||||
if (isset($result->records[0]->fields->geo_point_2d[0]) && isset($result->records[0]->fields->geo_point_2d[1])) {
|
||||
$coordinate = new Coordinate($result->records[0]->fields->geo_point_2d[0], $result->records[0]->fields->geo_point_2d[1]);
|
||||
}
|
||||
|
||||
return $coordinate;
|
||||
}
|
||||
}
|
Reference in a new issue