1

Trying to get my head around using the Javascript API from Google Maps in my Angular 2 Project.

The problem i have is based on this code:

@Injectable()

export class CalculateAndDisplayRouteService {

public durationTrafficSource = new ReplaySubject<string>();
public durationTraffic$ = this.durationTrafficSource.asObservable();

public changeDurationTraffic(string) {
    this.durationTrafficSource.next(string);
}


public routeResponse(response, status) {
    console.log(response);
    let map = new google.maps.Map(document.getElementById('map'), {
        center: {lat: lat, lng: lng},
        zoom: 8
    });
    if (status === 'OK') {
        let directionsDisplay = new google.maps.DirectionsRenderer({
            suppressMarkers: false,
            suppressInfoWindows: true
        });
        directionsDisplay.setMap(map);
        directionsDisplay.setDirections(response);

        this.changeDurationTraffic(response.routes[0].legs[0].duration.text); //Error is here
    } else {
        window.alert('Directions request failed due to ' + status);
    }
}
private currentDate = new Date();

public route(directionsService, transportMode, origin, destination) {
    if(transportMode === 'DRIVING') {
        directionsService.route({
            origin: origin,
            destination: destination,
            travelMode: transportMode,
            drivingOptions: {
                departureTime: new Date(this.currentDate.getFullYear(), this.currentDate.getMonth(), this.currentDate.getDate() + 1, 7, 45),
                trafficModel: 'pessimistic'
            }
        }, this.routeResponse);

The problem I have is in the routeResponse function. I receive an error where the changeDurationTraffic function is called.

"Uncaught TypeError: this.changeDurationTraffic is not a function".

Is there something i am doing wrong? Thanks.

3
  • anyone have any ideas? haha. I've attempted to progress by adding a second function to the callback ` drivingOptions: { departureTime: new Date(this.currentDate.getFullYear(), this.currentDate.getMonth(), this.currentDate.getDate() + 1, 7, 45), trafficModel: 'pessimistic' } }, this.routeResponse || this.otherFunction); ` but this other function still cannot access my changeDurationTraffic function. Commented Apr 13, 2017 at 3:58
  • Did the answer solve your problem? Commented Apr 13, 2017 at 8:19
  • 1
    @echonax yes it did, thank you very much. Commented Apr 13, 2017 at 8:35

1 Answer 1

2

The this inside that function is not refering to your component because of this method:

public route(directionsService, transportMode, origin, destination) {
    if(transportMode === 'DRIVING') {
        directionsService.route({
            origin: origin,
            destination: destination,
            travelMode: transportMode,
            drivingOptions: {
                departureTime: new Date(this.currentDate.getFullYear(), this.currentDate.getMonth(), this.currentDate.getDate() + 1, 7, 45),
                trafficModel: 'pessimistic'
            }
        }, this.routeResponse); //<-- ****Error here.****

Change that line to

this.routeResponse.bind(this);

Suggested reading: How to access the correct `this` context inside a callback?

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.