Points styling
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://npmcdn.com/@turf/turf/turf.min.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">
<div>Current zoom {{ zoom }}</div>
<vl-map ref="map" data-projection="EPSG:4326">
<vl-view ref="mapView" :zoom.sync="zoom" :center.sync="center"></vl-view>
<vl-layer-tile>
<vl-source-osm />
</vl-layer-tile>
<vl-layer-vector id="points" render-mode="image">
<vl-source-vector :features="points" />
<vl-style-func :factory="pointsStyleFunc" />
</vl-layer-vector>
</vl-map>
</div>
CSS
html, body, #app {
width: 100%;
height: 100%;
margin: 0;
padding: 0;
}
JavaScript
new Vue({
el: '#app',
data () {
return {
zoom: 2,
center: [0, 0],
points: _.range(0, 100).map(i => {
let coordinate = [
_.random(-50, 50),
_.random(-50, 50),
]
return {
type: 'Feature',
id: 'point-' + i,
geometry: {
type: 'Point',
coordinates: coordinate,
},
properties: {
number: i + 1,
address: '1234567, Russia, Moscow, ...',
},
}
}),
}
},
methods: {
pointsStyleFunc () {
// style function and styles using OpenLayers API
// https://openlayers.org/en/latest/apidoc/module-ol_style_Style.html
return feature => {
let baseStyle = new ol.style.Style({
image: new ol.style.Circle({
radius: 10,
fill: new ol.style.Fill({
color: '#fff',
}),
stroke: new ol.style.Stroke({
color: '#F44336',
}),
}),
text: new ol.style.Text({
text: String(feature.get('number')), // get feature property
}),
})
return [
baseStyle,
]
}
},
},
})