add getRoute method and format numbers returned

This commit is contained in:
Ryan Prather 2024-12-25 17:17:23 +00:00
parent 1e3a6dd612
commit d67db33ed0

View File

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