0

I'm building an class to render maps using the Google Maps API.

This is my class

function Map() {
}

Map.prototype.map = null;
Map.prototype.map_options = null;
Map.prototype.div_map = null;
Map.prototype.directions_service = null;
Map.prototype.center = null;
Map.prototype.points = null;

Map.prototype.start = function() {

    this.map_options = {
        zoom: 14,
        center: this.center,
        mapTypeId: google.maps.MapTypeId.ROADMAP,
        streetViewControl: false
    };

    this.map = new google.maps.Map(document.getElementById(this.div_map), this.map_options);

    this.directions_service = new google.maps.DirectionsRenderer();

    this.directions_service.setOptions({
        preserveViewport: true,
        suppressInfoWindows: true,
        polylineOptions: {
            strokeWeight: 2,
            strokeOpacity: 0.8,
            strokeColor: "blue"
        }
    });

    this.map.setMap(this.directions_service);
};

When I use this class, a received an error, in this line:

this.map.setMap(this.directions_service);

The error is: Uncaught Typeerror: undefined is not a function

But, I test the this.map variable with typeof(), and returns 'object'.

The this.div_map is set normally, also, the initial map in rendered.

I'm doing something wrong?

1 Answer 1

2

Object map has no method 'setMap'. But DirectionRenderer has. You have to change that line to

this.directions_service.setMap(this.map)
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.