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   Blog    
Ratings Unique User Downloads Download Rankings
Not yet rated by the usersTotal: 247 All time: 7,939 This week: 129Up
Version License PHP version Categories
osmroute 1.0.0Freely Distributable5PHP 5, Web services, Geography
Description 

Author

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: 18 packages by
Country: Germany Germany
Age: 56
All time rank: 73247 in Germany Germany
Week rank: 14 Up1 in Germany Germany Up
Innovation award
Innovation award
Nominee: 11x

Winner: 6x

Recommendations

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

Example

<?php
use lib\OSMap\OSMapOpenRoute;
use
lib\OSMap\OSMapPoint;
// use lib\OSMap\OSMapNominatim;

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

   
/**
     * to get own API key, you must register at
     * https://openrouteservice.org/dev/#/home
     * and request new token. All further description ist found at the openrouteservice.org - page.
     * (registration is free!)
     */
   
$oOR = new OSMapOpenRoute('insert your own API-Key here');
   
   
$oOR->setLanguage('EN');
   
$oOR->setVehicleType(OSMapOpenRoute::VT_HGV); // we're driving heavy goods ;-)
   
$oOR->setFormat(OSMapOpenRoute::FMT_JSON);
   
$oOR->enableInstructions();
   
$oOR->setInstructionFormat(OSMapOpenRoute::IF_HTML);
   
   
$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->reset();
    $oOSMap->setStr('Washington Monument');
    $oOSMap->setPostcode('DC 20024');
    $oOSMap->setCity('Washington');
    if ($oOSMap->searchAddress()) {
        $aRoute[] = $oOSMap->getLocation();
    }
   
    // George Washington Masonic National Memorial
    $oOSMap->reset();
    $oOSMap->setStr('101 Callahan Dr');
    $oOSMap->setPostcode('VA 22301');
    $oOSMap->setCity('Alexandria');
    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)) {
        echo
'Distance: ' . $oOR->getDistance() . $oOR->getUnits() . '<br/>';
        echo
'Duration: ' . $oOR->getDuration() . 's<br/>';
       
$iCnt = $oOR->getSegmentCount();
        echo
'Segment Count: ' . $iCnt . '<br/>';
        for (
$iSeg = 0; $iSeg < $iCnt; $iSeg++) {
            echo
'&nbsp;&nbsp;&nbsp;Segment ' . ($iSeg + 1) . '<br/>';
            echo
'&nbsp;&nbsp;&nbsp;Distance: ' . $oOR->getDistance($iSeg) . $oOR->getUnits() . '<br/>';
            echo
'&nbsp;&nbsp;&nbsp;Duration: ' . $oOR->getDuration($iSeg) . 's<br/>';
           
$iSteps = $oOR->getStepCount($iSeg);
            echo
'&nbsp;&nbsp;&nbsp;Step Count: ' . $iSteps . '<br/>';
            for (
$iStep = 0; $iStep < $iSteps; $iStep++) {
               
$oStep = $oOR->getStep($iSeg, $iStep);
                if (
$oStep) {
                    echo
'&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;' . $oStep->getInstruction() . '<br/>';
                }
            }
            echo
'<br/>';
        }
       
// save on file
        // $oOR->saveRoute();
   
} else {
        echo
$oOR->getError();
    }


  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

Downloadosmroute-2020-03-29.zip 17KB
Downloadosmroute-2020-03-29.tar.gz
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  
 0%
Total:247
This week:0
All time:7,939
This week:129Up