I'm not even sure if this is possible but I am trying to change the color of a marker on an open layers 7 map. The marker is being added from an svg file using the following lines of code:
const marker = new ol.Feature({
geometry: new ol.geom.Point(ol.proj.fromLonLat(london)),
});
const markerStyle = new ol.style.Style({
image: new ol.style.Icon({
anchor: [0.5,1],
scale: 0.06,
src: 'https://testing.52weekdog.com/images/markers/marker_marker01_black.svg'
})
});
const vectorSource = new ol.layer.Vector({
source: new ol.source.Vector({
features: [marker]
}),
style: markerStyle
});
marker.setStyle([markerStyle]);
Is there a way to change the fill color of the external svg image? It is currently all black. I've tried adding both 'color' and 'fill' to the image element of the markerStyle without any luck. I think the demo (https://openlayers.org/en/latest/examples/icon-color.html) may be relevant but I'm not exactly sure how it applies.
Any help on how to solve this problem or maybe a different root to take if necessary would be greatly appreciated.
Here is the page I'm trying to get working JSFiddle.
var london = [-0.1276474, 51.507321899999994];
const baseMapTile = new ol.layer.Tile({
source: new ol.source.OSM(),
visible: true,
title: 'OSMStandard'
});
const marker = new ol.Feature({
geometry: new ol.geom.Point(ol.proj.fromLonLat(london)),
});
const markerStyle = new ol.style.Style({
image: new ol.style.Icon({
anchor: [0.5,1],
scale: 0.06,
src: 'https://testing.52weekdog.com/images/markers/marker_marker01_black.svg'
})
});
const vectorSource = new ol.layer.Vector({
source: new ol.source.Vector({
features: [marker]
}),
style: markerStyle
});
marker.setStyle([markerStyle]);
const map = new ol.Map({
view: new ol.View({
center: (ol.proj.fromLonLat(london)),
zoom: 15,
}),
layers: [baseMapTile, vectorSource],
target: 'map'
});
document.getElementById('markerColor').addEventListener("change", function() {
//change the color of the icon
newHex = document.getElementById('markerColor').value;
alert('Change marker icon color to ' + newHex);
});
#map{
width: 300px;
height: 300px;
border: 1px solid black;
padding: 5px;
float: left;
margin-right: 15px;
}
<link href="https://cdn.jsdelivr.net/npm/ol@v7.2.2/ol.css" rel="stylesheet"/>
<script src="https://cdn.jsdelivr.net/npm/ol@v7.2.2/dist/ol.js"></script>
<div id="map" class="map"></div>
<div id="info" class="info">
<h2>Change Marker Image</h2>
<select name="markerColor" id="markerColor">
<option value="#000000">Black</option>
<option value="#eb3434">Red</option>
<option value="#345ceb">Blue</option>
</select>
</div>