PHP Classes

PHP Openstreetmap Route API: Calculate route between two locations with OSM API

Recommend this page to a friend!
  Info   View files Example   View files View files (6)   DownloadInstall with Composer Download .zip   Reputation   Support forum (1)   Blog    
Ratings Unique User Downloads Download Rankings
Not yet rated by the usersTotal: 254 This week: 1All time: 7,869 This week: 95Up
Version License PHP version Categories
osmroute 1.0.0Freely Distributable5PHP 5, Web services, Geography


This class can calculate routes between two locations with OpenStreetMap API.

It can send HTTP requests to the OpenStreetMap API Web server to query the route between two given locations, using a given type of vehicle.

The class can return the route details like the distance to travel, the duration of the trip, trip instructions, and a list of segments that connect locations with given coordinates that form the route.

The returned route can be downloaded or saved to a file in GPX format.

Picture of Stefan Kientzler
  Performance   Level  
Name: Stefan Kientzler is available for providing paid consulting. Contact Stefan Kientzler .
Classes: 19 packages by
Country: Germany Germany
Age: 56
All time rank: 72246 in Germany Germany
Week rank: 24 Up1 in Germany Germany Up
Innovation award
Innovation award
Nominee: 11x

Winner: 6x


What is the best PHP vehicle tracking system class?
Getting distance to calculate fuel used and cost from map


use lib\OSMap\OSMapOpenRoute;
// use lib\OSMap\OSMapNominatim;

// require_once 'lib/OSMap/OSMapNominatim.php';
require_once 'lib/OSMap/OSMapPoint.php';

     * to get own API key, you must register at
     * and request new token. All further description ist found at the - page.
     * (registration is free!)
$oOR = new OSMapOpenRoute('insert your own API-Key here');
$oOR->setVehicleType(OSMapOpenRoute::VT_HGV); // we're driving heavy goods ;-)
$aRoute = array();
    // simple version with from - to points
    $ptFrom = new OSMapPoint(49.41461,8.681495);
    $ptTo = new OSMapPoint(49.420318,8.687872);
    if ($oOR->calcRoute($ptFrom, $ptTo)) {

    // determine geolocations with OSMapNominatim...
    $oOSMap = new OSMapNominatim();
    // Dulles International Airport
    $oOSMap->setStr('Dulles International Airport');
    $oOSMap->setPostcode('VA 20166');
    if ($oOSMap->searchAddress()) {
        $aRoute[] = $oOSMap->getLocation();

    // Washington Monument
    $oOSMap->setStr('Washington Monument');
    $oOSMap->setPostcode('DC 20024');
    if ($oOSMap->searchAddress()) {
        $aRoute[] = $oOSMap->getLocation();
    // George Washington Masonic National Memorial
    $oOSMap->setStr('101 Callahan Dr');
    $oOSMap->setPostcode('VA 22301');
    if ($oOSMap->searchAddress()) {
        $aRoute[] = $oOSMap->getLocation();
    // variable version: array may contain more than two points
    // coordinates may be as comma separated string lat, lon or object from class OSMapPoint, if available as single values
$aRoute[] = '38.95226625, -77.45342297783296'; // Dulles International Airport
$aRoute[] = '38.889483150000004, -77.03524967010638'; // Washington Monument
$aRoute[] = new OSMapPoint(38.80746845, -77.06596192040345); // George Washington Masonic National Memorial
if ($oOR->calcRoute($aRoute)) {
'Distance: ' . $oOR->getDistance() . $oOR->getUnits() . '<br/>';
'Duration: ' . $oOR->getDuration() . 's<br/>';
$iCnt = $oOR->getSegmentCount();
'Segment Count: ' . $iCnt . '<br/>';
        for (
$iSeg = 0; $iSeg < $iCnt; $iSeg++) {
'&nbsp;&nbsp;&nbsp;Segment ' . ($iSeg + 1) . '<br/>';
'&nbsp;&nbsp;&nbsp;Distance: ' . $oOR->getDistance($iSeg) . $oOR->getUnits() . '<br/>';
'&nbsp;&nbsp;&nbsp;Duration: ' . $oOR->getDuration($iSeg) . 's<br/>';
$iSteps = $oOR->getStepCount($iSeg);
'&nbsp;&nbsp;&nbsp;Step Count: ' . $iSteps . '<br/>';
            for (
$iStep = 0; $iStep < $iSteps; $iStep++) {
$oStep = $oOR->getStep($iSeg, $iStep);
                if (
$oStep) {
'&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;' . $oStep->getInstruction() . '<br/>';
// save on file
        // $oOR->saveRoute();
} else {

  Files folder image Files  
File Role Description
Files folder imagelib (1 directory)
Accessible without login Plain text file ExampleRoute.gpx Output Example GPX output for calculated route
Accessible without login Plain text file ExampleRoute.txt Output excample for calculated route
Accessible without login Plain text file OSMapOpenRouteTest.php Example script demonstrating usage

  Files folder image Files  /  lib  
File Role Description
Files folder imageOSMap (3 files)

  Files folder image Files  /  lib  /  OSMap  
File Role Description
  Plain text file OSMapOpenRoute.php Class main class of package
  Plain text file OSMapOpenRouteStep.php Class helper class
  Plain text file OSMapPoint.php Class helper class 17KB
Downloadosmroute-2020-03-29.tar.gz 16KB
Install with ComposerInstall with Composer
Needed packages  
Class DownloadWhy it is needed Dependency
OpenStreetMap Nominatim PHP Class Download .zip .tar.gz to determine geolocations for Adresses Optional
 Version Control Unique User Downloads Download Rankings  
This week:1
All time:7,869
This week:95Up
User Comments (1)