Note: You are currently viewing an older version of our documentation. All functions described here are still fully available. For more up-to-date examples, enhanced functionality, and improved user guidance, we encourage you to visit our new documentation page.
ym.services.Routing
You can use this class to perform route calculations.
Initialization
Three classes are available for route planning, one for calculating a simple route (one destination), one for calculating multiple destinations (matrix route planning) and one to calculate a route based on individual GPS traces.
Initialization of the Routing, MatrixRouting and MatchRouting :
Code example: Initialization Routing
// Initialization: simple routingvar routing =newym.services.Routing();// Initialization: Routing with multiple destinationsvar matrixRouting =newym.services.MatrixRouting();// Initialization: Match routingvar matchRouting =newym.services.MatchRouting();
Methods
Routing.calcRoute(waypoints, options) from v3
Parameter: waypoints
An array of LatLng-objects. The first point marks the start, the last one the destination. The points in between are interpreted as waypoints, over which the route should run.
Parameter: options
Name
Description
Data type
Default value
type
Allows to specify which routing option should be used.
ROUTE - Calculation of a simple route with a start and destination point and intermediate destinations.
TRIP - Optimized the route from a starting point (first coordinate) to multiple destinations.
By setting the option: routingRoundTrip to true, it is determined that the start point is approached again at the end.
ISOCHRONE - Calculation of an area that can be approached from a starting point (first coordinate) in a specified time or distance.
Option timeInMinutes: Time in minutes in which the object can move. Can take values from >0 to 60. Either timeInMinutes or distanceInMeters can be passed.
Option distanceInMeters: Distance in meters in which the object can move. Either timeInMinutes or distanceInMeters can be passed.
Option isochroneGrid: Defines the fineness of the polygon that will be calculated. As higher the values, the finer the polygon. Can take values from 35 to 150.
string
ROUTE
timeInMinutes
Only for the type ISOCHRONE. Time in minutes in which the object can move. Can take values from >0 to 60. Either timeInMinutes or distanceInMeters can be passed.
double
null
distanceInMeters
Only for the type ISOCHRONE. Distance in meters in which the object can move. Either timeInMinutes or distanceInMeters can be passed.
double
null
isochroneGrid
Only for the type ISOCHRONE. Defines the fineness of the polygon that will be calculated. As higher the values, the finer the polygon. Can take values from 35 to 150.
number
null
isoLocale
The locale is composed of language (ISO-639) and country (ISO-3166-2).
string
ym.options.locale
coordFormatOut
The default coordinate format is GEODECIMAL_POINT, which corresponds to the CRS84 format. Furthermore there is the format MERCATOR, which is similar to EPSG:3857.
string
GEODECIMAL_POINT
channel
A character string that is logged; it can be freely selected by the user.
string
speedProfile
The speed profile: 1. FAST – fast vehicle 2. BICYCLE – Bicycle 3. PEDESTRIAN – Pedestrian
string
FAST
routingTimeMode
The time for the routing 1. ARRIVAL – Arrival 2. DEPARTURE – Departure
string
ARRIVAL
Call (application/json)
Code example: Routing.calcRoute(waypoints, options) Example calls
This example is complete; you can save it locally and run it in an http context. You only have to replace the value for apiKey before.
In the example a route between Frankfurt and Karlsruhe is calculated, where PEDESTRIAN (pedestrian) is chosen as speed profile. In this case only the start and end point and the route line is shown. The color, thickness and degree of coverage of the route are defined.
Note that the last feature element contains the route line.
Code example: MatrixRouting.calcRoute(waypoints, options) Example calls
<html><headlang="en"><metacharset="UTF-8"><title>Routing example</title><!-- SmartMaps Style --><linkhref="https://www.yellowmap.de/Presentation/Yellowmaps/Examples/assets/site.css"rel="stylesheet"><linkrel="stylesheet"href="https://www.yellowmap.de/api_js/cdn/highlight/styles/github.css"><style>.leaflet-control-zoom-in{color: red !important;}.leaflet-control-zoom-out{color: blue !important;}</style></head><body><divid="map-wrapper"><divid="map"></div></div><scriptsrc="https://www.yellowmap.de/api_rst/api/loader?apiKey={API_KEY}&libraries=enterprise-3"></script><script>var routingLayer, map;var rootUrl = ym.options.rootUrl;
ym.ready({
locale:"de-DE",
rootUrl: rootUrl
},function(modules){var routing =newym.services.Routing();// First create a routing layer instance. You will see here // the complete logic to create a route with waypoints.
routingLayer = ym.geoJson(null,{style:function(feature){// Draw the routing polyline. For more information about // GeoJSON features and geometry, see: http://geojson.org/if(feature.geometry.type ==="LineString"){// To make the line invisible, set weight to 0.return{weight:5, opacity:0.8, color:'#406291'};}},// Popup of the starting point.onEachFeature:function(feature, layer){if(feature.properties && feature.properties.description){
layer.bindPopup(feature.properties.description);}},pointToLayer:function(feature, latlng){// Draw the waypoint markers above the polyline route. You have // Description and orientation information is available in the properties // for a more detailed output.return ym.circleMarker(latlng,{
radius:12,
fillColor:"#990000",
color:"#ffffff",
weight:1,
opacity:1,
fillOpacity:0.8});}});
map = ym.map("map",{
center:[49.001,8.417],
zoom:12});
map.addLayer(routingLayer);// From Frankfurt to Karlsruhe on foot.var latlngs =[];
latlngs.push(ym.latLng(50.095685,8.690445));
latlngs.push(ym.latLng(49.00162,8.39905));
routing.calcRoute(latlngs,{speedProfile:'PEDESTRIAN'});
routing.on("success",function(request, response){// Now you can redraw.var route = response.body;var features = response.body.features;
console.log(response.body);
route.features =[];
route.features.push(features[0])// Starting point
route.features.push(features[features.length-2])// Destination// The last feature contains the route line:
route.features.push(features[features.length-1])// Route line
console.log(route);
routingLayer.addData(route);});});</script></body></html>