I have a series of dynamically generated SVG icons to be used as markers on a Leaflet map.
Icons take the r
number and insert it into the image; this works perfectly when the icon is directly referenced by the browser, however, when the icon is placed within the Leaflet system, the icon background image does not display; but I can't find why this is so:
SVG:
/***
* Create an SVG file using the Bronze image and apply a text as given by PHP GET clause.
***/
$rank = (int)$_GET['r'];
if($rank < 1 ) {
$rank = null;
}
header("content-type: image/svg+xml");
?>
<?xml version='1.0' encoding='utf-8'?>
<svg version='1.1' baseProfile='full' xmlns='http://www.w3.org/2000/svg' xmlns:svg='http://www.w3.org/2000/svg'
xmlns:xlink='http://www.w3.org/1999/xlink' viewBox='0 0 20 25' xml:space='preserve' height='25' width='20'>
<image xlink:href='https://domain.co.uk/images/map_icons/bronze5_20.png' x='0' y='0' width='20' height='25' />
<text id='svgtext' x='50%' y='40%' dominant-baseline='middle' text-anchor='middle' fill='black' font-size='11px'><?php print $rank;?></text>
</svg>
Leaflet (v1.6 current ):
var Brnz4 = L.icon({
iconUrl: '/images/map_icons/bronze.php?r=4',
iconSize: [20, 25], // size of the icon
iconAnchor: [0, 25], // point of the icon which will correspond to marker's location
popupAnchor: [0, -20] // point from which the popup should open relative to the iconAnchor
});
And
L.marker([56.224800000000000,-2.703890000000000], {icon:Brnz4} ).addTo(map)
But what actually displays is simply the text number (4) without the background image.
The background image loads ok when directly called.
The SVG (PHP) loads correctly when directly called by the browser.
The text displays in the correct position which implies the SVG is being read.
What have I tried?
- Added various headers to the
<svg>
/<xml>
tags as displayed above (such as viewbox, etc.) . - absolute URLs
- Read this post
- used single
'
and double"
quotes in the SVG incase XML was quote-type specific.
Images:
How the marker looks on the Leaflet Map.
How the leaflet appears in the browser Inspector.
How the marker appears with the direct URL in the browser. This should be how it appears on the map.
It appears that somehow via Leaflet the SVG background image is not being loaded. Why?
https://domain.co.uk/images/map_icons/bronze5_20.png
? I'm not sure how raster images embedded in SVG images should work in this particular case. – IvanSanchez