HERE Maps API - How to calculate a route between two points

Introduction

In this article we will study on how to calculate the Route between two selected points on the map, providing that a valid route can be found in all.

Prerequisites

Ovi Maps API supported web browser (basically any modern web browser)

Important about Maps credentials

With Ovi Maps API you can start without having any credentials given, but you might face a performance gap. In order to get the full potential out of the offering, you must get the credentials that authenticate your application against the Services. Please read through the Location API.

For more information on how to obtain the credentials, please start with the Ovi Maps API Developers Guide section "Acquiring API credentials"

Implementation

In this example we will define two waypoints for the route; the geographical location of Berlin, Germany to the one of Paris, France. To be able to use the routing we need to initialize a new Routing Manager. We will cast the waypoints to it with some additional parameters. We want the "shortest" route with "car" and no fee roads with "avoidTollroad" plus having the trafficMode set to "default".

Example code

This example has all the required HTML and JavaScript code to perform the routing between Berlin, Germany and Paris, France.

`<html><head><script src="http://api.maps.ovi.com/jsl.js" type="text/javascript" charset="utf-8"></script></head><div id="map" style="width:100%; height:80%;"></div><body></div></body><script type="text/javascript"> var map;             // Instance of Ovi Maps Displayvar router;          // New Route searchvar geoloc;          // New Geocoding search //Define waypoints for the Routingvar waypoints = new ovi.mapsapi.routing.WaypointParameterList(); waypoints.addCoordinate( new ovi.mapsapi.geo.Coordinate(52.500556, 13.398889));  // Berlin, Germanywaypoints.addCoordinate( new ovi.mapsapi.geo.Coordinate(48.856667, 2.350833));   // Paris, France //Define Routing parameters var modes = [{        type: "shortest",    transportModes: ["car"],    options: "avoidTollroad",    trafficMode: "default"}];  map = new ovi.mapsapi.map.Display(document.getElementById("map"), {     components: [ new ovi.mapsapi.map.component.Behavior(),                new ovi.mapsapi.map.component.ZoomBar(),                new ovi.mapsapi.map.component.Overview(),                                             new ovi.mapsapi.map.component.TypeSelector(),                     new ovi.mapsapi.map.component.ScaleBar()],    'zoomLevel': 3, });      router = new ovi.mapsapi.routing.Manager();  //create a route manager    //    map.addListener("dblclick", eventListener);   //onRouteCalculated will be called when a route was calculated var onRouteCalculated = function(observedRouter, key, value){    if (value == "finished") {        var routes = observedRouter.getRoutes();         //create the default map representation of a route        var mapRoute = new ovi.mapsapi.routing.component.RouteResultSet(routes[0]).container; //first option found        map.objects.add(mapRoute);         //Zoom to the bounding box of the route        map.zoomTo(mapRoute.getBoundingBox(), false, "default");    } else if(value == "failed") {        alert("The routing request failed.");    }}; //add the observer function to the router's "state" propertyrouter.addObserver("state", onRouteCalculated);  //calculate the route (and call onRouteCalculated afterwards)router.calculateRoute(waypoints, modes); </script></html>`

For more on Ovi Maps API

Please check out the Ovi Maps API full documentation and API reference here:

Tested with

Mozilla Firefox 5.0

423 page views in the last 30 days.