Sunday 27 November 2016

openlayers - Local OpenLayers3 - SVG icon


I have an OpenLayers3 map on my local filesystem, and I want a point layer to use SVG icons. This seems to fail silently:


var style = new ol.style.Style({

image: new ol.style.Icon({
size: [95, 95],
src: "styles/transport_aerodrome.svg"
})
});

Is this because the SVG, since it is a local file, does not have content-type, but OL3 needs SVGs to have content-type: image/svg+xml? Or is there another reason this fails, and a way to fix it?



Answer



I am not sure whether this is a file location problem or if it is a problem of the svg you use. As long as it is within the root folder of your app I can not see why is not working. Dont you have any firebug errors? try to use the following snip to check if it is working. I have tried it works fine.


var style = new ol.style.Style({

image: new ol.style.Icon({
src: 'http://www.williambuck.com/portals/0/Skins/WilliamBuck2014/images/location-icon.svg'
})
});

Also consider removing the size you provide within your code snip. So call it like that


var style = new ol.style.Style({
image: new ol.style.Icon({
src: "styles/transport_aerodrome.svg"
})

});

UPDATE if you can get the xml out of your svg file. try the following:


var svg = ''+    
''+
'';

var mysvg = new Image();
mysvg.src = 'data:image/svg+xml,' + escape(svg);


//and then declare your style with img and imgSize
var style = new ol.style.Style({
image: new ol.style.Icon({
img: mysvg,
imgSize:[30,30]
})
});

No comments:

Post a Comment

arcpy - Changing output name when exporting data driven pages to JPG?

Is there a way to save the output JPG, changing the output file name to the page name, instead of page number? I mean changing the script fo...