I tried to adopt this approach based on ol.source.ImageStatic
for OpenLayers 3 https://stackoverflow.com/a/36183738/2797243
to OpenLayers 4, with no result:
https:
The functionality seems to depend on the SVG data.. A working example:
var svgComment = '';
//Test SVG
//var img = document.createElement('img');
//img.src=src;
//document.body.append(img);
var commentStyle = new ol.style.Style({
image: new ol.style.Icon({
src: 'data:image/svg+xml;charset=utf-8,' + encodeURIComponent(svgComment)
})
});
var vectorSource = new ol.source.Vector({
features: [
new ol.Feature({
geometry: new ol.geom.Point([0, 0])
})
]
});
var vectorLayer = new ol.layer.Vector({
name: 'Comments',
style: commentStyle,
source: vectorSource
});
//display the map
var rasterLayer = new ol.layer.Tile({
source: new ol.source.TileJSON({
url: 'https://api.tiles.mapbox.com/v3/mapbox.geography-class.json?secure',
crossOrigin: ''
})
});
var map = new ol.Map({
layers: [rasterLayer, vectorLayer],
target: document.getElementById('map'),
view: new ol.View({
center: [0, 0],
zoom: 3
})
});