first draft of case addresses and itineraries
This commit is contained in:
@@ -2,6 +2,8 @@
|
||||
|
||||
namespace App\Libs;
|
||||
|
||||
use App\Entity\Location;
|
||||
|
||||
class Libs
|
||||
{
|
||||
public static function getLatLonFromGeoapify($address): ?array
|
||||
@@ -24,7 +26,7 @@ class Libs
|
||||
return null;
|
||||
}
|
||||
|
||||
public static function getRoute($lat1, $lon1, $lat2, $lon2): ?array
|
||||
public static function getRoute($lat1, $lon1, $lat2, $lon2): ?Route
|
||||
{
|
||||
$params = [
|
||||
'waypoints' => "{$lat1},{$lon1}|{$lat2},{$lon2}",
|
||||
@@ -34,7 +36,6 @@ class Libs
|
||||
'apiKey' => $_ENV['GEOAPIFY_API_KEY']
|
||||
];
|
||||
|
||||
|
||||
$url = "https://api.geoapify.com/v1/routing?".http_build_query($params);
|
||||
$ch = curl_init();
|
||||
curl_setopt($ch, CURLOPT_URL, $url);
|
||||
@@ -43,10 +44,18 @@ class Libs
|
||||
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);
|
||||
$result = curl_exec($ch);
|
||||
curl_close($ch);
|
||||
$result = json_decode($result, true);
|
||||
dd($result);
|
||||
if (isset($result['features'][0]['geometry']['coordinates'])) {
|
||||
$route = $result['features'][0]['geometry']['coordinates'];
|
||||
$route = new Route(json_decode($result));
|
||||
|
||||
if (is_a($route, Route::class)) {
|
||||
return $route;
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
public static function getRouteDistance(Location $origin, Location $destination): ?Route
|
||||
{
|
||||
$route = self::getRoute($origin->getLat(), $origin->getLon(), $destination->getLat(), $destination->getLon());
|
||||
if ($route) {
|
||||
return $route;
|
||||
}
|
||||
return null;
|
||||
|
47
src/Libs/Route.php
Normal file
47
src/Libs/Route.php
Normal file
@@ -0,0 +1,47 @@
|
||||
<?php
|
||||
|
||||
namespace App\Libs;
|
||||
|
||||
use DateInterval;
|
||||
use DateTime;
|
||||
|
||||
class Route
|
||||
{
|
||||
private float $distance;
|
||||
|
||||
private float $duration;
|
||||
|
||||
private array $geometry;
|
||||
|
||||
public function __construct(
|
||||
object $route
|
||||
) {
|
||||
$this->distance = $route->results[0]->distance;
|
||||
$this->duration = $route->results[0]->time;
|
||||
$this->geometry = $route->results[0]->geometry[0];
|
||||
}
|
||||
|
||||
public function getDuration(): ?DateInterval
|
||||
{
|
||||
$startDate = new DateTime("@0");
|
||||
$endDate = new DateTime("@{$this->duration}");
|
||||
$dur = $startDate->diff($endDate);
|
||||
|
||||
return $dur;
|
||||
}
|
||||
|
||||
public function getDurationString(): string
|
||||
{
|
||||
return $this->getDuration()->format('%H:%I:%S');
|
||||
}
|
||||
|
||||
public function getDistance(): float
|
||||
{
|
||||
return $this->distance;
|
||||
}
|
||||
|
||||
public function getGeometry(): array
|
||||
{
|
||||
return $this->geometry;
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user