简体   繁体   English

Leaflet.js地图中的SVG图标

[英]SVG icons in Leaflet.js map

I have a working Leaflet map but cannot pass in SVG icons using encodeURI (have not tried encodeURIComponent because I'm not sure that is the issue). 我有一个工作正常的Leaflet贴图,但是无法使用encodeURI传递SVG图标(由于我不确定这是问题,因此未尝试使用encodeURIComponent)。 The gist I'm using shows how to pass in SVG rectangles and this works: 我正在使用的要点显示了如何通过SVG矩形,这可行:

<svg xmlns='http://www.w3.org/2000/svg'> <rect> x='0' y='0' width='20' height='10' fill='#000000' </rect> </svg> 

However, I cannot pass in a circle or a path successfully, even though the code is valid, optimized in SVGOMG, and appearing properly on SVG linters such as SVG Viewer. 但是,即使代码有效,已在SVGOMG中优化并在SVG短绒毛绒(例如SVG Viewer)上正确显示,我也无法成功通过圆圈或路径。 For example, a star. 例如,一颗星星。

<svg xmlns='http://www.w3.org/2000/svg' width='50px' height='50px'><path d='M2,111 h300 l-242.7,176.3 92.7,-285.3 92.7,285.3 z' fill='#000000'/></svg>

An example is on CodePen and the relevant lines of code are: 在CodePen上有一个示例 ,相关的代码行是:

var svgicon ="<svg xmlns='http://www.w3.org/2000/svg' width='50px' height='50px'><path d='M2,111 h300 l-242.7,176.3 92.7,-285.3 92.7,285.3 z' fill='#000000'/></svg>"

var url = encodeURI("data:image/svg+xml," + svgicon).replace(/%20/g, ' ').replace(/%22/g, "'").replace(/%3C/g,'<').replace(/%3E/g,'>');
console.log(url);

You can see the SVG path in the console. 您可以在控制台中看到SVG路径。

"data:image/svg+xml,<svg xmlns='http://www.w3.org/2000/svg' width='50px' height='50px'><path d='M2,111 h300 l-242.7,176.3 92.7,-285.3 92.7,285.3 z' fill='#000000'/></svg>"

Nothing shows up and there is no error message. 没有任何显示,也没有错误消息。 Sometimes, a broken image link shows up. 有时会出现损坏的图像链接。

Can you display that svg in a browser? 您可以在浏览器中显示该svg吗? I don't think the SVG path is well formed. 我认为SVG路径格式不正确。

You define a 50(px) x 50(px) svg canvas. 您定义一个50(px)x 50(px)的svg画布。

<path d="
M2,111
h300
l-242.7,176.3
   92.7,-285.3
   92.7,285.3
Z
" fill="#000"/>

You start the path with an absolute ( M )ove declaration at 2,111 which is outside of the canvas. 您可以在画布之外的2,111处以绝对( M )ove声明开始路径。 Followed by a relative ( h )orizontal line 300 to the right. 随后是右侧的( h )相对原始线300。 Then relative ( l )ines -242.7,176.3 92.7,-285.3 92.7,285.3 before you clo( Z )e the path. 然后,相对的(L)INES -242.7,176.3 92.7,-285.3 92.7,285.3你CLO(Z)之前Ë路径。

If I set the canvas to 1000 * 1000 I see this icon in the browser. 如果将画布设置为1000 * 1000,我将在浏览器中看到此图标。 Is this what you would like to see? 这是您想看到的吗?

在此处输入图片说明

I draw this in LeafletJS like so: 我在LeafletJS中这样绘制:

let achenSvgString = "<svg xmlns='http://www.w3.org/2000/svg' width='1000' height='1000'><path d='M2,111 h300 l-242.7,176.3 92.7,-285.3 92.7,285.3 z' fill='#000000'/></svg>"
let myIconUrl = encodeURI("data:image/svg+xml," + achenSvgString).replace('#','%23');

// L.icon({
//     iconUrl: myIconUrl,
//     iconSize: 40
// });

I didn't adjust the size as I just hammered this into a working bit of code but you can see the stars drawing here... 我没有调整大小,因为我将其敲定为一段工作代码,但是您可以看到此处绘制的星星...

在此处输入图片说明

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM