1
function testPanZoom() {
    console.log("test func");
    var newPanZoom = new OpenLayers.Control.PanZoom();

    OpenLayers.Util.extend(newPanZoom, {
        onButtonClick: function(evt) {
            console.log("test button click");
            var btn = evt.buttonElement;
            switch (btn.action) {
                case "panup": 
                    this.map.pan(0, -this.getSlideFactor("h"));
                    break;
                case "pandown": 
                    this.map.pan(0, this.getSlideFactor("h"));
                    break;
                case "panleft": 
                    this.map.pan(-this.getSlideFactor("w"), 0);
                    break;
                case "panright": 
                    this.map.pan(this.getSlideFactor("w"), 0);
                    break;
                case "zoomin": 
                    this.map.zoomIn(); 
                    break;
                case "zoomout": 
                    this.map.zoomOut(); 
                    break;
                case "zoomworld": 
                    this.map.zoomToExtent(new OpenLayers.Bounds(380983.55914508,
                                                                6668932.8432267,
                                                                392057.98129392,
                                                                6674994.9886843)); 
                    break;
                }
        }
    });
    return newPanZoom;
}

map.addControl(new testPanZoom());

However it does not seem to work. The control is added but zoomToExtent for worldicon in switch case does not really change anything.

Can you tell me what's wrong ?

1
  • What if you remove new when calling testPanZoom()?
    – erilem
    May 5, 2012 at 23:35

1 Answer 1

5

For override control's method you should use the following approach:

OpenLayers.Control.PanZoom.prototype.onButtonClick = function(evt) {
    var btn = evt.buttonElement;
    switch (btn.action) {
        case "panup": 
            this.map.pan(0, -this.getSlideFactor("h"));
            break;
        case "pandown": 
            this.map.pan(0, this.getSlideFactor("h"));
            break;
        case "panleft": 
            this.map.pan(-this.getSlideFactor("w"), 0);
            break;
        case "panright": 
            this.map.pan(this.getSlideFactor("w"), 0);
            break;
        case "zoomin": 
            this.map.zoomIn(); 
            break;
        case "zoomout": 
            this.map.zoomOut(); 
            break;
        case "zoomworld": 
            alert("Hello world!");
            break;
    }
}
1
  • 1
    I had to change this slightly for my application, which uses OpenLayers 2.11. I posted an answer with the changes here
    – jhsowter
    Jul 4, 2012 at 4:46

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.