3

I am working on a GIS API, that uses OpenLayers. I initialize the map lke this:

xgis.map = OpenLayers.Class(OpenLayers.Map, {
    var mapOptions = {};
    mapOptions.allOverlays = false;
    /*
        more mapOptions here
    */
    OpenLayers.Map.prototype.initialize.apply(this, ['map', mapOptions]);
});

Of course, I do this because I want to create my own methods, like this one, for example.

xgis.map.prototype.zoom(action) {
    switch(action) {
        case 'in:
            this.zoomIn();    // Uses the OpenLayers zoomIn method
            break;
        case 'out:
            this.zoomOut();    // Uses the OpenLayers zoomOut method
            break;
};

The problem is this 'zoom' method I created is not recognized as a function (I get: number is not a function), because OpenLayers has a 'zoom' numeric value where it stores the zoom.

Is there a way to use inheritance and access the parent's methods like this?

this.parent.zoomIn()

This would definitely fix the 'zoom' name problem.

1 Answer 1

1

You are writing bad, it must be:

xgis.map.prototype.zoom = function(action) { ... }

Anyway, it is better you follow the Ol code conventions. To create subclasses you must create methods inside class definition, something like:

xgis.map = OpenLayers.Class(OpenLayers.Map, {
    someAttribute: 'someValue'.
    someMethod: function() {
        ...
    }

    CLASS_NAME: 'your_class_name'
});

You can find a custom control code at: http://acanimal.github.io/Openlayers-Cookbook/recipes/ch08/crossControl.js Which is part of the samples of the OpenLayers Cookbook. See more samples at http://acanimal.github.io/Openlayers-Cookbook/ the sample "chapter8-custom control".

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.