ol-ext/examples/control/map.control.select.html
2024-06-26 16:24:06 +02:00

147 lines
4.2 KiB
HTML

<!DOCTYPE html>
<html>
<head>
<!--
Copyright (c) 2017-2018 Jean-Marc VIGLINO,
released under CeCILL-B (french BSD like) licence: http://www.cecill.info/
-->
<title>ol-ext: Select control</title>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<meta name="description" content="Control to select features by attributes ." />
<meta name="keywords" content="ol3, control, select, vector, feature, attribute" />
<!-- FontAwesome -->
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/4.7.0/css/font-awesome.min.css">
<!-- 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"/>
</head>
<body >
<a href="https://github.com/Viglino/ol-ext" class="icss-github-corner"><i></i></a>
<a href="../../index.html">
<h1>ol-ext: Select control</h1>
</a>
<div class="info">
<i>ol.control.Select</i> is a control to select features by attributes in a source.
<ul>
<li>
You have to define a <i>ol.source.Vector</i> to search in or an array.
</li>
<li>
A <i>select</i> event is fired when features are selected.
</li>
</ul>
</div>
<!-- DIV pour la carte -->
<div id="map" style="width:600px; height:400px;"></div>
<div class="options" style="max-width:20em;">
<h2>Options:</h2>
<i>Click on the map to select a feature / use the control to look for features by attributes.</i>
<ul style="list-style:initial; padding: 0 1em;"></ul>
</div>
<script type="text/javascript">
// Layers
var layers = [
new ol.layer.Geoportail('ORTHOIMAGERY.ORTHOPHOTOS')
]
// The map
var map = new ol.Map({
target: 'map',
view: new ol.View({
zoom: 5,
center: [166326, 5992663]
}),
layers: layers
});
// GeoJSON layer
var vectorSource = new ol.source.Vector({
url: '../data/departements.geojsonx',
format: new ol.format.GeoJSONX(),
attributions: [ "&copy; <a href='https://www.insee.fr'>INSEE</a>", "&copy; <a href='https://www.data.gouv.fr/fr/datasets/geofla-r/'>IGN</a>" ],
});
map.addLayer(new ol.layer.Vector({
name: 'Departements',
source: vectorSource,
style: function(f) {
return new ol.style.Style({
image: new ol.style.RegularShape({
radius: 5,
radius2: 0,
points: 4,
stroke: new ol.style.Stroke({ color: "#000", width:1 })
}),
text: new ol.style.Text ({
text: f.get('id').toString(),
font: 'bold 11px sans-serif',
}),
stroke: new ol.style.Stroke({
width: 1,
color: [255,128,0]
}),
fill: new ol.style.Fill({
color: [255,128,0,.2]
})
})
}
}));
// Select interaction
var selecti = new ol.interaction.Select({
hitTolerance: 5,
condition: ol.events.condition.singleClick
});
map.addInteraction(selecti);
// Select feature when click on the reference index
selecti.on('select', function(e) {
var f = e.selected[0];
if (f) {
var prop = f.getProperties();
var ul = $('.options ul').html('');
for (var p in prop) if (p!=='geometry') {
$('<li>').text(p+': '+prop[p]).appendTo(ul);
}
}
});
// Select control
var selectCtrl = new ol.control.Select({
// target: $(".options").get(0),
source: vectorSource,
property: $(".options select").val()
});
map.addControl (selectCtrl);
selectCtrl.on('select', function(e) {
console.log(e);
selecti.getFeatures().clear();
for (var i=0, f; f=e.features[i]; i++) {
selecti.getFeatures().push(f);
}
});
</script>
</body>
</html>