【问题标题】:How can I use a SVG image as layer on OpenLayers-3如何在 OpenLayers-3 上使用 SVG 图像作为图层
【发布时间】:2015-12-22 12:55:33
【问题描述】:

如何通过 OpenLayers-3 将 SVG 图像用作图层(而不是地图标记)

在使用任何 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 的技巧,但这对我没有任何帮助。

所以,再说一遍: 我如何(如果可能)使用 SVG 图像作为 OpenLayers-3 的图层?

【问题讨论】:

  • 我很好奇,除了作为静态图像之外,您还想如何利用 SVG 的优势?
  • 好吧,根据我的想法(如果我错了,请纠正我)我应该能够添加一个“可缩放”的 SVG 图层,这样即使你放大图像也不会像素化并且保持锋利。我们倾向于将它用于平面图,因此 SVG(或者可能是其他矢量格式)会很棒。当然,静态图像仍然是可能的,但是当您放大时,它总是会变得模糊。
  • 我自己仍在了解 OL3,并且一直对 SVG 感到疑惑。我还需要在基础层上绘制类似 CAD 的矢量绘图,并且从我读过的 cmets 中可能还不支持 SVG?但是您绝对可以使用 Point、LineString 和 Polygon 类型来制作可缩放图层。我要试试ogr2ogr to translate DXF to GeoJSON.
  • 是的,这是我的问题。我不知道它是否受支持(我在网站上没有看到任何关于它的信息,所以我的猜测是否定的)。只是 SVG 是一种如此常用的格式,我以为我只是错过了一些东西。也许我需要在 GitHub 页面上问这个问题,因为这里似乎没有回答。谢谢你的链接,我也会调查一下。
  • 哦,顺便说一句,我注意到 SVG 图标是可能的,所以也许我们可以使用一个巨大的图标 :) stackoverflow.com/questions/29152405/…

标签: javascript openlayers-3


【解决方案1】:

ol.source.Vector 不能用于 svg 文件,但 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>

【讨论】:

  • 感谢您的回答。你知道如何应用 SVG 本身的样式(style 属性),还可以说onmousehover 事件?例如&lt;svg style="..." onmouseover="..."&gt;&lt;/svg&gt;
【解决方案2】:

现在,2018Open Layers 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>

查看原帖:

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

【讨论】:

    【解决方案3】:

    @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>

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多