Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Enable layers encoding by character in permalink (like on osm.org) #1209

Open
PetrDlouhy opened this issue Dec 24, 2013 · 0 comments
Open

Enable layers encoding by character in permalink (like on osm.org) #1209

PetrDlouhy opened this issue Dec 24, 2013 · 0 comments

Comments

@PetrDlouhy
Copy link
Contributor

The encoding of layers in permalink like layers=B00TTFF is highly unflexible. Almost every change in layers (adding, reordering, ...) break the existing permalinks.

The encoding used on osm.org (i.e. layers=TND) is much better. Every layer has it's unique character and if the character occur in layers character, it means that the layer is enabled.

I solved this problem in my project by overriding the 'createParams' and 'configureLayers' functions:

function createParams(center, zoom, layers) {
    center = center || this.map.getCenter();

    var params = OpenLayers.Util.getParameters(this.base);

    // If there's still no center, map is not initialized yet.
    // Break out of this function, and simply return the params from the
    // base link.
    if (center) {

        //zoom
        params.zoom = zoom || this.map.getZoom();

        //lon,lat
        var lat = center.lat;
        var lon = center.lon;

        if (this.displayProjection) {
            var mapPosition = OpenLayers.Projection.transform(
              { x: lon, y: lat },
              this.map.getProjectionObject(),
              this.displayProjection );
            lon = mapPosition.x;
            lat = mapPosition.y;
        }
        params.lat = Math.round(lat*100000)/100000;
        params.lon = Math.round(lon*100000)/100000;

        //layers
        layers = layers || this.map.layers;
        params.layers = '_';
        for (var i=0, len=layers.length; i<len; i++) {
            var layer = layers[i];

            if (layer.getVisibility() && layer.slug) {
               params.layers += layer.slug
            }
        }
    }

    return params;
}
function configureLayers() {
    for(var i=0, len=this.map.layers.length; i<len; i++) {
        layer = this.map.layers[i];

        if(!layer.slug) {
           continue;
        }

        var layer_visible = this.layers.indexOf(layer.slug) != -1;
        if(layer.isBaseLayer) {
           if(layer_visible) {
              this.map.setBaseLayer(layer);
           }
        } else {
           layer.setVisibility(layer_visible);
        }
    }

    if (this.layers.length == this.map.layers.length && this.layers.charAt(0) != "_") {
        this.map.events.unregister('addlayer', this, this.configureLayers);
        for(var i=0, len=this.layers.length; i<len; i++) {

            var layer = this.map.layers[i];
            var c = this.layers.charAt(i);

            if (c == "B") {
                this.map.setBaseLayer(layer);
            } else if ( (c == "T") || (c == "F") ) {
                layer.setVisibility(c == "T");
            }
        }
    }

}

But it would be nice, if this is integral part of OpenLayers.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant