Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I use a SVG image as layer on OpenLayers-3

How can I use a SVG image as a Layer (so not as a map marker) with OpenLayers-3

I was unable to get any output of my SVG image when using any of the ol.source.Vector and ol.format.Feature instances.

Small example:

var mapLayer = new ol.layer.Vector({
    source: new ol.source.Vector({
        url: 'image.svg',
        format: new ol.format.Feature() // http://openlayers.org/en/v3.12.1/apidoc/ol.format.Feature.html
    }),
}); 

I was able to get output when using the ImageStatic layer, but this uses/generates(?) a static image so the advantages of SVG are gone.

Example:

// Not sure if I need this for SVG, but is is required for an image
var extent = [0, 0, 1000, 1000]; // random image size
var projection = new ol.proj.Projection({
    code: 'test',
    units: 'pixels',
    extent: extent
});

var mapLayer = new ol.layer.Image({
    source: new ol.source.ImageStatic({
        url: 'image.svg',
        projection: projection,
        imageExtent: extent
    })
});

I already tried the trick with setting the Content-type: to image/svg+xml but this did not help me at all.

So, again: How can I (if possible) use a SVG image a a layer with OpenLayers-3?

like image 683
Matthijs Avatar asked Dec 22 '15 12:12

Matthijs


People also ask

Can SVG have layers?

There are no “layers” in SVG, and no real concept of depth. SVG does not support CSS's z-index property, so shapes can only be arranged within the two-dimensional x/y plane. The order in which elements are coded determines their depth order. The purple square appears first in the code, so it is rendered first.

How do I add images to SVG?

SVG images can be written directly into the HTML document using the <svg> </svg> tag. To do this, open the SVG image in VS code or your preferred IDE, copy the code, and paste it inside the <body> element in your HTML document. If you did everything correctly, your webpage should look exactly like the demo below.


1 Answers

You can not use the ol.source.Vector with svg files, but OL3 can display svg files as images.

The image stays sharp when zoomed.

I modified the official static image example, and replaced the png file with a svg file. See the runnable example below.

var extent = [0, 0, 512, 512];
var projection = new ol.proj.Projection({
  code: 'static-image',
  units: 'pixels',
  extent: extent
});

var map = new ol.Map({
  layers: [
    new ol.layer.Image({
      source: new ol.source.ImageStatic({
        url: 'https://upload.wikimedia.org/wikipedia/commons/f/fd/Ghostscript_Tiger.svg',
        projection: projection,
        imageExtent: extent
      })
    })
  ],
  target: 'map',
  view: new ol.View({
    projection: projection,
    center: ol.extent.getCenter(extent),
    zoom: 0
  })
});
<script src="https://cdn.rawgit.com/openlayers/openlayers.github.io/master/en/v5.3.0/build/ol.js"></script>
<link href="https://cdn.rawgit.com/openlayers/openlayers.github.io/master/en/v5.3.0/css/ol.css" rel="stylesheet"/>
<div id="map" class="map"></div>
like image 151
Alvin Lindstam Avatar answered Oct 09 '22 22:10

Alvin Lindstam