gpt4 book ai didi

javascript - 如何等待方向服务完成才能访问方向显示的方向属性

转载 作者:行者123 更新时间:2023-11-28 01:28:33 25 4
gpt4 key购买 nike

我正在使用 Google Map 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???
}
});
}

此后,我需要访问Direction Display对象,该对象仅在路线渲染成功后才可用(意味着该功能已完成)。我试图将该代码块放在该位置,但此时Direction DisplayDirection属性仍然不可用,因此失败。但如果我在 calcRoute 函数之后调用它,就可以了。

所以,我的问题是,我如何知道回调何时完成以便我可以继续我的工作?我尝试过像下面这样放置一个 flag ,但它不成功,循环是无限的。

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) {
}
}

最佳答案

观察directions_changed事件:

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

关于javascript - 如何等待方向服务完成才能访问方向显示的方向属性,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22423847/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com