如何通过OpenLayers-3将SVG图像用作图层(因此不能用作地图标记
在使用任何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层时获得输出,但这里使用/generates(?)一个静态的图像,所以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图像a层?
发布于 2016-03-24 00:33:17
不能将ol.source.Vector与svg文件一起使用,但OL3可以将svg文件显示为图像。
图像在缩放时仍保持清晰。
我修改了官方的static image example,并将png文件替换为svg文件。请参阅下面的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="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>
发布于 2018-01-13 05:26:49
目前,2018是开放式第4层的示例
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>
请参阅原文:
发布于 2019-05-30 00:23:56
@Alvin Lindstam的答案几乎就在那里,但在Chrome (或IE)上没有显示完整的图像。因为SVG没有固定的大小,所以需要在imageLoadFunction中设置宽度和高度
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,
imageLoadFunction: function (image, src) {
image.getImage().src = src;
image.getImage().width = ol.extent.getWidth(extent);
image.getImage().height = ol.extent.getHeight(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>
https://stackoverflow.com/questions/34416412
复制相似问题