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

Permalink: Added support for overlays #59

Closed
buche opened this issue Feb 1, 2013 · 5 comments
Closed

Permalink: Added support for overlays #59

buche opened this issue Feb 1, 2013 · 5 comments
Assignees

Comments

@buche
Copy link
Contributor

buche commented Feb 1, 2013

I've added support for overlays in my project (sorry, no github pull request availabe).

It works using flags (T/F) for each overlay (e.g. ...&overlays=TFTT). Dynamically changing overlays in layer control will break it, because it relies on the sequence of the overlays in layer control.

It needs leaflet from github with merged pull request #1286 (layer control events for overlays): Leaflet/Leaflet#1286

Permalink.Overlay.js: http://pastebin.com/wChyR7bD

Feel free to reuse the code as you like!

@Firefishy
Copy link

+1 :)

@tormi
Copy link

tormi commented Nov 22, 2013

Thanks, works like a charm!
+1

@brunob
Copy link
Collaborator

brunob commented Jun 21, 2015

Copying proposed code here in order to keep it :

/**
 * This file is licensed under Creative Commons Zero (CC0)
 * http://creativecommons.org/publicdomain/zero/1.0/
 */

L.Control.Permalink.include({

    initialize_overlay: function() {
        this.on('update', this._set_overlays, this);
        this.on('add', this._onadd_overlay, this);
    },

    _onadd_overlay: function(e) {
        this._map.on('overlayadd', this._update_overlay, this);
        this._map.on('overlayremove', this._update_overlay, this);
        this._update_overlay();
    },

    _update_overlay: function() {
        if (!this.options.layers) return;
        var overlayflags = this.options.layers.overlayFlags();
        if (overlayflags && overlayflags != '') {
            this._update({overlays: overlayflags});
        }
    },

    _set_overlays: function(e) {
        var p = e.params;
        if (!this.options.layers || !p.overlays) return;
        this.options.layers.setOverlays(p.overlays);
    }
});

L.Control.Layers.include({
    setOverlays: function(overlayflags) {
        var layer, obj, idx=0;
        for (var i in this._layers) {
            if (!this._layers.hasOwnProperty(i)) continue;
            obj = this._layers[i];
            if (obj.overlay) {
                // visible if not specified or flag==T
                var visible = (idx >= overlayflags.length || overlayflags[idx] == 'T');
                idx++;
                if (!visible && this._map.hasLayer(obj.layer)) {
                    this._map.removeLayer(obj.layer)
                } else if (visible && !this._map.hasLayer(obj.layer)) {
                    this._map.addLayer(obj.layer)
                }
            }
        }
    },

    overlayFlags: function() {
        var flags = '';
        for (var i in this._layers) {
            if (!this._layers.hasOwnProperty(i))
                continue;
            var obj = this._layers[i];
            if (!obj.overlay) continue;
            if (obj.overlay) {
                if (this._map.hasLayer(obj.layer)) {
                    flags += 'T';
                } else {
                    flags += 'F';
                }
            }
        }
        return flags;
    }
});

@brunob brunob self-assigned this Jun 22, 2015
@brunob
Copy link
Collaborator

brunob commented Dec 2, 2015

@buche would you like to make a pull request for it or you prefer that i do it + credit yourself in the commit ?

@buche
Copy link
Contributor Author

buche commented Dec 3, 2015

@brunob feel free to make a pull request yourself. No credit is needed.

@brunob brunob closed this as completed in e02ce62 Dec 3, 2015
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

4 participants