×
Namespaces

Variants
Actions
(Difference between revisions)

HERE Maps API - Route planning and journey plotting

From Nokia Developer Wiki
Jump to: navigation, search
jackdenial (Talk | contribs)
hamishwillee (Talk | contribs)
m (Hamishwillee - Remove "done" items)
Line 144: Line 144:
 
==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.
* In this example, the route of journey wont be recalculated. It will stay as it is. We should make the script in such a way that the first point of route should be the car (GPS location) and the rest of the points should be as it is. ('''DONE''')
 
* As the car moves, periodically the route should be recalculated after a specific period of time (say 1 minute to reduce data transfer) ('''DONE''')
 

Revision as of 00:30, 4 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
Dependencies: Nokia Maps 2.1.0
Article
Keywords: Nokia Maps
Created: jackdenial (29 Dec 2011)
Last edited: hamishwillee (04 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,
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

TODOs

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