The Open Source Routing Machine (OSRM) is a high-performance routing engine used to calculate routes, distances, and travel times using OpenStreetMap data.
sudo apt update && sudo apt upgrade -y
sudo apt install build-essential git cmake pkg-config \
libbz2-dev libstxxl-dev libstxxl1v5 libxml2-dev \
libzip-dev libboost-all-dev lua5.3 liblua5.3-dev -y
git clone https://github.com/Project-OSRM/osrm-backend.git
cd osrm-backend
mkdir build && cd build
cmake ..
make -j4
./osrm-extract --help
https://download.geofabrik.de/europe/germany-latest.osm.pbf
mv germany-latest.osm.pbf ~/osrm-backend/
cd ~/osrm-backend
./build/osrm-extract germany-latest.osm.pbf -p profiles/car.lua
./build/osrm-partition germany-latest.osrm
./build/osrm-customize germany-latest.osrm
./build/osrm-routed germany-latest.osrm
Open in browser:
You can test API from address:
http://localhost:5000/route/v1/driving/13.388860,52.517037;13.397634,52.529407
You should get JSON response
$url = "http://localhost:5000/route/v1/driving/$lon1,$lat1;$lon2,$lat2";
$json = file_get_contents($url);
$data = json_decode($json, true);
$distance = $data['routes'][0]['distance'];
$duration = $data['routes'][0]['duration'];
$km = $distance / 1000;
$minutes = $duration / 60;