javascript - 如何在 OpenLayers-3 上使用 SVG 图像作为图层

标签 javascript openlayers-3

如何使用 OpenLayers-3 将 SVG 图像用作图层(而不是 map 标记)

在使用任何 ol.source.Vectorol.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/

相关文章:

openlayers-3 - 如何缩放到 Openlayers3 (v3.17.1) map 的任何特征

javascript - 开放层 3 : Basic Authentication

javascript - Jquery UI无法从左侧拖动到右侧

javascript - 处理和返回多个 promise

javascript - 分配 ID/名称属性以唯一标识绘制的多边形

javascript - OpenLayers 3.13 : issue with bindTo by linking two views

openlayers-3 - map.on ('click' ) 在 Firefox 中坐标错误

javascript - 如何在 Matter.js 中实现鼠标悬停行为?

javascript - jQuery 选项卡在移动设备中不起作用 - 自定义代码

javascript - $project 以外的方法在 $lookup 和 $unwind 之后限制 MongoDB View 中的数据