DEV Community

Cover image for How to retrieve the fastest walking route using HERE Routing API
Roberto B.
Roberto B.

Posted on

How to retrieve the fastest walking route using HERE Routing API

With this short article I would like to show you how to calculate routes with PHP language.
We are going to use HERE APIs so for that you need to have a *Free*mium account https://developer.here.com/. There you can obtain your API KEY for REST API, that we will use later.
In order to make our life easier we are going to use Milk SDK PHP library. This library is not the official SDK, it is part of my personal side project and I'm writing this SDK to consume HERE APIs to calculate route, to retrieve weather forecast etc.

Now, it is time to jump on the code...

Create your new directory:

mkdir  milk-sdk-example
cd milk-sdk-example
Enter fullscreen mode Exit fullscreen mode

In the new directory, create a new composer.json file like this one:

{
    "name": "milk-sdk-example",
    "require": {}
}
Enter fullscreen mode Exit fullscreen mode

The you can instal a PHP SDK:

composer require rbit/milk-sdk-php
Enter fullscreen mode Exit fullscreen mode

Now you can define your .env file, and fill HERE_API_KEY with your API KEY:

XYZ_ACCESS_TOKEN="your_access_token"
HERE_API_KEY="your_access_token"
Enter fullscreen mode Exit fullscreen mode

Create an index.php file

<?php

// 001 Autoload for yuor packages
require __DIR__ . "/vendor/autoload.php";
// 002 Use the Routing class
use Rbit\Milk\HRestApi\Routing\Routing;
// 003 load your env file and the APP KEY
Dotenv\Dotenv::createImmutable(__DIR__)->load();
$hereApiKey = getenv('HERE_API_KEY');

// 004 Instance Routing class and get instructions
// for the fastest walking route
$r =Routing::instance($hereApiKey)
  ->byFoot()
  ->typeFastest()
  ->startingPoint(52.5160,13.3779)
  ->destination(52.5185,13.4283)
  ->getManeuverInstructions(); 

// 005 parse and list the instructions
foreach ($r as $key => $value) {
  echo strip_tags($value->instruction) . PHP_EOL;
}

Enter fullscreen mode Exit fullscreen mode

Let me walk through the code:

  • 001: Autoload all packages;
  • 002: use the Routing class;
  • 003: load your env file and your HERE Api key;
  • 004: do the magic ;) instance Routing class and get instructions for the fastest walking route;
  • 005: list the instructions (i stripped some html tags in the instruction via strip_tags function)

Now you can calculate your route:

php index.php
Enter fullscreen mode Exit fullscreen mode

Here the result:

Head north on Pariser Platz. Go for 33 m.
Turn slightly right onto Pariser Platz. Go for 1.4 km.
Take the street in the middle, Am Lustgarten. Go for 273 m.
Turn right. Go for 19 m.
Turn left. Go for 18 m.
Turn left. Go for 185 m.
Turn left. Go for 10 m.
Turn right onto Spandauer StraรŸe. Go for 39 m.
Turn left onto Platz. Go for 70 m.
Head northeast. Go for 24 m.
Turn slightly right. Go for 12 m.
Turn slightly right. Go for 120 m.
Turn left onto RathausstraรŸe. Go for 548 m.
Turn right onto Alexanderplatz. Go for 962 m.
Walk right around the roundabout and turn onto Strausberger Platz. Go for 39 m.
Arrive at Strausberger Platz. Your destination is on the left.
Enter fullscreen mode Exit fullscreen mode

Alt Text

How easy was it? :)

Top comments (2)

Collapse
 
jonrandy profile image
Jon Randy ๐ŸŽ–๏ธ • Edited

A bit misleading... I was actually expecting an example of how to use Dijkstra's algorithm, rather than an example that does no calculation and just shows how to call a library that wraps an API

Collapse
 
robertobutti profile image
Roberto B.

Thank you for your feedback. I'm going to change the title.