S
S
sgidlev2018-07-10 18:24:53
Yandex maps
sgidlev, 2018-07-10 18:24:53

How to compare the times of routes by car, public transport and on foot, and build the fastest one?

Help solve the problem: It is necessary to compare routes with each other in time depending on the type of transport (car, public transport, on foot) and build the fastest one.
The main problem I have is how to compare the times of the routes. I am using the method

multiRoute.model.events
            .add("requestsuccess", function (event) {
            console.log("Найдено маршрутов: " + routes.length);
            for (var i = 0, l = routes.length; i < l; i++) {
                 console.log("Время маршрута в числовом выражении " + (i + 1) + ": " + routes[i].properties.get("durationInTraffic").value);
                    total_routes.push(routes[i].properties.get("durationInTraffic").value);
                 }
            })
            .add("requestfail", function (event) {
                console.log("Ошибка: " + event.get("error").message);
            });

And so I go through each route, but the data is not written to the array, i.e. I can't later compare the values ​​and output one of them.
Maybe there is some simple way to get the length of the route? For example console.log(multiRoute.getRoutes().get(0).properties.get("durationInTraffic").value);

Answer the question

In order to leave comments, you need to log in

1 answer(s)
S
sgidlev, 2018-07-12
@sgidlev

I did it like this, I think it can be better, but I did not figure out how:

/* =========== Прокладываем самый быстрый маршрут до ближайшего магазина =========== */
    var routes_array = [];

    //Проходимся по каждому маршруту и добавляем их в массив routes_array
    var PromiseRoute = new Promise(function (resolve, reject) {
        var test;
        getRouteLength(startPoint, destPoint, 'auto').then(
            result => {
                console.log("Fulfilled: " + result);
                //routes_array.push(result);
                routes_array.push(result);
                resolve(routes_array);
            }
        );

    }).then(function (result) {
        console.log(result);
        return getRouteLength(startPoint, destPoint, 'masstransit').then(
            result => {
                console.log("Fulfilled: " + result);
                //routes_array.push(result);
                routes_array.push(result);
                return routes_array;
            }
        );
    }).then(function (result) {
        console.log(result);
        return getRouteLength(startPoint, destPoint, 'pedestrian').then(
            result => {
                console.log("Fulfilled: " + result);
                routes_array.push(result);
                return routes_array;
            }
        );
    });

    var FastestRouteTime,
        FastestRouteLength,
        FastestRouteTimeText,
        FastestRouteLengthText;

    //Смотрим обобщенный результат
    PromiseRoute.then(
        result => {
            FastestRoute = result[0].Route;
            FastestRouteTime = result[0].RouteTime;
            FastestRouteLength = result[0].RouteLength;
            FastestRouteTimeText = result[0].RouteTimeText;
            FastestRouteLengthText = result[0].RouteLengthText;


            for (var i = 0, l = result.length; i < l; i++) {
                //Ищем самый быстрый маршрут
                if (result[i].RouteTime > 0 && result[i].RouteTime < result[0].RouteTime) {
                    FastestRoute = result[i].Route;
                    FastestRouteTimeText = result[i].RouteTimeText;
                    FastestRouteLengthText = result[i].RouteLengthText;

                }
            }

            console.log('FastestRouteTime: ', FastestRouteTime);
            console.log('FastestRouteLength: ', FastestRouteLength);
            $('#route-time').html(FastestRouteTimeText);
            $('#route-length').html(FastestRouteLengthText);
            myMap.geoObjects.add(FastestRoute); //Добавляем самый быстрый маршрут
        }
    );
    /* =========== Прокладываем самый быстрый маршрут до ближайшего магазина =========== */


    /* =========== Функция для поиска самого быстрого маршрута до ближайшего магазина  =========== */
    function getRouteLength(startPoint, destPoint, rMode) {
        multiRoute = new ymaps.multiRouter.MultiRoute({
            referencePoints: [
                startPoint,
                destPoint
            ],
            params: {
                routingMode: rMode,
                avoidTrafficJams: true,
                results: 1,
            }
        }, {
               // Автоматически устанавливать границы карты так, чтобы маршрут был виден целиком.
                boundsAutoApply: true
            });

        return new Promise(function (resolve, reject) {
            multiRoute.model.events.add("requestsuccess", function () {
                var routes = multiRoute.model.getRoutes();
                var minLength = -1;
                var routeLength = -1;
                var routeTime = -1;
                var routeLengthText;
                var routeTimeText;

                for (var i = 0, l = routes.length; i < l; i++) {
                    //let routeLength =  multiRoute.model.getRoutes().properties.get('distance').value / 1000;
                    routeLength = routes[i].properties.get("distance").value;
                    routeLengthText = routes[i].properties.get("distance").text;
                    routeTime = routes[i].properties.get("durationInTraffic").value;
                    routeTimeText = routes[i].properties.get("durationInTraffic").text;
                    if (minLength < 0 || routeLength < minLength) {
                        minLength = routeLength;
                    }
                    console.log("Длина маршрута " + (i + 1) + ": " + routes[i].properties.get("distance").text);
                    console.log(routeLengthText);
                    console.log("Время маршрута " + (i + 1) + ": " + routes[i].properties.get("durationInTraffic").text);
                    console.log(routeTimeText);
                }
                resolve({
                    Route: multiRoute,
                    RouteTime: routeTime,
                    RouteLength: routeLength,
                    RouteLengthText: routeLengthText,
                    RouteTimeText: routeTimeText,
                });
            });
        });
    }

    /* =========== ! Функция для поиска самого быстрого маршрута до ближайшего магазина  =========== */

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question