×

Discussion Board

Results 1 to 6 of 6
  1. #1
    Registered User
    Join Date
    May 2012
    Posts
    43

    Question How obtain the length of a route?

    Hello, how can i get the length of a route?

    I have been looking at this basic code of a route from a to b:
    Code:
    // 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: [52.51, 13.4],
    		zoomLevel: 7,
    		// We add the behavior component to allow panning / zooming of the map
    		components:[new nokia.maps.map.component.Behavior()]
    	}),
    	router = new nokia.maps.routing.Manager(); // create a route manager;
    	
    // 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);
    			
    			//Zoom to the bounding box of the route
    			map.zoomTo(mapRoute.getBoundingBox(), false, "default");
    		} 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);
    
    // Create waypoints
    var waypoints = new nokia.maps.routing.WaypointParameterList();
    waypoints.addCoordinate(new nokia.maps.geo.Coordinate(52.51652540955727, 13.380154923889933));
    waypoints.addCoordinate(new nokia.maps.geo.Coordinate(52.52114106145058, 13.40921934080231));
    
    /* Properties such as type, transportModes, options, trafficMode can be
     * specified as second parameter in performing the routing request.
     * 
     * See for the mode options the "nokia.maps.routing.Mode" section in the developer's guide
     */
    var modes = [{
    	type: "shortest", 
    	transportModes: ["car"],
    	options: "avoidTollroad",
    	trafficMode: "default"
    }];
    
    // Calculate the route (and call onRouteCalculated afterwards)
    router.calculateRoute(waypoints, modes);
    i found this example for Maps API for JavaME:

    http://www.developer.nokia.com/Commu...ance-the-route

    but i could not do it work for javascript api, im gonna wait for your answer!
    Last edited by jasfox; 2013-01-28 at 08:04.

  2. #2
    Nokia Developer Moderator
    Join Date
    Mar 2003
    Location
    Lempäälä/Finland
    Posts
    29,174

    Re: How obtain the length of a route?

    Basically you can not mix different implementation technologies, so newer ever expect Java Maps API examples to work with Javascript implementation. I would suggest that you would visit: http://developer.here.net/javascript_api to find java script API.

  3. #3
    Registered User
    Join Date
    May 2012
    Posts
    43

    Re: How obtain the length of a route?

    i know i cannot mix different technologies, i only want to know how can i get the length of a route? using JavaScript Maps API

  4. #4
    Nokia Developer Moderator
    Join Date
    Mar 2003
    Location
    Lempäälä/Finland
    Posts
    29,174

    Re: How obtain the length of a route?

    haven't been using that APIO too much, but I would assume you should find how you get the nokia.maps.routing.RouteSummary for the route.

  5. #5
    Regular Contributor
    Join Date
    Aug 2011
    Location
    Berlin
    Posts
    289

    Re: How obtain the length of a route?

    The architecture of a route is consistent across the the APIs, hence the code to obtain the totalLength looks very similar to the Java ME example

    When the route is successfully calculated, the call-back function holds an array of one or more routes.
    Code:
    var routes = observedRouter.getRoutes();
    Each of these holds a route summary, where you can obtain useful info about the route.


    Code:
    alert ("Route Length = " + routes[0].totalLength  + " m.");
    alert ("As the crow flies = " + routes[0].waypoints[0].mappedPosition.distance(routes[0].waypoints[1].mappedPosition)  + " m.");

    Here is the full code example:

    Code:
    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
    <!-- 
    	Example from Nokia Maps API Playground, for more information visit http://api.maps.nokia.com
     -->
    <html xmlns="http://www.w3.org/1999/xhtml">
    	<head>
    		<meta http-equiv="X-UA-Compatible" content="IE=7; IE=EmulateIE9"/>
    		<base href="http://developer.here.net/apiexplorer/examples/api-for-js/routing/map-with-route-from-a-to-b.html" />
    		<meta http-equiv="content-type" content="text/html; charset=UTF-8"/>
    		<title>Nokia Maps API Example: Add route from A to B</title>
    		<meta name="description" content="Routing Manager offers the ability to request a route with various modes between two points"/>
    		<meta name="keywords" content="routing, services, a to b, route, direction, navigation"/>
    		<!-- For scaling content for mobile devices, setting the viewport to the width of the device-->
    		<meta name=viewport content="width=device-width, height=device-height, initial-scale=1.0, maximum-scale=1.0, user-scalable=no"/>
    		<!-- Styling for example container (NoteContainer & Logger)  -->
    		<link rel="stylesheet" type="text/css" href="http://developer.here.net/apiexplorer/examples/templates/js/exampleHelpers.css"/>
    		<!-- By default we add ?with=all to load every package available, it's better to change this parameter to your use case. -->
                    <!-- Options ?with=maps|positioning|places|placesdata|directions|datarendering|all -->
    		<script type="text/javascript" charset="UTF-8" src="http://api.maps.nokia.com/2.2.3/jsl.js?with=all"></script>
    
    		<style type="text/css">
    			html {
    				overflow:hidden;
    			}
    			
    			body {
    				margin: 0;
    				padding: 0;
    				overflow: hidden;
    				width: 100%;
    				height: 100%;
    				position: absolute;
    			}
    			
    			#mapContainer {
    				width: 100%;
    				height: 100%;
    				left: 0;
    				top: 0;
    				position: absolute;
    			}
    		</style>
    	</head>
    	<body>
    		<div id="mapContainer"></div>
    
    		<script type="text/javascript" id="exampleJsSource">
    /*	Set authentication token and appid 
    *	WARNING: this is a demo-only key
    *	please register on http://api.developer.nokia.com/ 
    *	and obtain your own developer's API key 
    */
    nokia.Settings.set("appId", "_YOUR_APP_ID_"); 
    nokia.Settings.set("authenticationToken", "_YOUR_TOKEN_");
    
    // 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: [52.51, 13.4],
    		zoomLevel: 7,
    		// We add the behavior component to allow panning / zooming of the map
    		components:[new nokia.maps.map.component.Behavior()]
    	}),
    	router = new nokia.maps.routing.Manager(); // create a route manager;
    	
    // 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);
    			
    			//Zoom to the bounding box of the route
    			map.zoomTo(mapRoute.getBoundingBox(), false, "default");
    			alert ("Route Length = " + routes[0].totalLength  + " m.");
    			alert ("As the crow flies = " + routes[0].waypoints[0].mappedPosition.distance(routes[0].waypoints[1].mappedPosition)  + " m.");
    		} 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);
    
    // Create waypoints
    var waypoints = new nokia.maps.routing.WaypointParameterList();
    waypoints.addCoordinate(new nokia.maps.geo.Coordinate(52.51652540955727, 13.380154923889933));
    waypoints.addCoordinate(new nokia.maps.geo.Coordinate(52.52114106145058, 13.40921934080231));
    
    /* Properties such as type, transportModes, options, trafficMode can be
     * specified as second parameter in performing the routing request.
     * 
     * See for the mode options the "nokia.maps.routing.Mode" section in the developer's guide
     */
    var modes = [{
    	type: "shortest", 
    	transportModes: ["car"],
    	options: "avoidTollroad",
    	trafficMode: "default"
    }];
    
    // Calculate the route (and call onRouteCalculated afterwards)
    router.calculateRoute(waypoints, modes);
    
    		</script>
    	</body>
    </html>
    You can also find out more about routing from this article : http://www.developer.nokia.com/Commu...vanced_Routing
    Last edited by jasfox; 2013-01-30 at 08:41.

  6. #6
    Registered User
    Join Date
    May 2012
    Posts
    43

    Re: How obtain the length of a route?

    Thanks! it was very useful!!

Similar Threads

  1. How to obtain a route between locations
    By m00zg in forum [Archived] MH5: Nokia Maps Framework for mobile HTML5
    Replies: 1
    Last Post: 2012-07-07, 10:22
  2. Replies: 3
    Last Post: 2009-09-16, 16:08
  3. route and record route on 3rd edition
    By jcaradec in forum Symbian Networking & Messaging (Closed)
    Replies: 3
    Last Post: 2007-04-16, 19:07
  4. Record-Route and Route at SIP
    By mhappenhofer in forum Mobile Java Networking & Messaging & Security
    Replies: 0
    Last Post: 2005-11-30, 09:22
  5. Replies: 0
    Last Post: 1970-01-01, 02:00

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
×