使用jQuery检测Google地图路由何时完成

使用jQuery检测Google地图路由何时完成,jquery,ajax,google-maps,Jquery,Ajax,Google Maps,我正在画一张地图的方向图,由于许可证的限制,我需要把它链接成几个调用。通过maps API,我可以检测地图何时完成渲染,但这不包括绘制到地图的路线。我想调用一个函数,在渲染完路线后显示一个按钮。这是我的密码: // render the map from all points in the array function renderMap() { $('.allRunners').hide(); // temp hide the button var ds = new googl

我正在画一张地图的方向图,由于许可证的限制,我需要把它链接成几个调用。通过maps API,我可以检测地图何时完成渲染,但这不包括绘制到地图的路线。我想调用一个函数,在渲染完路线后显示一个按钮。这是我的密码:

// render the map from all points in the array
function renderMap() {
    $('.allRunners').hide(); // temp hide the button
    var ds = new google.maps.DirectionsService();
    var points = new Array();

    for (i = 0; i < routeData.length; i++) {
        if (routeData[i].type == "free") {
            processPolyLeg(routeData, i);
        } else {
            processLeg(routeData, i, ds);
        }
    }

    updateUnitMarkers();
    updateDistance();
}
我想我需要以renderLeg函数为目标,以了解上次调用它的时间,但正如您在下面看到的,此函数传递了参数,当我想要使用延迟对象时,这些参数不一定可用:

// when redrawing we need to wrap the asynchronous call to make it plot properly.
function processLeg(routeData, i, ds, retry) {
    // set up the initial request object
    var request = {
        origin: routeData[i].origin,
        destination: routeData[i].destination,
        travelMode: google.maps.DirectionsTravelMode.WALKING
    };

    ds.route(request, function (response, status) {
        if (status === google.maps.DirectionsStatus.OK) {
            // render the leg and set the renderer
            renderLeg(routeData, i, response);
            if (retry) {
                updateUnitMarkers();
                updateDistance();

            }
        } else if (status === google.maps.GeocoderStatus.OVER_QUERY_LIMIT) {
            setTimeout(function () {
                //retry
                processLeg(routeData, i, ds, true);

            }, 200);
        }
    });
}

// renders a leg to the map and updates the reference to the renderer
function renderLeg(routeData, i, response) {
    var dr = new google.maps.DirectionsRenderer({
        'draggable': false,
        'map': map,
        'preserveViewport': true,
        'suppressMarkers': true,
        'polylineOptions': { strokeColor: "#368CB9", strokeOpacity: 1, strokeWeight: 4 }
    });

    // render this step of the route to the map
    dr.setDirections(response);
    routeData[i].renderer = dr;
}

有什么想法吗?

DirectionsRenderer有一个
directions\u changed
事件。你有没有试过绑定任何代码?@Salman我没有,但我现在正在阅读。谢谢你的指导。
// when redrawing we need to wrap the asynchronous call to make it plot properly.
function processLeg(routeData, i, ds, retry) {
    // set up the initial request object
    var request = {
        origin: routeData[i].origin,
        destination: routeData[i].destination,
        travelMode: google.maps.DirectionsTravelMode.WALKING
    };

    ds.route(request, function (response, status) {
        if (status === google.maps.DirectionsStatus.OK) {
            // render the leg and set the renderer
            renderLeg(routeData, i, response);
            if (retry) {
                updateUnitMarkers();
                updateDistance();

            }
        } else if (status === google.maps.GeocoderStatus.OVER_QUERY_LIMIT) {
            setTimeout(function () {
                //retry
                processLeg(routeData, i, ds, true);

            }, 200);
        }
    });
}

// renders a leg to the map and updates the reference to the renderer
function renderLeg(routeData, i, response) {
    var dr = new google.maps.DirectionsRenderer({
        'draggable': false,
        'map': map,
        'preserveViewport': true,
        'suppressMarkers': true,
        'polylineOptions': { strokeColor: "#368CB9", strokeOpacity: 1, strokeWeight: 4 }
    });

    // render this step of the route to the map
    dr.setDirections(response);
    routeData[i].renderer = dr;
}