I find solution, code below (or live example http://spatialhast.github.io/leaflet.swipe.html):
<script>
var BingLayer = L.TileLayer.extend({
getTileUrl: function(tilePoint) {
this._adjustTilePoint(tilePoint);
return L.Util.template(this._url, {
s: this._getSubdomain(tilePoint),
q: this._quadKey(tilePoint.x, tilePoint.y, this._getZoomForUrl())
});
},
_quadKey: function(x, y, z) {
var quadKey = [];
for (var i = z; i > 0; i--) {
var digit = '0';
var mask = 1 << (i - 1);
if ((x & mask) != 0) {
digit++;
}
if ((y & mask) != 0) {
digit++;
digit++;
}
quadKey.push(digit);
}
return quadKey.join('');
}
});;
var testGeom = {
"type": "Polygon",
"coordinates": [
[
[5.9559111595, 45.8179931641],
[5.9559111595, 47.808380127],
[10.4920501709, 47.808380127],
[10.4920501709, 45.8179931641],
[5.9559111595, 45.8179931641]
]
]
};
var latLngGeom = [],
r, k;
for (r = 0; r < testGeom.coordinates.length; r++) {
latLngGeom[r] = [];
for (k = 0; k < testGeom.coordinates[r].length; k++) {
latLngGeom[r].push(new L.LatLng(testGeom.coordinates[r][k][1], testGeom.coordinates[r][k][0]));
}
};
var layerBA = new BingLayer('http://t{s}.tiles.virtualearth.net/tiles/a{q}.jpeg?g=2732', {
maxZoom: 19,
subdomains: ['0', '1', '2', '3', '4'],
attribution: '© <a href="http://bing.com/maps">Bing Maps</a>'
});
var layer01 = new L.TileLayer.boundaryCanvas('http://{s}.tile.openstreetmap.fr/hot/{z}/{x}/{y}.png', {
boundary: latLngGeom,
attribution: 'Tiles © <a href="http://hot.openstreetmap.org/">Humanitarian OpenStreetMap Team</a>; Map data © <a href="http://www.openstreetmap.org/copyright">OpenStreetMap</a> contributors'
});
var layer02 = new L.TileLayer.boundaryCanvas('https://{s}.tile.openstreetmap.org/{z}/{x}/{y}.png', {
boundary: latLngGeom,
attribution: '© Map Data <a href="https://openstreetmap.org/copyright">OpenStreetMap</a> contributors'
});
var layer03 = new L.TileLayer.boundaryCanvas("http://{s}.mqcdn.com/tiles/1.0.0/osm/{z}/{x}/{y}.png", {
subdomains: ["otile1", "otile2", "otile3", "otile4"],
boundary: latLngGeom,
attribution: '© Tiles courtesy of <a href="http://www.mapquest.com/" target="_blank">MapQuest</a>. © Map data <a href="http://www.openstreetmap.org/" target="_blank">OpenStreetMap</a> contributors'
});
var layer04 = new L.TileLayer.boundaryCanvas('http://{s}.tile.thunderforest.com/landscape/{z}/{x}/{y}.png', {
boundary: latLngGeom,
attribution: '© <a href="http://www.opencyclemap.org">OpenCycleMap</a>, © <a href="http://openstreetmap.org">OpenStreetMap</a> contributors, <a href="http://creativecommons.org/licenses/by-sa/2.0/">CC-BY-SA</a>'
});
var layer05 = new L.TileLayer.boundaryCanvas('http://{s}.hikebike.gpsies.com/{z}/{x}/{y}.png', {
boundary: latLngGeom,
attribution: '© <a href="http://hikebikemap.org/">HikeBikeMap</a>, © <a href="http://openstreetmap.org">OpenStreetMap</a> contributors, <a href="http://creativecommons.org/licenses/by-sa/2.0/">CC-BY-SA</a>'
});
var map = L.map('map', {
layers: [layerBA],
zoom: 8,
minZoom: 5,
maxZoom: 19,
center: [47, 8]
});
var overlay = layer01.addTo(map);
$('.button').click(function() {
$('.button').removeClass('selected');
$(this).addClass('selected');
if ($(this).attr('id') === "a1") {
overlay = layer01.addTo(map);
} else {
map.removeLayer(layer01);
};
if ($(this).attr('id') === "a2") {
overlay = layer02.addTo(map);
} else {
map.removeLayer(layer02);
};
if ($(this).attr('id') === "a3") {
overlay = layer03.addTo(map);
} else {
map.removeLayer(layer03);
};
if ($(this).attr('id') === "a4") {
overlay = layer04.addTo(map);
} else {
map.removeLayer(layer04);
};
if ($(this).attr('id') === "a5") {
overlay = layer05.addTo(map);
} else {
map.removeLayer(layer05);
};
});
var range = document.getElementById('range');
function clip() {
var nw = map.containerPointToLayerPoint([0, 0]),
se = map.containerPointToLayerPoint(map.getSize()),
clipX = nw.x + (se.x - nw.x) * range.value;
overlay.getContainer().style.clip = 'rect(' + [nw.y, clipX, se.y, nw.x].join('px,') + 'px)';
}
range['oninput' in range ? 'oninput' : 'onchange'] = clip;
map.on('move', clip);
map.setView([47, 8.1], 8);
</script>
Best Answer
This is not trivial, due to how web browsers handle DOM elements. If you have two (or more) overlapping (not nested) elements, pointer events will only apply to the top-most element. This is a limitation of the DOM model.
Two approaches to the problem spring to mind:
1 - Use GIS magic to calculate the intersection of all your different features. Create a feature layer with small polygons, each of which contains the properties of all your 4 feature layers.
i.e. each polygon should contain information about schools, counties, cities and misc; and each combination of schools, counties, cities and misc has its own polygon.
2 - Use something like
Leaflet.CheapLayerAt
to automate querying displayed vector data on the screen. When the user clicks on a feature on the map, remove that feature layer, run aLeaflet.CheapLayerAt
query. That should return the feature from the underlying feature layer. Repeat this process until no more features are found, then re-add all the feature layers.Yes, if you use this method you must remove and add layers. Or send them to the back of the stacking context so other layer is the top-most one when querying again.