ol-ext/examples/control/map.control.select.html
2018-10-27 11:36:18 +02:00

163 lines
4.9 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://openlayers.org/en/latest/css/ol.css" />
<script type="text/javascript" src="https://openlayers.org/en/latest/build/ol.js"></script>
<!-- ol-ext -->
<link rel="stylesheet" href="../../dist/ol-ext.css" />
<script type="text/javascript" src="../../dist/ol-ext.js"></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: 10em;">
<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.Tile({
name: "Natural Earth",
minResolution: 306,
source: new ol.source.XYZ(
{ url: 'https://{a-d}.tiles.mapbox.com/v3/mapbox.natural-earth-hypso-bathy/{z}/{x}/{y}.png',
attributions: [ '&copy; <a href="https://www.mapbox.com/map-feedback/">Mapbox</a> ' ]
})
})
]
// The map
var map = new ol.Map({
target: 'map',
view: new ol.View({
zoom: 5,
center: [166326, 5992663]
}),
interactions: ol.interaction.defaults({ altShiftDragRotate:false, pinchRotate:false }),
layers: layers
});
// GeoJSON layer
var vectorSource = new ol.source.Vector({
url: '../data/fond_guerre.geojson',
projection: 'EPSG:3857',
format: new ol.format.GeoJSON(),
attributions: [ "&copy; <a href='https://www.data.gouv.fr/fr/datasets/fonds-de-la-guerre-14-18-extrait-de-la-base-memoire/'>data.gouv.fr</a>" ],
logo:"https://www.data.gouv.fr/s/avatars/37/e56718abd4465985ddde68b33be1ef.jpg"
});
map.addLayer(new ol.layer.Vector({
name: 'Fonds de guerre 14-18',
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('random').toString(),
font: 'bold 11px sans-serif',
textAlign: 'left',
textBaseline: 'bottom',
offsetX: 3
})
})
}
}));
var listenerKey = vectorSource.on('change', function(e) {
if (vectorSource.getState() == 'ready') {
ol.Observable.unByKey(listenerKey);
var features = vectorSource.getFeatures();
// Set numeric values to test
for (var i=0, f; f=features[i]; i++) {
f.set('id', i);
f.set('random', Math.round(Math.random()*100));
}
}
});
// 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 selectc = new ol.control.Select({
// target: $(".options").get(0),
source: vectorSource,
property: $(".options select").val()
});
map.addControl (selectc);
selectc.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>