Display country name on click select

And not only when the box selection is used
This commit is contained in:
Frederic Junod
2017-05-12 09:56:18 +02:00
parent 7c5a3ae311
commit 026fcc66c9

View File

@@ -45,29 +45,29 @@ var dragBox = new ol.interaction.DragBox({
map.addInteraction(dragBox); map.addInteraction(dragBox);
var infoBox = document.getElementById('info');
dragBox.on('boxend', function() { dragBox.on('boxend', function() {
// features that intersect the box are added to the collection of // features that intersect the box are added to the collection of
// selected features, and their names are displayed in the "info" // selected features
// div
var info = [];
var extent = dragBox.getGeometry().getExtent(); var extent = dragBox.getGeometry().getExtent();
vectorSource.forEachFeatureIntersectingExtent(extent, function(feature) { vectorSource.forEachFeatureIntersectingExtent(extent, function(feature) {
selectedFeatures.push(feature); selectedFeatures.push(feature);
info.push(feature.get('name'));
}); });
if (info.length > 0) {
infoBox.innerHTML = info.join(', ');
}
}); });
// clear selection when drawing a new box and when clicking on the map // clear selection when drawing a new box and when clicking on the map
dragBox.on('boxstart', function() { dragBox.on('boxstart', function() {
selectedFeatures.clear(); selectedFeatures.clear();
infoBox.innerHTML = ' ';
}); });
map.on('click', function() {
selectedFeatures.clear(); var infoBox = document.getElementById('info');
infoBox.innerHTML = ' ';
selectedFeatures.on(['add', 'remove'], function() {
var names = selectedFeatures.getArray().map(function(feature) {
return feature.get('name');
});
if (names.length > 0) {
infoBox.innerHTML = names.join(', ');
} else {
infoBox.innerHTML = 'No countries selected';
}
}); });