Calculate distance between PLZs if given

This commit is contained in:
Jannis Portmann 2021-06-14 14:10:07 +02:00
parent 853b570f0f
commit d33e28467b
7 changed files with 716 additions and 472 deletions

View file

@ -12,6 +12,7 @@
"doctrine/doctrine-migrations-bundle": "^3.1",
"doctrine/orm": "^2.8",
"easycorp/easyadmin-bundle": "^3",
"mjaschen/phpgeo": "^3.2",
"sensio/framework-extra-bundle": "^6.1",
"symfony/asset": "5.2.*",
"symfony/console": "5.2.*",

1097
composer.lock generated

File diff suppressed because it is too large Load diff

View file

@ -8,6 +8,8 @@ use App\Form\OfferingFormType;
use App\Repository\OfferingRepository;
use App\Repository\WishRepository;
use App\Service\PlzToCoordinate;
use App\Service\DistanceCalculator;
use Doctrine\ORM\EntityManagerInterface;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
@ -73,12 +75,27 @@ class OfferController extends AbstractController
}
#[Route('/offer/{id}', name: 'show_offer')]
public function show_offer(Offering $offer, WishRepository $wishRepository): Response
public function show_offer(Offering $offer, WishRepository $wishRepository, PlzToCoordinate $plzconverter, DistanceCalculator $distanceCalculator): Response
{
$distance = 0;
$user = $this->getUser();
$offerPlz = $offer->getZipCode();
if (isset($user))
{
$userPlz = $user->getZipCode();
}
if (isset($userPlz))
{
$distance = $distanceCalculator->calculateDistance($plzconverter->getCoordinates($offerPlz), $plzconverter->getCoordinates($userPlz));
}
return $this->render('app/offer.html.twig', [
'user' => $this->getUser(),
'user' => $user,
'offer' => $offer,
'wishes' => $wishRepository->findByUser($offer->getByUser()),
'distance' => $distance,
]);
}

View 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;
}
}

View 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;
}
}

View file

@ -106,6 +106,9 @@
"laminas/laminas-zendframework-bridge": {
"version": "1.2.0"
},
"mjaschen/phpgeo": {
"version": "3.2.1"
},
"monolog/monolog": {
"version": "2.2.0"
},
@ -342,6 +345,9 @@
"symfony/polyfill-php80": {
"version": "v1.22.1"
},
"symfony/polyfill-php81": {
"version": "v1.23.0"
},
"symfony/polyfill-uuid": {
"version": "v1.22.1"
},

View file

@ -17,17 +17,30 @@
<div class="alert alert-info" role="alert">This is your offer!</div>
{% endif %}
<h1 class="mb-3">{{ offer.title }}</h1>
<div class="show-offer-container d-flex flex-wrap">
<div class="show-img-container">
<img class="mb-3 img-fluid rounded" alt="Generic placeholder image" src="{{ asset('uploads/photos/' ~ offer.photofilename) }}">
</div>
<div class="show-offer-info">
<div class="show-offer-info w-50">
<h1 class="mb-3">{{ offer.title }}</h1>
<div class="mb-3 d-flex">
<p class="pr-3">
<i class="fas fa-user"></i>
{% if offer.byUser == user %}
Me
{% else %}
{{ offer.byUser }}
{% endif %}
</p>
<p class="pr-3">
<i class="fas fa-map-marker-alt"></i> {{ offer.zipCode }}
{% if distance > 0 %}
(ca. {{ distance }} km)
{% endif %}
</p>
</div>
<h3>Description</h3>
<p>{{ offer.description }}</p>
<h3>From</h3>
<p>{% if offer.byUser == user %}Me{% else %}{{ offer.byUser }}{% endif %} in {{ offer.zipCode }}</p>
</div>
</div>