Zum Inhalt

ym.services.Routing

Überblick

Mit dieser Klasse können Sie Routenberechnungen durchführen. Für die Routenplanung stehen drei Klassen zur Verfügung: eine zur Berechnung einer einfachen Route (ein Ziel), eine zur Berechnung mehrerer Ziele (Matrix-Routenplanung) und eine zur Berechnung einer Route auf Basis einzelner GPS-Spuren.

Initialisierung

Codebeispiel: Initialisierung Routing

// Initialization: simple routing
var routing = new ym.services.Routing();

// Initialization: Routing with multiple destinations
var matrixRouting = new ym.services.MatrixRouting();

// Initialization: Match routing
var matchRouting = new ym.services.MatchRouting();

Methoden

Routing.calcRoute(waypoints, options) ab v3

Parameter: waypoints

Ein Array von LatLng-Objekten. Der erste Punkt markiert den Start, der letzte das Ziel. Die dazwischenliegenden Punkte werden als Wegpunkte interpretiert, über die die Route verlaufen soll.

Parameter: options

Name Beschreibung Datentyp Standardwert
type Ermöglicht die Angabe, welche Routing-Option verwendet werden soll. Optionen: ROUTE, TRIP, ISOCHRONE. string ROUTE
timeInMinutes Nur für den Typ ISOCHRONE. Zeit in Minuten, in der sich das Objekt bewegen kann. double null
distanceInMeters Nur für den Typ ISOCHRONE. Distanz in Metern, die das Objekt zurücklegen kann. double null
isochroneGrid Nur für den Typ ISOCHRONE. Legt die Feinheit des zu berechnenden Polygons fest. number null
isoLocale Das Locale setzt sich aus Sprache (ISO-639) und Land (ISO-3166-2) zusammen. string ym.options.locale
coordFormatOut Das Standard-Koordinatenformat ist GEODECIMAL_POINT. string GEODECIMAL_POINT
channel Eine Zeichenfolge, die protokolliert wird; sie kann vom Benutzer frei gewählt werden. string
speedProfile Das Geschwindigkeitsprofil: FAST, BICYCLE, PEDESTRIAN. string FAST
routingTimeMode Die Zeitangabe für das Routing: ARRIVAL, DEPARTURE. string ARRIVAL

Aufruf

Codebeispiel: Routing.calcRoute(waypoints, options) Beispielaufrufe
routing.calcRoute([[49.0, 10.0], [49.0, 9.0]]);
routing.calcRoute([ym.latLng(49.0, 10.0), ym.latLng(49.0, 9.0)]);
routing.calcRoute([{lat: 49.0, lng: 10.0}, {lat: 49.0, lng: 9.0}]);

MatrixRouting.calcRoute(startpoint, waypoints, options) ab v3

Parameter: startpoint

Ein LatLng-Objekt, das den Startpunkt definiert.

Parameter: waypoints

Ein Array von LatLng-Objekten. Die Punkte stellen verschiedene Zielpunkte dar.

Parameter: options

Die Optionen entsprechen denen der Methode calcRoute.

Aufruf

Codebeispiel: MatrixRouting.calcRoute(waypoints, options) Beispielaufrufe
matrixRouting.calcRoute(ym.latLng(49.1, 10.1), [ym.latLng(49.1, 10.1), ym.latLng(49.1, 9.1)]);
matrixRouting.calcRoute({lat: 49.1, lng: 9.1}, [{lat: 49.1, lng: 10.1}, {lat: 49.1, lng: 9.1}]);

MatchRouting.calcRoute(waypoints, options) ab v3.7

Parameter: waypoints

Ein Array von LatLng-Objekten. Die Punkte stellen verschiedene GPS-Punkte dar.

Parameter: options

Die Optionen entsprechen denen der Methode calcRoute.

Aufruf

Codebeispiel: MatchRouting.calcRoute(waypoints, options) Beispielaufrufe
matchRouting.calcRoute([ym.latLng(49.021629, 8.439032), ym.latLng(49.020770, 8.438335), ym.latLng(49.019820, 8.440667)]);
matchRouting.calcRoute([[8.439032, 49.021629], [8.438335, 49.020770], [8.440667, 49.019820]]);

Ereignisse

routing.on('success', function (request, response) {
    // Work
});

routing.on('error', function (request, response) {
    // Work
});

Codebeispiele

Dieses Beispiel ist vollständig; Sie können es lokal speichern und in einem HTTP-Kontext ausführen. Sie müssen vorher nur den Wert für apiKey ersetzen. Im Beispiel wird eine Route zwischen Frankfurt und Karlsruhe berechnet, wobei PEDESTRIAN (Fußgänger) als Geschwindigkeitsprofil gewählt wird. In diesem Fall werden nur der Start- und Endpunkt sowie die Routenlinie angezeigt. Farbe, Dicke und Deckungsgrad der Route werden festgelegt. Beachten Sie, dass das letzte Feature-Element die Routenlinie enthält.

<html>
<head lang="en">
    <meta charset="UTF-8">
    <title>Routing example</title>
    <!-- SmartMaps Style -->
    <link href="https://www.yellowmap.de/Presentation/Yellowmaps/Examples/assets/site.css" rel="stylesheet">
    <link rel="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>
    <div id="map-wrapper">
    <div id="map"></div>
</div>
<script src="https://www.yellowmap.de/api_rst/api/loader?apiKey=[INSERT API-KEY]&amp;libraries=enterprise-3"></script>
<script>
    var routingLayer, map;
    var rootUrl = ym.options.rootUrl;
    ym.ready({
        locale: "de-DE",
        rootUrl: rootUrl
    }, function (modules) {
        var routing = new ym.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>