×
Namespaces

Variants
Actions
(Difference between revisions)

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

From Nokia Developer Wiki
Jump to: navigation, search
jasfox (Talk | contribs)
m (Jasfox - update to 2.2.4)
jasfox (Talk | contribs)
m (Jasfox - - For more on the HERE Maps API)
 
(11 intermediate revisions by 2 users not shown)
Line 1: Line 1:
[[Category:Nokia Maps]][[Category:Code Snippet]][[Category:JavaScript]]
+
[[Category:HERE Maps]][[Category:JavaScript]][[Category:Code Examples]]
{{Abstract|This article explains how to geo-code two locations and show the route between them using Nokia Maps (providing that a valid route can be found). }}
+
{{Abstract|This article explains how to geo-code two locations and show the route between them using HERE Maps (providing that a valid route can be found). }}
  
{{ArticleMetaData
+
{{ArticleMetaData <!-- v1.2 -->
|sourcecode= <!-- Link to example source code e.g. [[Media:The Code Example ZIP.zip]] -->
+
|sourcecode=  [https://github.com/heremaps/examples/blob/master/maps_api_for_javascript/simple-examples/routing-from-a-to-b.html routing-from-a-to-b.html]
 
|installfile= <!-- Link to installation file (e.g. [[Media:The Installation File.sis]]) -->
 
|installfile= <!-- Link to installation file (e.g. [[Media:The Installation File.sis]]) -->
 
|devices= Internet Explorer, Google chrome, Firefox, Opera
 
|devices= Internet Explorer, Google chrome, Firefox, Opera
Line 9: Line 9:
 
|platform= Web browser
 
|platform= Web browser
 
|devicecompatability= <!-- Compatible devices e.g.: All* (must have internal GPS) -->
 
|devicecompatability= <!-- Compatible devices e.g.: All* (must have internal GPS) -->
|dependencies=Nokia Maps 2.2.4
+
|dependencies= HERE Maps 2.5.3
|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, JavaScript, routing
+
|keywords= HERE Maps, JavaScript, routing
|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 -->
 
|translated-by= <!-- [[User:XXXX]] -->
 
|translated-by= <!-- [[User:XXXX]] -->
|translated-from-title= <!-- Title only -->  
+
|translated-from-title= <!-- Title only -->
 
|translated-from-id= <!-- Id of translated revision -->
 
|translated-from-id= <!-- Id of translated revision -->
|review-by=<!-- After re-review: [[User:username]] -->
+
|review-by= <!-- After re-review: [[User:username]] -->
 
|review-timestamp= <!-- After re-review: YYYYMMDD -->
 
|review-timestamp= <!-- After re-review: YYYYMMDD -->
|update-by=[[User:avnee.nathani]]
+
|update-by= [[User:avnee.nathani]]
|update-timestamp=20111231
+
|update-timestamp= 20111231
|creationdate=20110621
+
|creationdate= 20110621
|author=[[User:Maveric]]
+
|author= [[User:Maveric]]
 
}}
 
}}
  
 
{{SeeAlso|
 
{{SeeAlso|
* [http://developer.here.net/javascript_api Nokia Maps API]
+
* [http://developer.here.net/javascript_api HERE Maps API]
*  [[Nokia Maps API - Advanced Routing]]
+
* [http://heremaps.github.io/examples/index.html HERE Maps on Github]
 +
*  [[HERE Maps API - Advanced Routing]]
 
*  [http://developer.here.net/apiexplorer/examples/api-for-js/routing/map-with-route-from-a-to-b.html A to B routing example]
 
*  [http://developer.here.net/apiexplorer/examples/api-for-js/routing/map-with-route-from-a-to-b.html A to B routing example]
 
}}
 
}}
Line 36: Line 36:
 
==Prerequisites==
 
==Prerequisites==
  
Nokia Maps API supported web browser (basically any modern web browser)
+
HERE Maps API supported web browser (basically any modern web browser)
  
 
==Important note about maps credentials==
 
==Important note about maps credentials==
  
Nokia provides several services options within the Maps API offering. The service is free to use, but you must obtain and use authentication and authorization credentials to use the services. Please read the
+
Nokia provides several services options within the Maps API offering. The service is free to use, but you must obtain and use authentication and authorization credentials to use the services. Please read the
 
[http://developer.here.net/terms_conditions Terms and Conditions] and check the [http://developer.here.net/web/guest/plans Pricing Plans page]  to decide which business model best fits your needs. Authentication requires unique Maps API credentials, namely an AppId and a token. You can get these credentials free for free following the instructions  [http://developer.here.net/docs/maps_js/topics/credentials.html#acquiring-credentials here]
 
[http://developer.here.net/terms_conditions Terms and Conditions] and check the [http://developer.here.net/web/guest/plans Pricing Plans page]  to decide which business model best fits your needs. Authentication requires unique Maps API credentials, namely an AppId and a token. You can get these credentials free for free following the instructions  [http://developer.here.net/docs/maps_js/topics/credentials.html#acquiring-credentials here]
  
Line 54: Line 54:
 
{{Note|In order to use routing services, the {{Icode|with}} parameter of the script element's {{Icode|src}} attribute must be set to {{Icode|?with&#61;all}} }}
 
{{Note|In order to use routing services, the {{Icode|with}} parameter of the script element's {{Icode|src}} attribute must be set to {{Icode|?with&#61;all}} }}
  
<code java>
+
<code javascript>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
+
function makeRouteRequest(){
<html xmlns="http://www.w3.org/1999/xhtml">
+
var waypoints = new nokia.maps.routing.WaypointParameterList();
    <head>
+
      <title>A to B Routing Example</title>
+
waypoints.addCoordinate(
      <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
+
new nokia.maps.geo.Coordinate(52.500556, 13.398889));  // Berlin, Germany
      <script type="text/javascript"
+
waypoints.addCoordinate(
            src="http://api.maps.nokia.com/2.2.4/jsl.js?with=all" charset="utf-8">
+
new nokia.maps.geo.Coordinate(48.856667, 2.350833));  // Paris, France
        </script>
+
    </head>
+
var modes = [{   
    <body>
+
type: "shortest",
 +
transportModes: ["car"],
 +
options: "avoidTollroad",
 +
trafficMode: "default"
 +
}];
  
        <div id="map" style="z-index: -1; left:0px; top:0px; width: 100%; height: 80%; position: absolute;"></div>
+
router = new nokia.maps.routing.Manager();  
        <script type="text/javascript">
+
router.addObserver("state", onRouteCalculated);
/////////////////////////////////////////////////////////////////////////////////////
+
router.calculateRoute(waypoints, modes);
// Don't forget to set your API credentials
+
}
//
+
</code>
// Replace with your appId and token which you can obtain when you
+
<code javascript>
// register on http://api.developer.nokia.com/
+
function onRouteCalculated(observedRouter, key, value){
//
+
if (value == "finished") {
nokia.Settings.set( "appId", "YOUR APP ID GOES HERE");
+
var routes = observedRouter.getRoutes();
nokia.Settings.set( "authenticationToken", "YOUR AUTHENTICATION TOKEN GOES HERE");
+
 
+
//create the default map representation of a route
//
+
var mapRoute =  
/////////////////////////////////////////////////////////////////////////////////////
+
new nokia.maps.routing.component.RouteResultSet(
var map;            // Instance of Nokia Maps Display
+
routes[0]).container; //first option found
var router;          // New Route search
+
map.objects.add(mapRoute);
var geoloc;          // New Geocoding search
+
+
//Zoom to the bounding box of the route
//Define waypoints for the Routing
+
map.zoomTo(mapRoute.getBoundingBox(), false, "default");
var waypoints = new nokia.maps.routing.WaypointParameterList();
+
} else if(value == "failed") {
+
alert("The routing request failed.");
waypoints.addCoordinate( new nokia.maps.geo.Coordinate(52.500556, 13.398889));  // Berlin, Germany
+
}
waypoints.addCoordinate( new nokia.maps.geo.Coordinate(48.856667, 2.350833));  // Paris, France
+
}
+
//Define Routing parameters
+
+
var modes = [{   
+
    type: "shortest",
+
    transportModes: ["car"],
+
    options: "avoidTollroad",
+
    trafficMode: "default"
+
}];
+
+
+
map = new nokia.maps.map.Display(document.getElementById("map"),
+
{
+
+
    components: [ new nokia.maps.map.component.Behavior(),
+
                new nokia.maps.map.component.ZoomBar(),
+
                new nokia.maps.map.component.Overview(),                           
+
                new nokia.maps.map.component.TypeSelector(),   
+
                new nokia.maps.map.component.ScaleBar()],
+
    'zoomLevel': 3,
+
});
+
+
+
    router = new nokia.maps.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 nokia.maps.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" property
+
router.addObserver("state", onRouteCalculated);
+
+
//calculate the route (and call onRouteCalculated afterwards)
+
router.calculateRoute(waypoints, modes);
+
</script>
+
</body>
+
</html>
+
 
</code>
 
</code>
  
== Screenshot==
+
 
 
[[File:NokiaMapsRoute.png]]
 
[[File:NokiaMapsRoute.png]]
  
==For more on the Nokia Maps API==
+
A working example can be found at:
  
Please check out the Nokia Maps API full documentation and API reference here:
+
http://heremaps.github.io/examples/examples.html#routing-from-a-to-b
* [http://developer.here.net/javascript_api Nokia Maps API]
+
 
 +
==For more on the HERE Maps API==
 +
 
 +
Please check out the HERE Maps API full documentation and API reference here:
 +
* [http://developer.here.com/javascript_api HERE Maps API]
  
 
You may also access the interactive API explorer
 
You may also access the interactive API explorer
* [http://developer.here.net/javascript_api_explorer API explorer]
+
* [http://developer.here.com/javascript_api_explorer API explorer]
 +
 
 +
And additional examples on GitHub
 +
* [http://heremaps.github.io/examples/ HERE Maps on Github]

Latest revision as of 17:29, 12 March 2014

This article explains how to geo-code two locations and show the route between them using HERE Maps (providing that a valid route can be found).

Article Metadata
Code ExampleTested with
Devices(s): Internet Explorer, Google chrome, Firefox, Opera
Compatibility
Platform(s): Web browser
Dependencies: HERE Maps 2.5.3
Article
Keywords: HERE Maps, JavaScript, routing
Created: Maveric (21 Jun 2011)
Updated: avnee.nathani (31 Dec 2011)
Last edited: jasfox (12 Mar 2014)


Contents

[edit] Prerequisites

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

[edit] Important note about maps credentials

Nokia provides several services options within the Maps API offering. The service is free to use, but you must obtain and use authentication and authorization credentials to use the services. Please read the Terms and Conditions and check the Pricing Plans page to decide which business model best fits your needs. Authentication requires unique Maps API credentials, namely an AppId and a token. You can get these credentials free for free following the instructions here

[edit] 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 routing we need to initialize a new nokia.maps.routing.Manager. We will cast the waypoints to it with some additional parameters. We want the shortest route with car and no toll roads with avoidTollroad, plus having the trafficMode set to default.

[edit] Example code

This example has all the required HTML and JavaScript code to perform the routing between Berlin, Germany and Paris, France. Remember to add in your own AppId and Token.

Note.pngNote: In order to use routing services, the with parameter of the script element's src attribute must be set to ?with=all

function makeRouteRequest(){
var waypoints = new nokia.maps.routing.WaypointParameterList();
 
waypoints.addCoordinate(
new nokia.maps.geo.Coordinate(52.500556, 13.398889)); // Berlin, Germany
waypoints.addCoordinate(
new nokia.maps.geo.Coordinate(48.856667, 2.350833)); // Paris, France
 
var modes = [{
type: "shortest",
transportModes: ["car"],
options: "avoidTollroad",
trafficMode: "default"
}];
 
router = new nokia.maps.routing.Manager();
router.addObserver("state", onRouteCalculated);
router.calculateRoute(waypoints, modes);
}
function onRouteCalculated(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; //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.");
}
}


NokiaMapsRoute.png

A working example can be found at:

http://heremaps.github.io/examples/examples.html#routing-from-a-to-b

[edit] For more on the HERE Maps API

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

You may also access the interactive API explorer

And additional examples on GitHub

This page was last modified on 12 March 2014, at 17:29.
236 page views in the last 30 days.

Was this page helpful?

Your feedback about this content is important. Let us know what you think.

 

Thank you!

We appreciate your feedback.

×