Published
Edited
Oct 20, 2018
2 stars
Insert cell
Insert cell
selection.map(f => f.getId())
Insert cell
viewof selection = {
const target = html`<div style="height:600px;">`;
yield target; // Give the container dimensions.

const vectorSource = new ol.source.Vector({
url: "https://openlayers.org/en/v5.2.0/examples/data/geojson/countries.geojson",
format: new ol.format.GeoJSON()
});

const map = new ol.Map({
layers: [
new ol.layer.Tile({
source: new ol.source.OSM()
}),
new ol.layer.Vector({
source: vectorSource
})
],
target,
view: new ol.View({
center: [0, 0],
zoom: 2
})
});

target.value = []; // Initial empty selection.

// Select by click.
const select = new ol.interaction.Select();
const selection = select.getFeatures();
function selected() {
target.value = selection.getArray();
target.dispatchEvent(new CustomEvent("input"));
}
select.on("select", selected);
map.addInteraction(select);

// Select by dragging a box.
const dragBox = new ol.interaction.DragBox({
condition: ol.events.condition.platformModifierKeyOnly
});
dragBox.on("boxstart", () => {
selection.clear();
});
dragBox.on("boxend", () => {
const e = dragBox.getGeometry().getExtent();
vectorSource.forEachFeatureIntersectingExtent(e, f => void selection.push(f));
selected();
});
map.addInteraction(dragBox);

invalidation.then(() => map.dispose());
return target;
}
Insert cell
ol = {
// OpenLayers doesn’t publish a build to npm, so we can’t use unpkg. :(
const ol = await require("https://openlayers.org/en/v5.2.0/build/ol.js").catch(() => window.ol);
if (!ol.css) ol.css = document.head.appendChild(html`<link rel=stylesheet href="https://openlayers.org/en/v5.2.0/css/ol.css">`);
return ol;
}
Insert cell
Insert cell

Purpose-built for displays of data

Observable is your go-to platform for exploring data and creating expressive data visualizations. Use reactive JavaScript notebooks for prototyping and a collaborative canvas for visual data exploration and dashboard creation.
Learn more