简体   繁体   English

OpenLayers-在标记的矢量要素周围绘制一个框

[英]OpenLayers - Draw a box around labeled vector feature

After endless searching, I've hit a wall. 经过无休止的搜索,我撞到了墙。 I have an OpenLayers map that uses many labels. 我有一个使用许多标签的OpenLayers地图。 My two options are to use the Popup Class or a labeled Vector Feature. 我的两个选择是使用Popup类或带有标签的Vector Feature。 I have tried using the Popup class, but it is extremely heavy on the DOM accounting for approximately 75% of the CPU load when updating the layer (verified with Chrome Developer CPU Profiling). 我曾尝试使用Popup类,但是在更新该层时,DOM非常沉重,约占CPU负载的75%(已通过Chrome Developer CPU Profiling验证)。 After switching from popups to vector features with a label, all the processor weight came off the DOM and now everything runs smoothly. 从带有标签的弹出窗口切换到矢量功能后,所有处理器的权重都减轻了DOM的负担,现在一切运行顺利。 The only problem though, is I can't get a solid colored box behind the label. 但是,唯一的问题是我在标签后面找不到纯色框。

The label renders inside a text and tspan svg element. 标签将在text和tspan svg元素内呈现。 From what I have read, there is no way to use the attributes of text or tspan to specify a backcolor property. 根据我的阅读,无法使用text或tspan的属性指定backcolor属性。 This is an endless search. 这是无止境的搜索。 I could add a rect around the text element using getBBox, but OpenLayers manages the tspan element and manipulating that alongside OpenLayers seems impossible. 我可以使用getBBox在text元素周围添加一个rect,但是OpenLayers管理tspan元素,并与OpenLayers一起操纵它似乎是不可能的。

Next, I was thinking that I could add a labeled vector feature that was a rectangle. 接下来,我想我可以添加一个标记的矢量特征,即矩形。 Here though, there is no way to draw the rectangle at the exact size of the label and behind it. 但是,这里没有办法以标签的确切大小及其后面绘制矩形。 Maybe this just isn't possible. 也许这是不可能的。 Hopefully one of you know a solution. 希望你们中的一个知道解决方案。 Thanks. 谢谢。

I figured it out. 我想到了。 Once I spent some time looking at OpenLayers on GitHub, I figured it out looking at the Popup class. 一旦我花了一些时间在GitHub上查看OpenLayers,我就知道了在查看Popup类。 First, autosize must be set to false and the size property must be set in addition to the contentsize. 首先,必须将autosize设置为false,并且除了contentsize之外还必须设置size属性。 Since the contentsize is part of the constructor, you must explicitly set the popup.size after initialization. 由于contentsize是构造函数的一部分,因此必须在初始化后显式设置popup.size。 This will prevent the setSize() method from executing, which will cut the load by about 50%. 这将阻止setSize()方法的执行,这会将负载减少约50%。 Then, I went and modified the class and removed the huge amount of event handlers. 然后,我去修改了该类并删除了大量的事件处理程序。 These two methods reduced the CPU load by about 50-75%. 这两种方法将CPU负载减少了约50-75%。

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

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