Update box selection example
This commit is contained in:
@@ -1,4 +1,3 @@
|
|||||||
.ol-dragbox {
|
#map {
|
||||||
background-color: rgba(255,255,255,0.4);
|
background: #1a2b39;
|
||||||
border-color: rgba(100,150,0,1);
|
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -9,4 +9,4 @@ docs: >
|
|||||||
tags: "DragBox, feature, selection, box"
|
tags: "DragBox, feature, selection, box"
|
||||||
---
|
---
|
||||||
<div id="map" class="map"></div>
|
<div id="map" class="map"></div>
|
||||||
<div id="info">No countries selected</div>
|
<div>Selected regions: <span id="info">None</span></div>
|
||||||
|
|||||||
@@ -1,23 +1,32 @@
|
|||||||
import GeoJSON from '../src/ol/format/GeoJSON.js';
|
import GeoJSON from '../src/ol/format/GeoJSON.js';
|
||||||
import Map from '../src/ol/Map.js';
|
import Map from '../src/ol/Map.js';
|
||||||
|
import VectorLayer from '../src/ol/layer/Vector.js';
|
||||||
|
import VectorSource from '../src/ol/source/Vector.js';
|
||||||
import View from '../src/ol/View.js';
|
import View from '../src/ol/View.js';
|
||||||
import {DragBox, Select} from '../src/ol/interaction.js';
|
import {DragBox, Select} from '../src/ol/interaction.js';
|
||||||
import {OSM, Vector as VectorSource} from '../src/ol/source.js';
|
import {Fill, Stroke, Style} from '../src/ol/style.js';
|
||||||
import {Tile as TileLayer, Vector as VectorLayer} from '../src/ol/layer.js';
|
|
||||||
import {platformModifierKeyOnly} from '../src/ol/events/condition.js';
|
import {platformModifierKeyOnly} from '../src/ol/events/condition.js';
|
||||||
|
|
||||||
const vectorSource = new VectorSource({
|
const vectorSource = new VectorSource({
|
||||||
url: 'data/geojson/countries.geojson',
|
url: 'https://openlayers.org/data/vector/ecoregions.json',
|
||||||
format: new GeoJSON(),
|
format: new GeoJSON(),
|
||||||
});
|
});
|
||||||
|
|
||||||
|
const style = new Style({
|
||||||
|
fill: new Fill({
|
||||||
|
color: 'rgba(255, 255, 255, 0.6)',
|
||||||
|
}),
|
||||||
|
});
|
||||||
|
|
||||||
const map = new Map({
|
const map = new Map({
|
||||||
layers: [
|
layers: [
|
||||||
new TileLayer({
|
|
||||||
source: new OSM(),
|
|
||||||
}),
|
|
||||||
new VectorLayer({
|
new VectorLayer({
|
||||||
source: vectorSource,
|
source: vectorSource,
|
||||||
|
style: function (feature) {
|
||||||
|
const color = feature.get('COLOR_BIO') || '#eeeeee';
|
||||||
|
style.getFill().setColor(color);
|
||||||
|
return style;
|
||||||
|
},
|
||||||
}),
|
}),
|
||||||
],
|
],
|
||||||
target: 'map',
|
target: 'map',
|
||||||
@@ -28,8 +37,24 @@ const map = new Map({
|
|||||||
}),
|
}),
|
||||||
});
|
});
|
||||||
|
|
||||||
|
const selectedStyle = new Style({
|
||||||
|
fill: new Fill({
|
||||||
|
color: 'rgba(255, 255, 255, 0.6)',
|
||||||
|
}),
|
||||||
|
stroke: new Stroke({
|
||||||
|
color: 'rgba(255, 255, 255, 0.7)',
|
||||||
|
width: 2,
|
||||||
|
}),
|
||||||
|
});
|
||||||
|
|
||||||
// a normal select interaction to handle click
|
// a normal select interaction to handle click
|
||||||
const select = new Select();
|
const select = new Select({
|
||||||
|
style: function (feature) {
|
||||||
|
const color = feature.get('COLOR_BIO') || '#eeeeee';
|
||||||
|
selectedStyle.getFill().setColor(color);
|
||||||
|
return selectedStyle;
|
||||||
|
},
|
||||||
|
});
|
||||||
map.addInteraction(select);
|
map.addInteraction(select);
|
||||||
|
|
||||||
const selectedFeatures = select.getFeatures();
|
const selectedFeatures = select.getFeatures();
|
||||||
@@ -85,11 +110,11 @@ const infoBox = document.getElementById('info');
|
|||||||
|
|
||||||
selectedFeatures.on(['add', 'remove'], function () {
|
selectedFeatures.on(['add', 'remove'], function () {
|
||||||
const names = selectedFeatures.getArray().map(function (feature) {
|
const names = selectedFeatures.getArray().map(function (feature) {
|
||||||
return feature.get('name');
|
return feature.get('ECO_NAME');
|
||||||
});
|
});
|
||||||
if (names.length > 0) {
|
if (names.length > 0) {
|
||||||
infoBox.innerHTML = names.join(', ');
|
infoBox.innerHTML = names.join(', ');
|
||||||
} else {
|
} else {
|
||||||
infoBox.innerHTML = 'No countries selected';
|
infoBox.innerHTML = 'None';
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
|||||||
@@ -17,12 +17,8 @@ const vectorLayer = new VectorLayer({
|
|||||||
format: new GeoJSON(),
|
format: new GeoJSON(),
|
||||||
}),
|
}),
|
||||||
style: function (feature) {
|
style: function (feature) {
|
||||||
const color = feature.get('COLOR');
|
const color = feature.get('COLOR') || '#eeeeee';
|
||||||
if (color) {
|
|
||||||
style.getFill().setColor(color);
|
style.getFill().setColor(color);
|
||||||
} else {
|
|
||||||
style.getFill().setColor('#eeeeee');
|
|
||||||
}
|
|
||||||
return style;
|
return style;
|
||||||
},
|
},
|
||||||
});
|
});
|
||||||
|
|||||||
Reference in New Issue
Block a user