<?php
$start = "12.971598,77.594566"; // Bangalore coordinates
$end = "13.082680,80.270718"; // Chennai coordinates
// OSRM API endpoint
$url = "http://router.project-osrm.org/route/v1/driving/$start;$end?overview=false";
// Send GET request to OSRM API
$response = file_get_contents($url);
$data = json_decode($response, true);
// Check for a valid response
if (isset($data['routes'][0])) {
$distance = $data['routes'][0]['distance']; // Distance in meters
$duration = $data['routes'][0]['duration']; // Duration in seconds
// Convert distance to kilometers and duration to minutes
$distanceInKm = $distance / 1000;
$durationInMinutes = $duration / 60;
echo "Distance: " . round($distanceInKm, 2) . " km\n";
echo "Duration: " . round($durationInMinutes, 2) . " minutes\n";
} else {
echo "Error: Unable to calculate route.";
}
?>
0 comments:
Post a Comment