[OpenLayers-Dev] PanZoomBar tries to zoom outside the range (plus bad events)

Martin Saturka martin.saturka at sourcefabric.org
Wed Mar 16 08:14:16 EDT 2011


Hi,
when you drag the bar at the PanZoomBar too up or down, the control
tries to zoom to levels outside the map zoom range, e.g. to -1 or 22,
...

It would be nice if we had a hook for checking / modifying that
targeted zoom number. We use some hook by now, as below at the
"zoomBarUp" method.

Together with it, the mouse click event stopping does not work
correctly (at MSIE, and OL 2.10), thus we had to put some external
dealing there (otherwise it ended up with adding new points as we have
it for mouse click events).


OpenLayers.Control.PanZoomBarMod =
OpenLayers.Class(OpenLayers.Control.PanZoomBar, {
    // msie does not stops the event, and does not preserves its
properties either
    divClick: function (evt) {
	if (!OpenLayers.Event.isLeftClick(evt)) {
		return;
	}
	var levels = evt.xy.y / this.zoomStopHeight;
	if (this.forceFixedZoomLevel || !this.map.fractionalZoom) {
		levels = Math.floor(levels);
	}
	var zoom = this.map.getNumZoomLevels() - 1 - levels;
	zoom = Math.min(Math.max(zoom, 0), this.map.getNumZoomLevels() - 1);
	this.map.zoomTo(zoom);

        var stop_event = OpenLayers.Hooks.PanZoomBar.divClick(this);
        if (stop_event)
        {
            OpenLayers.Event.stop(evt);
        }

    },

    buttonDown: function(evt) {
        if (!OpenLayers.Event.isLeftClick(evt)) {
            return;
        }
        OpenLayers.Hooks.PanZoomBar.buttonDown(this);

        switch (this.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.zoomToMaxExtent();
            break;
          default:;
        }

        OpenLayers.Event.stop(evt);
    },

    zoomBarUp: function(evt) {
        if (!OpenLayers.Event.isLeftClick(evt)) {
            return;
        }
        if (this.mouseDragStart) {
            this.div.style.cursor="";
            this.map.events.un({
                "mouseup": this.passEventToSlider,
                "mousemove": this.passEventToSlider,
                scope: this
            });
            var deltaY = this.zoomStart.y - evt.xy.y;
            var zoomLevel = this.map.zoom;
            if (!this.forceFixedZoomLevel && this.map.fractionalZoom) {
                zoomLevel += deltaY/this.zoomStopHeight;
                zoomLevel = Math.min(Math.max(zoomLevel, 0),
                                     this.map.getNumZoomLevels() - 1);
            } else {
                zoomLevel += Math.round(deltaY/this.zoomStopHeight);
            }

            var max_layer_zoom = OpenLayers.Hooks.Zooms.maxZoom(this);
            var min_layer_zoom = OpenLayers.Hooks.Zooms.minZoom(this);

            if ((max_layer_zoom !== undefined) && (typeof
max_layer_zoom === "number")) {
                if (max_layer_zoom < zoomLevel) {zoomLevel = max_layer_zoom;}
            }
            if ((min_layer_zoom !== undefined) && (typeof
min_layer_zoom === "number")) {
                if (min_layer_zoom > zoomLevel) {zoomLevel = min_layer_zoom;}
            }

            this.map.zoomTo(zoomLevel);
            this.mouseDragStart = null;
            this.zoomStart = null;

            var stop_event = OpenLayers.Hooks.PanZoomBar.zoomBarUp(this);
            if (stop_event) {
                OpenLayers.Event.stop(evt);
            }
        }
    },

    CLASS_NAME: "OpenLayers.Control.PanZoomBar"
});

M.

-- 
Martin Saturka
Software Engineer, Sourcefabric
www.sourcefabric.org


More information about the Dev mailing list