-
Notifications
You must be signed in to change notification settings - Fork 287
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
Labels
Comments
+1 :) |
Thanks, works like a charm! |
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;
}
}); |
@buche would you like to make a pull request for it or you prefer that i do it + credit yourself in the commit ? |
@brunob feel free to make a pull request yourself. No credit is needed. |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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!
The text was updated successfully, but these errors were encountered: