×
Namespaces

Variants
Actions
(Difference between revisions)

HERE Maps API - Route planning and journey plotting

From Nokia Developer Wiki
Jump to: navigation, search
jasfox (Talk | contribs)
m (Jasfox - link.)
Oskar Bukolt (Talk | contribs)
m (Oskar Bukolt - - Demo)
(One intermediate revision by one user not shown)
Line 14: Line 14:
 
|signing=<!-- Signing requirements - empty or one of: Self-Signed, DevCert, Manufacturer -->
 
|signing=<!-- Signing requirements - empty or one of: Self-Signed, DevCert, Manufacturer -->
 
|capabilities=<!-- Capabilities required by the article/code example (e.g. Location, NetworkServices. -->
 
|capabilities=<!-- Capabilities required by the article/code example (e.g. Location, NetworkServices. -->
|keywords= Nokia Maps
+
|keywords= Nokia Maps, JavaScript, routing
 
|id= <!-- Article Id (Knowledge base articles only) -->
 
|id= <!-- Article Id (Knowledge base articles only) -->
 
|language= <!-- Language category code for non-English topics - e.g. Lang-Chinese -->
 
|language= <!-- Language category code for non-English topics - e.g. Lang-Chinese -->
Line 149: Line 149:
 
==Demo==
 
==Demo==
 
* http://planmyroute.info/gps_drive.html
 
* http://planmyroute.info/gps_drive.html
'''This demo currently only works for Desktop browsers but mobile. We are working on it to make it compatible with Mobile Browsers.'''
+
'''This demo currently only works for Desktop browsers but not mobile. We are working on it to make it compatible with Mobile Browsers.'''
  
 
==TODOs==
 
==TODOs==
 
* In this example, we are not allowing user to set his destination. The destination automatically gets set North-East side.
 
* In this example, we are not allowing user to set his destination. The destination automatically gets set North-East side.

Revision as of 11:41, 17 July 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.

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
Compatibility
Platform(s): HTML5 compliant browser
Device(s): Desktop (not Symbian^3 browser)
Dependencies: Nokia Maps 2.1.0
Article
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 nokia.maps.map.Display(mapContainer, {
// Initial center and zoom level of the map
center: [71.23, 23.23],
zoomLevel: 17,
components:[
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(
tcoord,
{
icon: "http://www.worcestershire.gov.uk/cms/images/car.png",
$id: "marker",
anchor: {x: 22, y: 8}
}
);
 
map.objects.addAll([imageMarker]);

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
positioning.watchPosition(
// 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
alert(errorMsg);
}
);
}
 
// 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;
map.objects.add(mapRoute);
 
} 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.

Demo

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

TODOs

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