Select style
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">
<vl-map ref="map">
<vl-view ref="mapView" :zoom.sync="zoom" :center.sync="center"></vl-view>
<vl-layer-tile>
<vl-source-osm />
</vl-layer-tile>
<vl-layer-vector>
<vl-source-vector ref="vectorSource">
<vl-feature v-for="feature in geojsonObject.features" :key="feature.id"
:id="feature.id" :properties="feature.properties">
<vl-geom-polygon :coordinates="feature.geometry.coordinates" />
</vl-feature>
</vl-source-vector>
<vl-style-func :factory="styleFuncFactory" />
</vl-layer-vector>
<vl-interaction-select :features.sync="selectedFeatures" :condition="pointerMoveCondition">
<vl-style-func :factory="styleFuncFactory"></vl-style-func>
</vl-interaction-select>
</vl-map>
</div>
CSS
html, body, #app {
width: 100%;
height: 100%;
margin: 0;
padding: 0;
}
JavaScript
let geojsonObject = {
'type': 'FeatureCollection',
'crs': {
'type': 'name',
'properties': {
'name': 'EPSG:3857'
}
},
'features': [{
'id': 1,
'type': 'Feature',
'geometry': {
'type': 'Polygon',
'coordinates': [[[-5e6, 6e6], [-5e6, 8e6], [-3e6, 8e6],
[-3e6, 6e6], [-5e6, 6e6]]]
}
}, {
'id': 2,
'type': 'Feature',
'geometry': {
'type': 'Polygon',
'coordinates': [[[-2e6, 6e6], [-2e6, 8e6], [0, 8e6],
[0, 6e6], [-2e6, 6e6]]]
}
}, {
'id': 3,
'type': 'Feature',
'geometry': {
'type': 'Polygon',
'coordinates': [[[1e6, 6e6], [1e6, 8e6], [3e6, 8e6],
[3e6, 6e6], [1e6, 6e6]]]
}
}, {
'id': 4,
'type': 'Feature',
'geometry': {
'type': 'Polygon',
'coordinates': [[[-2e6, -1e6], [-1e6, 1e6],
[0, -1e6], [-2e6, -1e6]]]
}
}]
};
new Vue({
el: '#app',
data () {
return {
zoom: 5,
center: [0, 3000000],
geojsonObject: geojsonObject,
selectedFeatures: [],
}
},
mounted () {
setTimeout(() => {
this.$refs.mapView.$view.fit(this.$refs.vectorSource.$source.getExtent(), {
size: this.$refs.map.$map.getSize(),
duration: 1000,
})
}, 1000)
},
methods: {
pointerMoveCondition: ol.events.condition.pointerMove,
styleFuncFactory () {
const fill = new ol.style.Fill({
color: 'rgba(0, 0, 255, 0.1)'
})
const fillSelected = new ol.style.Fill({
color: 'rgba(50, 150, 255, 0.5)'
})
const stroke = new ol.style.Stroke({
color: 'blue',
width: 3
})
const circleStyle = new ol.style.Circle({
radius: 5,
fill: new ol.style.Fill({
color: 'orange'
})
})
return feature => {
const selected = this.selectedFeatures.filter(f => f.id === feature.getId()).length > 0
const...