我在Angular项目中使用Google Directions API。我已经创建了生成路线的服务,但是无法返回如下结果:
public directionsService: any;
public route: any;
constructor() {
this.directionsService = new google.maps.DirectionsService();
}
getRoute(addr_start: any, addr_dest: any) {
const req = {
origin: addr_start,
destination: addr_dest,
travelMode: google.maps.TravelMode.DRIVING
}
let route = this.directionsService.route(req, (result, status) => {
if (status == google.maps.DirectionsStatus.OK) {
console.log(result); // this works
this.route = result; // Doesn't work
// return result; // Doesn't work
}
});
return this.route; // returns undefined
}
对此有道理吗?