Svg icon from source
by Vladimir Vershinin
HTML
<script src="https://cdnjs.cloudflare.com/ajax/libs/svg.js/2.6.5/svg.js"></script>
<script src="https://cdn.rawgit.com/openlayers/openlayers.github.io/master/en/v5.3.0/build/ol.js"></script>
<script src="https://unpkg.com/[email protected]/dist/vue.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 data-projection="EPSG:4326">
<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-style-func :factory="styleFuncFactory"></vl-style-func>
</vl-layer-vector>
</vl-map>
<svg id="skull" version="1.1" id="Layer_1" xmlns="http://www.w3.org/2000/svg" x="0px" y="0px"
width="100px" height="100px" viewBox="0 0 100 100" enable-background="new 0 0 100 100" xml:space="preserve">
<g>
<path...
CSS
html, body, #app {
width: 100%;
height: 100%;
margin: 0;
padding: 0;
}
JavaScript
Vue.use(VueLayers)
// external loaded features
let features = [
{
"type": "Feature",
"id": 1,
"properties": {
"color": "red",
},
"geometry": {
"type": "Point",
"coordinates": [
-30.761718749999996,
58.90464570302001
]
}
},
{
"type": "Feature",
"id": 2,
"properties": {
"color": "green",
},
"geometry": {
"type": "Point",
"coordinates": [
36.73828124999999,
44.59046718130883
]
}
},
{
"type": "Feature",
"id": 3,
"properties": {
"color": "purple",
},
"geometry": {
"type": "Point",
"coordinates": [
-23.37890625,
32.10118973232094
]
}
}
]
new Vue({
el: '#app',
methods: {
styleFuncFactory () {
console.log("create style func")
// get raw svg using http://svgjs.com/
let svg = SVG('skull')
return feature => {
let featureSvg = svg.clone()
featureSvg.attr('width', 100 + 'px')
featureSvg.attr('height', 100 + 'px')
// set color from feature data
let path = featureSvg.select('path')
path.fill(feature.get('color'))
let img = new Image()
img.src = 'data:image/svg+xml;utf8,' + encodeURIComponent(featureSvg.svg())
featureSvg.remove()
//console.log(src)
// apply color by some rule: here we use proprty from feature
let icon = new ol.style.Icon({
img: img,
imgSize: [100, 100],
size: [100, 100],
})
return [
new ol.style.Style({
image: icon,
})
]
}
},
},
data () {
return {
zoom: 3,
center: [-10, 35],
features: features,
}
},
})