如何使用 OpenLayers-3 将 SVG 图像用作图层(而不是 map 标记)
在使用任何 ol.source.Vector
和 ol.format.Feature
实例时,我无法获得我的 SVG 图像的任何输出。
小例子:
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
}),
});
我在使用 ImageStatic 层时能够获得输出,但这使用/生成(?)静态图像,因此 SVG 的优势消失了。
例子:
// 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
})
});
我已经尝试过将 Content-type:
设置为 image/svg+xml
的技巧,但这对我一点帮助都没有。
所以,再次: 我如何(如果可能)在 OpenLayers-3 的图层中使用 SVG 图像?
最佳答案
您不能对 svg 文件使用 ol.source.Vector
,但 OL3 可以将 svg 文件显示为图像。
图像在缩放时保持清晰。
我修改了官方static image example , 并将 png 文件替换为 svg 文件。请参阅下面的可运行示例。
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>
关于javascript - 如何在 OpenLayers-3 上使用 SVG 图像作为图层,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34416412/