Created
November 22, 2013 13:15
-
-
Save tormi/7599701 to your computer and use it in GitHub Desktop.
Leaflet permalink - support for layer overlays. See https://github.com/shramov/leaflet-plugins/issues/59
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* This file is licensed under Creative Commons Zero (CC0) | |
* http://creativecommons.org/publicdomain/zero/1.0/ | |
* Source: https://github.com/shramov/leaflet-plugins/issues/59 | |
*/ | |
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; | |
} | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment