从Google DistanceMatrix排序回调

时间:2017-02-25 16:52:50

标签: javascript callback google-distancematrix-api

背景:我有2个出发地,2个目的地和2个不同的出发时间:

1) A->B (start-time: X)
2) C->D (start-time: Y)

我正在寻找从B到C以及从D到A的时间,这些出发时间是动态的:在旅程的第一条路径执行后计算。

3) B->C (start-time: X+p)
4) D->A (start-time: Y+q)

基本上,需要执行1)和2)才能执行3)和4)。此外,我需要精确的顺序这些结果:1,2,3,4。

我对回调并不太熟悉,我没有发现其他人有此类问题,所以任何帮助都表示赞赏。 目前,我已经知道至少1和2在3和4之前执行,但由于异步,回调以随机顺序响应+有时3和4相等。

JS代码:

$(function () {

    var date = new Date();
    date.setDate(date.getDate() + 7);
    var DrivingOptions = {};
    var hours, mins, hours2, mins2, origin1, origin2, origin3, origin4, dest1, dest2, dest3, dest4;
    var resultCounter = 1;

    $('#distance_form').submit(function (e) {
        event.preventDefault();
        //Following code assigns values for origins/destinations and values for initial departuretimes (taken from format hh:mm)
        origin1 = dest1 = $('#sp0001').val();
        dest2 = origin2 = $('#ep0001').val();
        origin3 = dest3 = $('#sp0002').val();
        dest4 = origin4 = $('#ep0002').val();
        hours = $('#st0001').val().slice(0, 2);
        mins = $('#st0001').val().slice(3, 5);
        hours2 = $('#st0002').val().slice(0, 2);
        mins2 = $('#st0002').val().slice(3, 5);
        var duration_text = calculateDistance(origin1, dest2);
        var duration_text2 = calculateDistance2(origin3, dest4);
    });

    function calculateDistance(origin, destination) {
        var service = new google.maps.DistanceMatrixService();
        date.setHours(hours);
        date.setMinutes(mins);
        date.setSeconds(0);
        DrivingOptions = {
            departureTime: date,
            trafficModel: 'pessimistic'
        };
        service.getDistanceMatrix({
            origins: [origin],
            destinations: [destination],
            travelMode: 'DRIVING',
            drivingOptions: DrivingOptions
        }, callback);
    }

    function calculateDistance2(origin, destination) {
        var service = new google.maps.DistanceMatrixService();
        date.setHours(hours2);
        date.setMinutes(mins2);
        date.setSeconds(0);
        DrivingOptions = {
            departureTime: date,
            trafficModel: 'pessimistic'
        };
        service.getDistanceMatrix({
            origins: [origin],
            destinations: [destination],
            travelMode: 'DRIVING',
            drivingOptions: DrivingOptions
        }, callback2);
    }

    function callback(response, status) {
        if (status != google.maps.DistanceMatrixStatus.OK) {
            $('#result').html(err);
        } else {
            var origin = response.originAddresses[0];
            var destination = response.destinationAddresses[0];

            var duration = response.rows[0].elements[0].duration_in_traffic;
            var duration_value = duration.value;
            var duration_text = duration.text;
            var duration_time = duration_text.substring(0, duration_text.length);
            console.log('Trip from ' + origin + ' to ' + destination + ' takes ' + duration_time + '.';

            if (resultCounter < 2 || resultCounter % 2 == 1) {
                var service = new google.maps.DistanceMatrixService();
                date.setSeconds(duration.value);
                DrivingOptions = {
                    departureTime: date,
                    trafficModel: 'pessimistic'
                };
                service.getDistanceMatrix({
                    origins: [origin2],
                    destinations: [dest3],
                    travelMode: 'DRIVING',
                    drivingOptions: DrivingOptions
                }, callback);

            }
            resultCounter++;
        }
    }

    function callback2(response, status) {
        if (status != google.maps.DistanceMatrixStatus.OK) {
            $('#result').html(err);
        } else {
            var origin = response.originAddresses[0];
            var destination = response.destinationAddresses[0];

            var duration = response.rows[0].elements[0].duration_in_traffic;
            var duration_value = duration.value;
            var duration_text = duration.text;
            var duration_time = duration_text.substring(0, duration_text.length);
            console.log('Trip from ' + origin + ' to ' + destination + ' takes ' + duration_time + '.';

            if (resultCounter < 2 || resultCounter % 2 == 1) {
                var service = new google.maps.DistanceMatrixService();
                date.setSeconds(duration.value);
                DrivingOptions = {
                    departureTime: date,
                    trafficModel: 'pessimistic'
                };
                service.getDistanceMatrix({
                    origins: [origin4],
                    destinations: [dest1],
                    travelMode: 'DRIVING',
                    drivingOptions: DrivingOptions
                }, callback);

            }
            resultCounter++;
        }
    }
});

0 个答案:

没有答案