如何在OpenLayers-3上使用SVG圖像作為層?

[英]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

如何使用OpenLayers-3將SVG圖像作為一個層(所以不是一個地圖標記)?

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

在使用任何ol.source時,我無法獲得SVG圖像的任何輸出。向量和ol.format。功能的實例。

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.

在使用ImageStatic層時,我能夠獲得輸出,但是這個使用/生成(?)靜態圖像,因此SVG的優勢消失了。

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.

我已經嘗試過設置Content-type: to image/svg+xml,但這對我毫無幫助。

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

那么,我又如何(如果可能的話)使用SVG圖像作為一個具有openlayer -3的層呢?

2 个解决方案

#1


11  

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

您不能使用ol.source。向量與svg文件,但OL3可以顯示svg文件作為圖像。

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.

我修改了官方的靜態圖像示例,並用svg文件替換了png文件。參見下面的runnable示例。

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="http://openlayers.org/en/v3.14.2/build/ol.js"></script>
<link href="http://openlayers.org/en/v3.14.2/css/ol.css" rel="stylesheet"/>
<div id="map" class="map"></div>

#2


0  

Nowadays, 2018, a example for Open Layers 4:

現在,2018年,開放層的一個例子:

var svgComment = '<svg width="160" height="160" version="1.1" xmlns="http://www.w3.org/2000/svg" viewBox="0 0 160 160" viewPort="0 0 160 160" class="svgClass">'
    + '<circle cx="30" cy="30" r="10" stroke="rgb(0, 191, 255)" stroke-width="1" fill="none" opacity="0.8">'
    + '<animate attributeType="CSS" attributeName="stroke-width" from="1" to="30" dur="0.5s" begin="0s" repeatCount="indefinite" />'
    + '<animate attributeType="CSS" attributeName="opacity" from="0.8" to="0.2" dur="0.5s" begin="0s" repeatCount="indefinite" />'
    + '</circle>'
    + '<circle cx="30" cy="30" r="10" fill="rgba(0,0,0,0.8)">'
    + '</circle>'
    + '</svg>';


//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
  })
});
<script src="https://openlayers.org/en/v4.6.4/build/ol.js"></script>
<div id="map" class="map"></div>


see orIginal post:

看到早前發布的文章:

https://stackoverflow.com/a/48232790/2797243

https://stackoverflow.com/a/48232790/2797243


注意!

本站翻译的文章,版权归属于本站,未经许可禁止转摘,转摘请注明本文地址:https://www.itdaan.com/blog/2015/12/22/7253f399e67901afdad302e4de66d57f.html



 
粤ICP备14056181号  © 2014-2021 ITdaan.com