Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/security/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 如何等待方向服务完成以访问方向显示的方向属性_Javascript_Google Maps_Google Maps Api 3 - Fatal编程技术网

Javascript 如何等待方向服务完成以访问方向显示的方向属性

Javascript 如何等待方向服务完成以访问方向显示的方向属性,javascript,google-maps,google-maps-api-3,Javascript,Google Maps,Google Maps Api 3,我正在使用谷歌地图API进行工作。要在两点之间绘制路线,我使用此函数: function calcRoute(start, end) { var pStart = new google.maps.LatLng(start.lat(), start.lng()); var pEnd = new google.maps.LatLng(end.lat(), end.lng()); var request = { origin:

我正在使用
谷歌地图API
进行工作。要在两点之间绘制路线,我使用此函数:

function calcRoute(start, end) {
        var pStart = new google.maps.LatLng(start.lat(), start.lng());
        var pEnd = new google.maps.LatLng(end.lat(), end.lng());

        var request = {
            origin: pStart,
            destination: pEnd,
            travelMode: google.maps.TravelMode.DRIVING
        };
        directionsService.route(request, function(result, status) {
            if (status == google.maps.DirectionsStatus.OK) {
                directionsDisplay.setDirections(result);

                // Box the overview path of the first route
                var path = result.routes[0].overview_path;
                boxes = rboxer.box(path, distance);

                //drawBoxes(boxes);
                nearbyMarkets = search_market(boxes);
                // PUT HERE???
            }
        });
    }
在此之后,我需要访问
方向显示
对象,该对象仅在路线成功渲染后可用(表示此功能已完成)。我试图将代码块置于该位置,但当时,
方向显示的
方向属性
仍然不可用,因此失败。但是如果我在
calcRoute
函数之后调用它,就可以了

所以,我的问题是,我如何知道回调何时结束,以便继续我的工作?我试着像下面这样放置一个
标志,但没有成功,循环是无限的

function calcRoute(start, end) {
        var pStart = new google.maps.LatLng(start.lat(), start.lng());
        var pEnd = new google.maps.LatLng(end.lat(), end.lng());
        var pass = false;

        var request = {
            origin: pStart,
            destination: pEnd,
            travelMode: google.maps.TravelMode.DRIVING
        };
        directionsService.route(request, function(result, status) {
            if (status == google.maps.DirectionsStatus.OK) {
                directionsDisplay.setDirections(result);

                // Box the overview path of the first route
                var path = result.routes[0].overview_path;
                boxes = rboxer.box(path, distance);

                //drawBoxes(boxes);
                nearbyMarkets = search_market(boxes);
                pass = true;
            }
        });
        while (!pass) {
        }
    }

遵守
方向\u更改
-事件:

google.maps.event.addListener(directionsDisplay, 'directions_changed',function(){
 if(this.get('directions')){
   //directions are available, do something
 }
});

DirectionsRenderer在初始化后始终可以访问(无论是否绘制了方向)。请解释什么没有按预期工作。我试图访问
directionsDisplay.directions.routes[0]。legs[0]
,但
directionsDisplay.directions
不可用,可能为空。这正是我需要的。非常感谢:)你是谷歌开发者团队的吗?