Hover cursor tooltip
by Leespiker
HTML
<script src="https://unpkg.com/[email protected]/dist/vue.js"></script>
<script src="https://unpkg.com/[email protected]/dist/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.min.css">
<script src="https://rawgit.com/icebob/fakerator/master/dist/fakerator.js"></script>
<div id="app">
<button @click="toggle">
Toggle
</button>
<vl-map ref="map" data-projection="EPSG:4326" @pointermove="onMapPointerMove" :style="{cursor: mapCursor}" v-if="showMap">
<vl-view :zoom.sync="zoom" :center.sync="center"></vl-view>
<vl-layer-tile>
<vl-source-osm></vl-source-osm>
</vl-layer-tile>
<vl-layer-vector ref="featuresLayer">
<vl-source-vector :features="features"></vl-source-vector>
</vl-layer-vector>
<vl-overlay v-if="currentPosition" :position="currentPosition">
<template>
<div style="background: #fff; box-shadow: 2px 2px 10px rgba(2,2,2,0.1); padding: 2px">
{{ currentName }}
</div>
</template>
</vl-overlay>
</vl-map>
</div>
CSS
html, body, #app {
width: 100%;
height: 100%;
margin: 0;
padding: 0;
}
JavaScript
Vue.use(VueLayers)
let fakerator = Fakerator()
// external loaded features
let features = [
{
"type": "Feature",
"id": 1,
"properties": {
"special": true,
name: 'one'
},
"geometry": {
"type": "Point",
"coordinates": [
-30.761718749999996,
58.90464570302001
]
}
},
{
"type": "Feature",
"id": 2,
"properties": {
"special": true,
name: 'two'
},
"geometry": {
"type": "Point",
"coordinates": [
36.73828124999999,
44.59046718130883
]
}
},
{
"type": "Feature",
"id": 3,
"properties": {
"special": false,
name: 'three'
},
"geometry": {
"type": "Point",
"coordinates": [
-23.37890625,
32.10118973232094
]
}
}
]
new Vue({
el: '#app',
methods: {
onMapPointerMove ({ pixel }) {
let hitFeature = this.$refs.map.forEachFeatureAtPixel(pixel, feature => feature)
if (hitFeature) {
this.mapCursor = 'pointer'
this.currentPosition = ol.proj.transform(hitFeature.getGeometry().getCoordinates(), 'EPSG:3857', 'EPSG:4326')
this.currentName = hitFeature.get('name')
} else {
this.mapCursor = 'default'
this.currentPosition = this.currentName = undefined
}
},
toggle () {
this.showMap = !this.showMap
}
},
data () {
return {
zoom: 3,
center: [-10, 35],
features: features,
currentPosition: undefined,
currentName: undefined,
mapCursor: 'default',
showMap: true
}
},
})