Files
Siro/backend/core/osrm_routing.php
T

72 lines
2.3 KiB
PHP

<?php
// ============================================================
// osrm_routing.php
// Helper functions for OpenStreetMap Routing Machine (OSRM)
// Used to estimate accurate distance (km) and duration (mins)
// ============================================================
/**
* Get Distance and Duration between two coordinates using public OSRM server.
* Note: If you host your own OSRM, replace the $osrmBaseUrl.
*
* @param float $startLat
* @param float $startLng
* @param float $endLat
* @param float $endLng
* @param string $countryCode (e.g. 'JO', 'SY', 'EG')
* @return array|null Returns ['distance_km' => float, 'duration_min' => float] or null on failure.
*/
function getOsrmRouteDetails($startLat, $startLng, $endLat, $endLng, $countryCode = 'JO') {
// Select Intaleq Maps SaaS server based on country
switch (strtoupper($countryCode)) {
case 'SY':
$baseUrl = "https://map-syria.siromove.com/api/maps/route";
break;
case 'EG':
$baseUrl = "https://map-egypt.siromove.com/api/maps/route";
break;
case 'JO':
default:
$baseUrl = "https://map-saas.intaleqapp.com/api/maps/route";
break;
}
$queryParams = http_build_query([
'fromLat' => $startLat,
'fromLng' => $startLng,
'toLat' => $endLat,
'toLng' => $endLng
]);
$url = "{$baseUrl}?{$queryParams}";
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_TIMEOUT, 5); // 5 seconds timeout
// Add Intaleq API Key Header
curl_setopt($ch, CURLOPT_HTTPHEADER, [
"x-api-key: in_9478b32836d19cff73db3063"
]);
$response = curl_exec($ch);
$httpCode = curl_getinfo($ch, CURLINFO_HTTP_CODE);
curl_close($ch);
if ($httpCode === 200 && $response) {
$data = json_decode($response, true);
if (isset($data['distance']) && isset($data['duration'])) {
$distanceMeters = (float)$data['distance'];
$durationSeconds = (float)$data['duration'];
return [
'distance_km' => round($distanceMeters / 1000, 2),
'duration_min' => round($durationSeconds / 60, 2)
];
}
}
return null;
}