在OpenLayers中使用SVG图标作为标记

时间:2016-06-15 08:25:16

标签: svg styles openlayers marker

我尝试将svg图标作为Openlayers-3中的标记。在我的代码中。

var svg = '<?xml version="1.0"?>'
            + '<svg viewBox="0 0 120 120" version="1.1" xmlns="http://www.w3.org/2000/svg">'
            + '<circle cx="60" cy="60" r="60"/>'
            + '</svg>';

var style = new ol.style.Style({
            image: new ol.style.Icon({
                opacity: 1,
                src: 'data:image/svg+xml;base64,' + btoa(svg)
            })
        });

但我的svg图像被截断,如下图所示。 ( 图标应为圆圈)

enter image description here

2 个答案:

答案 0 :(得分:7)

以下是一个在图标符号器中显示内联SVG的示例:http://jsfiddle.net/eze84su3/

以下是相关代码:

var svg = '<svg width="120" height="120" version="1.1" xmlns="http://www.w3.org/2000/svg">'
    + '<circle cx="60" cy="60" r="60"/>'
    + '</svg>';

var style = new ol.style.Style({
  image: new ol.style.Icon({
    opacity: 1,
    src: 'data:image/svg+xml;utf8,' + svg,
    scale: 0.3
  })
});

与您的一些区别:

  • 我向width添加了height<svg>个属性。这样浏览器就可以知道生成图像的大小。
  • 我在图标中添加了scale属性以调整图片大小。
  • 我使用utf8代替base64编码(不重要)。

答案 1 :(得分:0)

对我来说,解决方案是:

const iconMarkerStyle = new ol.style.Icon({
    src: './data/static_images/marker.svg',
    //size: [100, 100],
    offset: [0, 0],
    opacity: 1,
    scale: 0.35,
    //color: [10, 98, 240, 1]
})

然后直接在SVG文件中添加尺寸参数:

<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 144.81 215.81" width="14.5px" height="21.6px">
    <title>Asset 190-SVG</title>
</svg>
相关问题