Fix plz error

This commit is contained in:
Jannis Portmann 2021-12-22 17:36:12 +01:00
parent 28f71ee479
commit e59f3c05e3
3 changed files with 36 additions and 7 deletions

View file

@ -81,7 +81,16 @@ class OfferController extends AbstractController
if (isset($userPlz))
{
$distance = $distanceCalculator->calculateDistance($plzconverter->getCoordinates($offerPlz), $plzconverter->getCoordinates($userPlz));
if (isset($offerPlz))
{
$offerCoordinate = $plzconverter->convertPlzToCoordinate($offerPlz);
$userCoordinate = $plzconverter->convertPlzToCoordinate($userPlz);
if ($userCoordinate != null && $offerCoordinate != null)
{
$distance = $distanceCalculator->calculateDistance($offerCoordinate, $userCoordinate);
}
}
}
return $this->render('app/offer.html.twig', [

View file

@ -9,12 +9,19 @@ class DistanceCalculator
{
public function calculateDistance(Coordinate $coordinate1, Coordinate $coordinate2)
{
$calculator = new Vincenty();
if ($coordinate1 == null || $coordinate2 == null)
{
$distance = "N/A";
}
else
{
$calculator = new Vincenty();
$distance = $calculator->getDistance($coordinate1, $coordinate2);
$distance = round($distance / 1000);
$distance = $calculator->getDistance($coordinate1, $coordinate2);
$distance = round($distance / 1000);
}
return $distance;
}
}

View file

@ -11,8 +11,21 @@ class PlzToCoordinate
$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]);
for ($i=0; $i<2; $i++)
{
try {
$lat = $result->records[$i]->fields->geo_point_2d[0];
$long = $result->records[$i]->fields->geo_point_2d[0];
} catch (\Throwable $th) {
// throw $th;
}
}
if (isset($lat) && isset($long)) {
$coordinate = new Coordinate($lat, $long);
}
else {
$coordinate = null;
}
return $coordinate;