Published
Edited
Apr 25, 2019
Insert cell
Insert cell
md`## Click map to select a country. Output is shown in the array dump below.
`
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 map = new ol.Map({
layers: [
new ol.layer.Tile({
source: new ol.source.OSM()
}),
new ol.layer.Vector({
source: new ol.source.Vector({
url: "https://openlayers.org/en/v5.2.0/examples/data/geojson/countries.geojson",
format: new ol.format.GeoJSON()
})
})
],
target,
view: new ol.View({
center: [0, 0],
zoom: 2
})
});
target.value = []; // Initial empty selection.
const select = new ol.interaction.Select();
select.on("select", event => {
target.value = select.getFeatures().getArray();
target.dispatchEvent(new CustomEvent("input"));
})
map.addInteraction(select);
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

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