Skip to content Skip to sidebar Skip to footer

Javascript Objects - Object Values Getting Undefined After Constructing

I am trying to create an object that handles Google Maps Api as following: function GoogleMap(container, mapOptions) { this.Container = container; this.Options = mapOption

Solution 1:

The this keyword does point to something else in the route callback function. It's DirectionRenderer property resolves to null/undefined, and getting the setDirections property from that will cause the exception.

Use a dereferencing variable:

functiondrawDirectionDriving(start, end) {
  var request = {
    origin: start,
    destination: end,
    travelMode: google.maps.TravelMode.DRIVING
  };
  var that = this;

  this.DirectionService.route(request,
    function (response, status) {
      if (status == google.maps.DirectionsStatus.OK) {
        that.DirectionRenderer.setDirections(response);
        that.DirectionResponse[this.DirectionId] = response;
        that.DirectionId++;
//      ^^^^ points to the GoogleMap instance
      }
      else {
        alert("Error during drawing direction, Google is not responding...");
      }
    }
  );
}

Post a Comment for "Javascript Objects - Object Values Getting Undefined After Constructing"