mirror of
https://github.com/Viglino/ol-ext.git
synced 2025-12-08 19:26:29 +00:00
318 lines
9.8 KiB
HTML
318 lines
9.8 KiB
HTML
<!DOCTYPE html>
|
|
<html>
|
|
<head>
|
|
<!--
|
|
Copyright (c) 2015 Jean-Marc VIGLINO,
|
|
released under CeCILL-B (french BSD like) licence: http://www.cecill.info/
|
|
-->
|
|
<title>ol-ext: Layerswitcher</title>
|
|
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
|
|
|
|
<meta name="description" content="An OL3 layerswitcher." />
|
|
<meta name="keywords" content="ol3, layer, layerswitcher, control,jQuery" />
|
|
|
|
<!-- jQuery -->
|
|
<script type="text/javascript" src="https://code.jquery.com/jquery-1.11.0.min.js"></script>
|
|
|
|
<!-- Openlayers -->
|
|
<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/ol@latest/ol.css" />
|
|
<script type="text/javascript" src="https://cdn.jsdelivr.net/npm/ol@latest/dist/ol.js"></script>
|
|
|
|
|
|
<!-- ol-ext -->
|
|
<link rel="stylesheet" href="../../dist/ol-ext.css" />
|
|
<script type="text/javascript" src="../../dist/ol-ext.js"></script>
|
|
<!-- Pointer events polyfill for old browsers, see https://caniuse.com/#feat=pointer -->
|
|
<script src="https://unpkg.com/elm-pep"></script>
|
|
|
|
<link rel="stylesheet" href="../style.css" />
|
|
<style>
|
|
.hideOpacity .layerswitcher-opacity {
|
|
display:none;
|
|
}
|
|
.hideOpacity .ol-layerswitcher .layerup {
|
|
height: 1.5em;
|
|
}
|
|
.showPercent .layerSwitcher .ol-layerswitcher .layerswitcher-opacity-label {
|
|
display: block;
|
|
}
|
|
|
|
.ol-header > div {
|
|
width:100%;
|
|
}
|
|
.toggleVisibility {
|
|
padding-left: 1.6em;
|
|
cursor: pointer;
|
|
border-bottom: 2px solid #369;
|
|
margin-bottom: 0.5em;
|
|
}
|
|
.toggleVisibility:before {
|
|
background-color: #fff;
|
|
border: 2px solid #369;
|
|
box-sizing: border-box;
|
|
content: "";
|
|
display: block;
|
|
height: 1.2em;
|
|
left: 0.1em;
|
|
margin: 0;
|
|
position: absolute;
|
|
width: 1.2em;
|
|
}
|
|
.toggleVisibility.show:before {
|
|
background: #369;
|
|
}
|
|
#map:focus-visible,
|
|
#map button:focus-visible {
|
|
/*
|
|
box-shadow: inset 0 0 3px#369;
|
|
*/
|
|
outline: 2px solid #369;
|
|
}
|
|
#map .ol-layerswitcher > button {
|
|
margin: 2px;
|
|
}
|
|
.ol-layerswitcher li:has(> div.li-content > .ol-visibility:focus) > div > .layerInfo:after,
|
|
.ol-layerswitcher li:has(> div.li-content > .ol-visibility:focus) > div > .layerTrash:after,
|
|
.ol-layerswitcher li:has(> div.li-content > .ol-visibility:focus) > div > .myInfo:after {
|
|
content: "i";
|
|
position: relative;
|
|
top: 10px;
|
|
left: 3px;
|
|
background: red;
|
|
padding: 0 5px;
|
|
font-size: 13px;
|
|
}
|
|
.ol-layerswitcher li:has(> div.li-content > .ol-visibility:focus) > div > .layerTrash:after {
|
|
content: "x";
|
|
}
|
|
.ol-layerswitcher li:has(> div.li-content > .ol-visibility:focus) > div > .myInfo:after {
|
|
content: "?";
|
|
}
|
|
kbd {
|
|
display: inline-block;
|
|
border: 1px solid #aaa;
|
|
padding: 0.1em .25em;
|
|
border-radius: 3px;
|
|
background-color: #e0e0e0;
|
|
box-shadow: inset -1px -1px 2px #bbb, inset 2px 2px 2px #efefef;
|
|
font-style: normal;
|
|
}
|
|
</style>
|
|
</head>
|
|
<body >
|
|
<a href="https://github.com/Viglino/ol-ext" class="icss-github-corner"><i></i></a>
|
|
|
|
<a href="../../index.html">
|
|
<h1>ol-ext: LayerSwitcher control</h1>
|
|
</a>
|
|
<div class="info">
|
|
Add a layer switcher inside or outside the map.
|
|
<ul>
|
|
<li>
|
|
The <i>title</i> or <i>name</i> layer's property is used to named the ol.layer in the switcher.
|
|
</li>
|
|
<li>
|
|
It shows use of the <i>displayInLayerSwitcher</i> layer's property to cause it to not display in the LayerSwitcher
|
|
</li>
|
|
<li>
|
|
The <i>baseLayer</i> layer's property manage exclusive visibility on layers (only one base layer is shown at a time).
|
|
</li>
|
|
<li>
|
|
The <i>allwaysOnTop</i> layer's property make a layer to stay on top of others.
|
|
</li>
|
|
<li>
|
|
Layers in an ol.layer.Group are grouped together and can be managed all together (visibility/opacity/ordering).
|
|
</li>
|
|
<li>
|
|
The <i>openInLayerSwitcher</i> property of an ol.layer.Group is used to code the visibility of the sublayers.
|
|
</li>
|
|
<li>
|
|
The <i>show_progress</i> option display a progress bar on tiled layers (only once per map).
|
|
</li>
|
|
<li>
|
|
You can use the <i>setHeader()</i> function to add a header to the layer list.
|
|
</li>
|
|
<li>
|
|
You can use the <i>drawlist</i> event handle the current line display (add button, change list style, etc.)
|
|
</li>
|
|
<li>
|
|
It triggers a <i>toogle</i> event when shown/hidden
|
|
</li>
|
|
<li>
|
|
It triggers a <i>layer:keydown</i> event when a key is pressed on the layer
|
|
</li>
|
|
</ul>
|
|
<p>
|
|
<a href="map.switcher2.html">You can customize the LayerSwitcher using CSS</a>
|
|
</p>
|
|
</div>
|
|
|
|
<!-- Map div -->
|
|
<div id="map" style="width:600px; height:400px;" tabindex="0"></div>
|
|
<div class="external layerSwitcher"><b>External layer switcher</b></div>
|
|
|
|
<div class="options">
|
|
<h2>Options:</h2>
|
|
<input id="dils" type="checkbox" checked="checked" onchange="displayInLayerSwitcher(this.checked);"/>
|
|
<label for="dils">display "Natural Earth" in LayerSwitcher (zoom out to make it visible).</label>
|
|
<br/>
|
|
<input id="opb" type="checkbox" onchange="$('body').toggleClass('hideOpacity');" />
|
|
<label for="opb">hide opacity bar</label>
|
|
<br/>
|
|
<input id="percent" type="checkbox" onchange="$('body').toggleClass('showPercent');" />
|
|
<label for="percent">show percent information</label>
|
|
<p><i>
|
|
Use <kbd>Tab</kbd> to navigate in the layer switcher, <kbd>⇐</kbd> and <kbd>⇒</kbd> to change opacity, <kbd>Ctrl</kbd>+<kbd>⇑</kbd> or <kbd>⇓</kbd> to move up / down in the list.
|
|
</i></p>
|
|
</div>
|
|
|
|
<script type="text/javascript">
|
|
|
|
// A group layer for base layers
|
|
var baseLayers = new ol.layer.Group({
|
|
title: 'Base Layers',
|
|
openInLayerSwitcher: true,
|
|
layers: [
|
|
new ol.layer.Geoportail({
|
|
baseLayer: true,
|
|
visible: false,
|
|
layer: 'ORTHOIMAGERY.ORTHOPHOTOS'
|
|
}),
|
|
new ol.layer.Geoportail({
|
|
baseLayer: true,
|
|
layer: 'GEOGRAPHICALGRIDSYSTEMS.PLANIGNV2'
|
|
}),
|
|
new ol.layer.Tile({
|
|
title: "OSM",
|
|
baseLayer: true,
|
|
source: new ol.source.OSM(),
|
|
visible: false
|
|
})
|
|
]
|
|
});
|
|
// A layer with minResolution (hidden on hight zoom level)
|
|
var nearth = new ol.layer.Geoportail({
|
|
layer: 'CARTES.NATURALEARTH',
|
|
maxZoom: 6
|
|
})
|
|
// An overlay that stay on top
|
|
var labels = new ol.layer.Geoportail('GEOGRAPHICALNAMES.NAMES');
|
|
// WMS with bbox
|
|
var brgm = new ol.layer.Tile ({
|
|
"title": "GEOLOGIE",
|
|
"extent": [
|
|
-653182.6969582437,
|
|
5037463.842847037,
|
|
1233297.5065495989,
|
|
6646432.677299531
|
|
],
|
|
"minResolution": 3.527777777777778,
|
|
"maxResolution": 3527.777777777778,
|
|
"source": new ol.source.TileWMS({
|
|
"url": "https://geoservices.brgm.fr/geologie",
|
|
"projection": "EPSG:3857",
|
|
"params": {
|
|
"LAYERS": "GEOLOGIE",
|
|
"FORMAT": "image/png",
|
|
"VERSION": "1.3.0"
|
|
},
|
|
"attributions": [
|
|
"<a href='http://www.brgm.fr/'>© Brgm</a>"
|
|
]
|
|
})
|
|
});
|
|
|
|
// The Map
|
|
var map = new ol.Map({
|
|
target: 'map',
|
|
view: new ol.View({
|
|
zoom: 11,
|
|
center: [260497, 6249720]
|
|
}),
|
|
layers: [ baseLayers, nearth, brgm, labels ]
|
|
});
|
|
// Add control inside the map
|
|
var ctrl = new ol.control.LayerSwitcher({
|
|
// collapsed: false,
|
|
// mouseover: true
|
|
});
|
|
map.addControl(ctrl);
|
|
ctrl.on('toggle', function(e) {
|
|
console.log('Collapse layerswitcher', e.collapsed);
|
|
});
|
|
|
|
// Add a layer switcher outside the map
|
|
var switcher = new ol.control.LayerSwitcher({
|
|
target:$(".layerSwitcher").get(0),
|
|
// displayInLayerSwitcher: function (l) { return false; },
|
|
show_progress:true,
|
|
// reordering: false,
|
|
extent: true,
|
|
trash: true,
|
|
oninfo: function (l) { alert(l.get("title")); }
|
|
});
|
|
// Add a new button to the list
|
|
switcher.on('drawlist', function(e) {
|
|
var layer = e.layer;
|
|
$('<div class="myInfo">').text('?')// addClass('layerInfo')
|
|
.click(function(){
|
|
alert(layer.get('title'));
|
|
})
|
|
.appendTo($('> .ol-layerswitcher-buttons', e.li));
|
|
});
|
|
// Add a button to show/hide the layers
|
|
var button = $('<div class="toggleVisibility" title="show/hide">')
|
|
.text("Show/hide all")
|
|
.click(function() {
|
|
var a = map.getLayers().getArray();
|
|
var b = !a[0].getVisible();
|
|
if (b) button.removeClass("show");
|
|
else button.addClass("show");
|
|
for (var i=0; i<a.length; i++) {
|
|
a[i].setVisible(b);
|
|
}
|
|
});
|
|
switcher.setHeader($('<div>').append(button).get(0))
|
|
|
|
// Accessibility keys
|
|
switcher.on('layer:keydown', function (e) {
|
|
switch(e.key.toLowerCase()) {
|
|
// Show info
|
|
case '?':
|
|
case 'i': {
|
|
alert(e.layer.get('title'))
|
|
break;
|
|
}
|
|
// Zoom to extent
|
|
case 'e': {
|
|
var k = e.li.querySelector('.layerExtent')
|
|
if (k) k.click();
|
|
break;
|
|
}
|
|
// Remove layer
|
|
case 'x': {
|
|
if (e.group) {
|
|
e.group.getLayers().remove(e.layer)
|
|
} else {
|
|
map.removeLayer(e.layer)
|
|
}
|
|
break;
|
|
}
|
|
}
|
|
})
|
|
|
|
map.addControl(switcher);
|
|
// Insert nearth layer in layer switcher
|
|
function displayInLayerSwitcher(b) {
|
|
nearth.set('displayInLayerSwitcher', b);
|
|
}
|
|
|
|
// Get options values
|
|
if ($("#opb").prop("checked")) $('body').addClass('hideOpacity');
|
|
if ($("#percent").prop("checked")) $('body').addClass('showPercent');
|
|
if ($("#dils").prop("checked")) displayInLayerSwitcher(true);
|
|
|
|
</script>
|
|
|
|
</body>
|
|
</html> |