在 OpenLayers 中使用 SVG 图标作为标记
Use SVG icon as marker in OpenLayers
我尝试将 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图片被截断了,如下图所示。 (
图标应该是一个圆圈)
这是一个在图标符号器中显示内联 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
})
});
与您的一些不同之处:
- 我向
<svg>
添加了 width
和 height
属性。这让浏览器知道生成的图像有多大。
- 我在图标上添加了
scale
属性 以调整图像大小。
- 我使用
utf8
而不是 base64
编码(不重要)。
对我来说,解决方案是:
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>
我在打开图层样式中使用 svg 图标作为图像 src。
var custstyle = new ol.style.Style({
image: new ol.style.Icon({
anchor: [0.5, 50],
anchorXUnits: 'fraction',
anchorYUnits: 'pixels',
src: './Icon.svg',
}),
});
我尝试将 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图片被截断了,如下图所示。 ( 图标应该是一个圆圈)
这是一个在图标符号器中显示内联 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
})
});
与您的一些不同之处:
- 我向
<svg>
添加了width
和height
属性。这让浏览器知道生成的图像有多大。 - 我在图标上添加了
scale
属性 以调整图像大小。 - 我使用
utf8
而不是base64
编码(不重要)。
对我来说,解决方案是:
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>
我在打开图层样式中使用 svg 图标作为图像 src。
var custstyle = new ol.style.Style({
image: new ol.style.Icon({
anchor: [0.5, 50],
anchorXUnits: 'fraction',
anchorYUnits: 'pixels',
src: './Icon.svg',
}),
});