Calculating a Route

This use-case describes how to calculate basic information on a routeClosed A route corresponds to a path of a vehicle through the underlying transport network. The main attributes of a route are the distance and the time that the vehicle travels along the path. from a point A to B via C as well as how to adjust the request to receive additional information like the route and elevation information.


  • Using the xroute API makes it easy to obtain optimized routes based on minimal input.
  • Fine-grained control over the calculation and output format saves processing as well as development time.


Please ensure following prerequisites are fulfilled before you start with the use case:

  • Installed and licensed PTV xRoute service

Programming Guide

The PTV xServer offers both a Web serviceClosed A web service represents a communication technology applied in computer networks. It provides an interface described in a machine-processable format, for example WSDL. and a RESTClosed REST (Representational State Transfer) represents a World Wide Web paradigm, consisting of constraints to the design of components which results in a better performance and maintainability. API for calculating routes.

Calculating a route with the web service API

The most basic functionality of xroute is calculating the distance and traveltime from a given point A to a given point B. In the example below we perform this operation using the JavaScript bindings to the xroute API:

In this example we provide our waypointsClosed A waypoint is a geographic location used to specify start, destination and possible stopovers for a route. A and B as x, y coordinates in the default EPSG:4326 format (see RequestBase.coordinateFormat). For A we use an OffRoadWaypoint and for B an OnRoadWaypoint, see the technical concept on waypoints and route legs for more information on the different waypoints. The request we pass to calculateRoute for this task contains just the list with our two waypoints. Once xroute has processed the request the callback routingCompleted is invoked which gives us access to the result of the calculation in the form of a RouteResponse object. As we did not specify a specific vehicleClosed The term vehicle describes what is being routed or planned for. Vehicles are used in route calculation, distance matrix calculation and effectively also in tour planning. In route calculation, vehicle properties like overall size, weight and speed are in focus. In tour planning, it is vehicle properties like capacity and availability. Commonly a vehicle is motorized, like a truck - including its trailer or a car. However also a bike or even a pedestrian are included in this definition. profileClosed A profile is a collection of parameters used to configure the request. Full profiles consist of a number of specialized sub-profiles like the VehicleProfile which describes the properties of a vehicle. (see Vehicle Parametrization) the default one for a 40 ton truck was used.

For many applications knowing only the distance and time traveled is not sufficient. You can selectively enable the calculation of additional information in your request to fit it to your application.

The example below shows some more sophisticated features for a route from A to B via C. For input waypoint A it uses the flag considerAlternativeNearByRoads to avoid using the wrong lane of a highway for a location provided by a GPS device. For the via input waypoint C it uses the ManipulateRouteWaypoint to pass by a location within a radius of 1 km. Finally, the calculation of a polylineClosed A polyline is a continuous line composed of one or more line segments given as a set of tuples with x,y and optional z coordinates. with additional elevation data is requested for the :

All that is needed for the polyline is requesting the corresponding ResultField to be provided. As the elevation data is an optional element of the returned polyline it is configured in the PolylineOptions instead of a separate field in the results.

By default the polyline will be returned as a plain Polyline object consisting of a list of Coordinate objects each of which has x, y and an optional z elevation. The elevation is given as meters above the mean sea level (EGM96) independent of the requested coordinate format.

Another standard task is to avoid toll roads. This feature is contained in the basic xroute license. To do so set the tollPenalty in the RequestProfile to a value greater than 0 and smaller then 2501. For further toll related information see the chapter toll calculation.

For a more complete example of what can be achieved by combining basic routing information, polylines and elevations you can take a look at the Visualize Elevations Sample.

Please refer to the documentation of the RouteRequest for more information on request parameterization.

Calculating a route with the REST API


In addition to a start and destination location, the results can be further refined by specifying query parameters:

Please refer to the documentation of the route REST operation for more information on request parameterization.