创建地图图例以匹配 OpenLayers.Style
我创建了一个 OpenLayers.Style 为我的多边形着色,一个为我的点调整大小的样式以及所有爵士乐,现在我想向用户解释这些样式代表什么。
我在 OpenLayers 中看不到任何允许我使用这些样式绘制自己的图例的内容。一切似乎都指向我假设的地图服务器正在向我发送数据,但我没有。
目前看来我必须绘制一些样本点/区域并抓取它们来制作我自己的图例。有没有更好的方法可以直接基于样式来完成此操作,这样当样式更改时我就不必重新生成图像?
更新 我有一个很好的答案,依赖于 GeoExt (以及 ExtJS),我仍然想听听是否有人有 jQuery 兼容的答案。特别是如果它是纯 Javascript 和 OpenLayers。
I've created an OpenLayers.Style that colours my polygons, a style that sizes my points and all that jazz, now I want to explain to the user what those styles represent.
I can't see anything in OpenLayers that allows me to draw my own legend using those styles. Everything seems to point me towards the assumed map server that is sending me data, which I don't have.
At the moment it looks like I'll have to draw some sample points/areas and screen grab them to make my own legend. Is there a better way to do it based straight off the Style so I don't have to regenerate the images when the Style gets changed?
Update
I've had a good answer that relies on GeoExt (and thus ExtJS), I'd still like to hear if anyone has a jQuery compatible answer. Especially if it is plain Javascript and OpenLayers.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
我能够使用 ol.Map 类作为符号容器来解决我的图例需求。也许有点破解,但似乎适用于大多数(?)矢量图层(我没有 WMS)。
所以我正在做的是:
循环遍历地图图层并选择矢量类型
if(lyr instanceof ol.layer.Vector)
检查使用什么类型的样式并存储在数组中
还存储几何类型
循环存储的图例行并构造所需的 HTML 元素,通常是“迷你地图”的 div 和图层名称
一旦 HTML 元素已添加到页面,启动地图并插入假特征以显示符号
通过此,我能够创建和更新单独的图例对话框 (jQuery UI):
我还没有进行很多测试,这种方法可能存在一些问题......
I was able to solve my legend needs using the ol.Map-class as a container for the symbols. Maybe a bit of a hack, but seems to work for most (?) vector layers (I have no WMS's).
So what I'm doing is:
loop through the map layers and pick out the vector type with
if(lyr instanceof ol.layer.Vector)
check what type of style is used and store in an array
also store the geometry type
loop through the stored to-be legend rows and construct the HTML elements needed, typically a div for the "mini-map" and the layer name
once HTML elements have been added to the page, initiate the maps and insert the fake features to display the symbols
With this, I was able create and update a separate Legend dialog (jQuery UI):
I haven't tested a lot yet, there may be some problems with this approach...
实际上,OpenLayers 不支持你想要的(或者至少我不知道该怎么做)。正如 Chau 告诉您的,GeoExt 的 LegendPanel 是您唯一的希望。
有趣的链接:
http://geoext.org/lib/GeoExt/widgets/LegendPanel.html< /a>
http://www.mail-archive. com/[电子邮件受保护]/msg01318.html
Actually, OpenLayers doesn't support what you want (or at least I don't know how to do it). As Chau told you, LegendPanel from GeoExt is your only hope.
Interesting links:
http://geoext.org/lib/GeoExt/widgets/LegendPanel.html
http://www.mail-archive.com/[email protected]/msg01318.html
作为一种选择,您可以创建具有与 OL3 样式相同属性的 SVG 元素。下面是圆形样式的示例(您还需要类似的方法以及其他类型):
由于使用 jQuery 进行的 SVG 操作与 HTML 元素有些不同,因此我将对象转换为字符串作为回报。详细信息可以从 jquery's append not work with svg element?
稍后 找到,您可以将图例图标粘贴到 HTML 中
As one option, you could create SVG elements with same attributes like in OL3 style. Here's example for circle style (you would need similar methods also other types):
Since SVG manipulation with jQuery is bit different from HTML elements, I'm converting object to string in return. Details can be found from jquery's append not working with svg element?
Later, you can stick legend icon to HTML with
使用普通 OpenLayers 最接近您想要的效果是使用 WMS 服务而不是 WFS 或任何用于获取功能的方法。 WMS 具有请求类型 GetLegendGraphic,顾名思义,它允许您动态获取显示应用于图层的样式的图像。
The closest you can get to what you want with plain OpenLayers is if you use WMS service instead of WFS or whatever method you use to fetch features. WMS has request type GetLegendGraphic that, as name suggests, lets you dynamically fetch image that shows what style is applied to layer.
可以制作半手图例:
对于WMS功能
使用 Geoserver,GetLegendGraphic 可以生成每个 WMS 图层的图像图例
对于 WFS 功能
对于每个图层,可以根据其样式 属性:
style.getFill().getColor()
)style.getStroke().getColor()
)style.getImage().getImage()
)A half-handed legend can be made:
For WMS features
With a Geoserver, GetLegendGraphic can generate an image legend for each WMS layer
For WFS features
For each layer, a legend can be built based on its style attributes:
style.getFill().getColor()
)style.getStroke().getColor()
)style.getImage().getImage()
)