如何使用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图层时能够获得输出,但这会使用/生成(?)静态图像,因此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
但这并没有帮助我.
那么,再说一遍:我怎样(如果可能的话)使用SVG图像和OpenLayers-3一层?
您不能使用ol.source.Vector
with svg文件,但OL3可以将svg文件显示为图像.
缩放时图像保持清晰.
我修改了官方静态图像示例,并用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
})
});
如今,2018年,开放层4的示例:
var svgComment = '';
//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
})
});