Search code examples
openlayers

OpenLayers finding out a user's zoom level with the BBOX strategy


I am currently making a site which uses OpenLayers and plotting with the BBOX strategy. Potentially there can be alot of points to plot in the db. When OpenLayers sends the BBOX coordinates I would like OpenLayers to send what the current zoom level is. I would use this zoom level to decide whether I should send all results or the 10 most recent entries. Is it possible for OpenLayers to send what the current zoom level is back to the server when OpenLayers sends the BBOX information?


Solution

  • Not by default, but you could override the triggerRead function and add the zoom level yourself. Something like this (untested):

    OpenLayers.Strategy.BBOX.prototype.triggerRead = 
    function() {
        if (this.response) {
            this.layer.protocol.abort(this.response);
            this.layer.events.triggerEvent("loadend");
        }
        this.layer.events.triggerEvent("loadstart");
        this.response = this.layer.protocol.read({
            filter: this.createFilter(),
            callback: this.merge,
            scope: this,
            params: {center: this.layer.getZoomForExtent()}
        });
    }
    

    Or, of course just calculate the zoom level with the BBOX parameters.