Please note that as of October 24, 2014, the Nokia Developer Wiki will no longer be accepting user contributions, including new entries, edits and comments, as we begin transitioning to our new home, in the Windows Phone Development Wiki. We plan to move over the majority of the existing entries. Thanks for all your past and future contributions.

Revision as of 08:41, 17 July 2012 by Oskar Bukolt (Talk | contribs)

HERE Maps API - Route planning and journey plotting

From Wiki
Jump to: navigation, search

This article uses the Nokia Maps javascript API to calculate a route and display it on the map, and to display the current position of the user based on their GPS co-ordinates.

Needs-update.pngThis article needs to be updated: If you found this article useful, please fix the problems below then delete the {{ArticleNeedsUpdate}} template from the article to remove this warning.

Reasons: jasfox (18 Apr 2012)
This article refers to an outdated version 2.1.x of the API. Since version 2.2.x the syntax has changed.

Article Metadata
Code Example
Source file: gps_drive.html
Platform(s): HTML5 compliant browser
Device(s): Desktop (not Symbian^3 browser)
Dependencies: Nokia Maps 2.1.0
Keywords: Nokia Maps, JavaScript, routing
Created: jackdenial (29 Dec 2011)
Last edited: Oskar Bukolt (17 Jul 2012)

Unfolding the magic

Lets first have a car and a route ready for the journey.

// Get the DOM node to which we will append the map
var mapContainer = document.getElementById("mapContainer");
// Create a map inside the map container DOM node
var map = new, {
// Initial center and zoom level of the map
center: [71.23, 23.23],
zoomLevel: 17,
new nokia.maps.positioning.component.Positioning(),
var router = new nokia.maps.routing.Manager();
var tcoord = new nokia.maps.geo.Coordinate(71.23, 23.23)
var imageMarker = new
icon: "",
$id: "marker",
anchor: {x: 22, y: 8}

Gps car.jpg

We are now ready with the car and the route, what we need now is continuous coordinates from the GPS device to track the position of car on the map. To access GPS device we have to use nokia.maps.positioning.Manager() Nokia API.

We will be using watchPosition method to continuously get feed of the GPS sensor coordinates to position the car icon. The watchPosition (successCallback, errorCallback, options) function returns a Number Starts periodically watching the positioning sensor and fires events containing the data, if there are changes ocurring

/* The positioning manager is only available if the browser used supports
* W3C geolocation API

if (nokia.maps.positioning.Manager) {
var positioning = new nokia.maps.positioning.Manager();
// Gets the current position periodically, if available the first given callback function is executed else the second
// If a position is provided by W3C geolocation API of the browser
function (position) {
var coord = position.coords;
tcoord = coord; //set the coordinates to a temporary variable which will be further used to calculate the route.
// Something went wrong we wee unable to retrieve the GPS location
function (error) {
var errorMsg = "Location could not be determined: ";
// We determine what caused the error and generate error message
if (error.code == 1) errorMsg += "PERMISSION_DENIED";
else if (error.code == 2) errorMsg += "POSITION_UNAVAILABLE";
else if (error.code == 3) errorMsg += "TIMEOUT";
else errorMsg += "UNKNOWN_ERROR";
// Throw an alert with error message
// The function 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 nokia.maps.routing.component.RouteResultSet(routes[0]).container;
} else if (value == "failed") {
alert("The routing request failed.");
/* We create on observer on router's "state" property so the above created
* onRouteCalculated we be called once the route is calculated

router.addObserver("state", onRouteCalculated);
function update_map(){
var modes = [{
type: "shortest",
transportModes: ["car"],
options: "avoidTollroad",
trafficMode: "default"
map.set("center", tcoord);
imageMarker.set("coordinate", tcoord);
var waypoints = new nokia.maps.routing.WaypointParameterList();
waypoints.addCoordinate(new nokia.maps.geo.Coordinate(tcoord.latitude, tcoord.longitude));
waypoints.addCoordinate(new nokia.maps.geo.Coordinate(tcoord.latitude + 0.01, tcoord.longitude + 0.01));
router.calculateRoute(waypoints, modes);
// Force immediate re-render of the map display
map.update(-1, true);
window.setInterval("update_map()",300); //Map Update frequency, which can be set to a upper value. We can also set it dynamic so that when the speed is high the interval would be low and when the speed is low the interval would be high.


This demo currently only works for Desktop browsers but not mobile. We are working on it to make it compatible with Mobile Browsers.


  • In this example, we are not allowing user to set his destination. The destination automatically gets set North-East side.
137 page views in the last 30 days.