From d67db33ed0ff5d8256feab333e41013ecd6740d4 Mon Sep 17 00:00:00 2001 From: Ryan Prather Date: Wed, 25 Dec 2024 17:17:23 +0000 Subject: [PATCH] add getRoute method and format numbers returned --- src/Libs/Libs.php | 30 +++++++++++++++++++++++++++++- 1 file changed, 29 insertions(+), 1 deletion(-) diff --git a/src/Libs/Libs.php b/src/Libs/Libs.php index a19f6d3..5bcc071 100644 --- a/src/Libs/Libs.php +++ b/src/Libs/Libs.php @@ -19,7 +19,35 @@ class Libs if (isset($result['results'][0]['lat']) && isset($result['results'][0]['lon'])) { $lat = $result['results'][0]['lat']; $lon = $result['results'][0]['lon']; - return [$lat, $lon]; + return [number_format($lat, 6), number_format($lon, 6)]; + } + return null; + } + + public static function getRoute($lat1, $lon1, $lat2, $lon2): ?array + { + $params = [ + 'waypoints' => "{$lat1},{$lon1}|{$lat2},{$lon2}", + 'mode' => 'drive', + 'units' => 'imperial', + 'format' => 'json', + '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); + curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); + curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0); + 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']; + return $route; } return null; }