(Difference between revisions)

HERE Maps API - Route planning and journey plotting

From Nokia Developer Wiki
Jump to: navigation, search
hamishwillee (Talk | contribs)
m (Hamishwillee - Add platform information to ArticleMetaData)
hamishwillee (Talk | contribs)
m (Hamishwillee - Add JavaScript to allow mapping of Nokia Maps + JavaScript)
Line 1: Line 1:
[[Category:Nokia Maps]][[Category:Code Snippet]][[Category:Code Examples]][[Category:Web Services]]
[[Category:Nokia Maps]][[Category:Code Examples]][[Category:JavaScript]]
{{Abstract|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.}}
{{Abstract|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.}}

Revision as of 07:01, 26 January 2012

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.

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
Created: jackdenial (29 Dec 2011)
Last edited: hamishwillee (26 Jan 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 nokia.maps.map.Display(mapContainer, {
// Initial center and zoom level of the map
center: [71.23, 23.23],
zoomLevel: 17,
new nokia.maps.map.component.ZoomBar(),
new nokia.maps.map.component.TypeSelector(),
new nokia.maps.positioning.component.Positioning(),
new nokia.maps.map.component.Behavior()
var router = new nokia.maps.routing.Manager();
var tcoord = new nokia.maps.geo.Coordinate(71.23, 23.23)
var imageMarker = new nokia.maps.map.Marker(
icon: "http://www.worcestershire.gov.uk/cms/images/car.png",
$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 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.
277 page views in the last 30 days.