Select delayed features
by Vladimir Vershinin
HTML
<script src="https://unpkg.com/[email protected]/lodash.js"></script>
<script src="https://unpkg.com/vue@latest/dist/vue.min.js"></script>
<script src="https://cdn.jsdelivr.net/gh/openlayers/openlayers.github.io/en/v5.3.0/build/ol.js"></script>
<script src="https://unpkg.com/[email protected]/lib/index.umd.js"></script>
<link rel="stylesheet" href="https://unpkg.com/[email protected]/lib/style.css">
<div id="app">
<vl-map ref="map" data-projection="EPSG:4326">
<aside style="position: absolute; right: 0.75rem; top: 0.75rem; width: 320px; background-color: white; box-shadow: 1px 1px 2px rgba(0, 0, 0, 0.5); z-index: 1; padding: 0.75rem; border-radius: 0.25rem;">
<form style="overflow: hidden;">
<button @click.prevent="mode = 'draw'" :disabled="mode == 'draw'" style="width: 100%">Draw a point</button>
<ul v-if="selectedFeatures.length > 0">
<li v-for="feature in selectedFeatures" :key="feature.id">
{{ feature.id }}
</li>
</ul>
</form>
</aside>
<vl-view ref="view" :zoom.sync="zoom" :center.sync="center"></vl-view>
<vl-layer-tile>
<vl-source-osm />
</vl-layer-tile>
<vl-layer-vector>
<vl-source-vector ident="draw-target" :features.sync="features" />
</vl-layer-vector>
<vl-interaction-select ref="interaction" v-if="mode === 'select'" :features.sync="selectedFeatures" @select="onSelect">
<template slot-scope="select">
<vl-style-box :z-index="1">
<vl-style-stroke color="#423e9e" :width="7"></vl-style-stroke>
<vl-style-fill :color="[254, 178, 76, 0.7]"></vl-style-fill>
<vl-style-circle :radius="5">
<vl-style-stroke color="#423e9e" :width="7"></vl-style-stroke>
<vl-style-fill :color="[254, 178, 76, 0.7]"></vl-style-fill>
</vl-style-circle>
</vl-style-box>
<vl-style-box :z-index="1">
<vl-style-stroke color="#d43f45" :width="2"></vl-style-stroke>
...
CSS
html,
body,
#app {
width: 100%;
height: 100%;
margin: 0;
padding: 0;
}
#app {
display: flex;
flex-direction: column;
}
.vl-map {
overflow: hidden;
}
JavaScript
new Vue({
el: '#app',
data() {
return {
zoom: 2,
center: [0, 0],
mode: 'draw',
features: [],
selectedFeatures: [],
drawType: 'point'
}
},
computed: {
layerFeatureIds() {
return this.features.map(f => f.id)
},
featuresState() {
if (this.features.length) {
return 'Loaded: ' + this.layerFeatureIds.length
}
return 'Loading....'
},
},
mounted() {
this.loadFeatures().then(features => console.log('loaded', features))
},
methods: {
onDrawEnd(event) {
this.mode = 'select'
console.log(this.features.find(f => f.id === event.feature.getId()))
this.$nextTick(() => {
this.$refs.interaction.select(event.feature)
})
},
onSelect(event) {
console.log(event)
},
selectCondition: ol.events.condition.click,
loadFeatures() {
return new Promise(resolve => {
setTimeout(() => {
this.features = _.range(0, 100).map(i => ({
type: 'Feature',
id: 'point-' + (i + 1),
properties: {},
geometry: {
type: 'Point',
coordinates: [
_.random(-50, 50),
_.random(-50, 50),
],
},
}))
resolve(this.features)
}, 3000)
})
},
}
})