Select sync workaround
by Vladimir Vershinin
HTML
<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">
<p>Center: {{ center }} Zoom: {{ zoom }}</p>
<p>Features: {{ features }} </p>
<p>Selected: {{ selectedFeatures }}</p>
<vl-map ref="map" data-projection="EPSG:4326" @pointermove="onMapPointerMove" :style="{cursor: mapCursor}">
<vl-view :zoom.sync="zoom" :center.sync="center"></vl-view>
<vl-layer-tile>
<vl-source-osm></vl-source-osm>
</vl-layer-tile>
<vl-interaction-select :features.sync="selected">
<vl-style-box>
<vl-style-icon src="https://img.icons8.com/ultraviolet/50/000000/place-marker.png" :anchor="[0.5, 1]"></vl-style-icon>
</vl-style-box>
</vl-interaction-select>
<vl-layer-vector>
<vl-source-vector>
<vl-feature v-for="feature in features" :key="feature" :id="feature.id">
<vl-geom-point :coordinates.sync="feature.geometry.coordinates"></vl-geom-point>
</vl-feature>
<vl-style-box>
<vl-style-icon src="https://img.icons8.com/office/50/000000/place-marker.png" :anchor="[0.5, 1]"></vl-style-icon>
</vl-style-box>
</vl-source-vector>
</vl-layer-vector>
<vl-overlay v-for="feature in selectedFeatures"
:key="feature.id"
:id="feature.id"
:position="feature.geometry.coordinates"
:auto-pan="true"
:auto-pan-animation="{ duration: 300 }">
<div style="background: grey">
{{ feature }}
</div>
</vl-overlay>
</vl-map>
<a href="https://icons8.com/icon/30622/place-marker">Place Marker icon by Icons8</a>
</div>
CSS
html, body, #app {
width: 100%;
height: 100%;
margin: 0;
padding: 0;
}
JavaScript
new Vue({
el: '#app',
data () {
return {
zoom: 3,
center: [25.25135774256233, 45.39334701133322],
selected: [],
mapCursor: 'default',
features: [
{
"type": "Feature",
"id": "marker",
"properties": {},
"geometry": {
"type": "Point",
"coordinates": [25.25135774256233, 45.39334701133322],
},
},
],
}
},
computed: {
selectedFeatureIds () {
return this.selected.map(feature => feature.id)
},
selectedFeatures () {
return this.features.filter(feature => this.selectedFeatureIds.includes(feature.id))
},
},
mounted () {
setInterval(() => {
let feature = this.features[0]
feature.geometry.coordinates = [
feature.geometry.coordinates[0] + Math.random(),
feature.geometry.coordinates[1] + Math.random()
]
}, 2000)
},
methods: {
onMapPointerMove ({ pixel }) {
let hit = this.$refs.map.forEachFeatureAtPixel(pixel, () => true)
if (hit) {
this.mapCursor = 'pointer'
} else {
this.mapCursor = 'default'
}
},
},
})