返回Google路线导航

时间:2018-08-28 16:33:36

标签: javascript angular google-maps-api-3

我在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
}

对此有道理吗?

0 个答案:

没有答案