Simple click feature
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>Selected: {{ selected }}</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 :features.sync="features">
<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-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": [10, 50],
},
},
],
}
},
methods: {
onMapPointerMove ({ pixel }) {
let hit = this.$refs.map.forEachFeatureAtPixel(pixel, () => true)
if (hit) {
this.mapCursor = 'pointer'
} else {
this.mapCursor = 'default'
}
},
},
})