创建地图图例以匹配OpenLayers.Style

时间:2011-03-11 00:16:37

标签: openlayers legend

我创建了一个OpenLayers.Style,为我的多边形着色,这个样式可以调整我的点和所有爵士乐,现在我想向用户解释这些样式代表的内容。

我在OpenLayers中看不到任何允许我使用这些样式绘制自己的图例的内容。一切似乎都指向我发送数据的假定地图服务器,我没有。

目前看起来我将不得不绘制一些样本点/区域,屏幕抓住它们来制作我自己的传奇。是否有一种更好的方法可以直接在Style上进行,所以当Style更改时我不需要重新生成图像?

更新 我有一个很好的答案,依赖于GeoExt(以及ExtJS),我仍然想听听是否有人有一个jQuery兼容的答案。特别是如果它是普通的Javascript和OpenLayers。

5 个答案:

答案 0 :(得分:3)

实际上,OpenLayers不支持你想要的东西(或者至少我不知道怎么做)。正如Chau告诉你的那样,来自GeoExt的LegendPanel是你唯一的希望。

有趣的链接:

http://geoext.org/lib/GeoExt/widgets/LegendPanel.html

http://www.mail-archive.com/openlayers-users@lists.osgeo.org/msg01318.html

答案 1 :(得分:2)

我能够使用ol.Map-class作为符号的容器来解决我的图例需求。也许有点像黑客,但似乎适用于大多数(?)矢量图层(我没有WMS')。

所以我正在做的是:

  1. 循环遍历地图图层并使用

    选择矢量类型

    if(lyr instanceof ol.layer.Vector)

  2. 检查使用的样式类型并存储在数组中

    var style = lyr.getStyle();
    var image = style.getImage();
    if(image){
        if(image instanceof ol.style.Icon){
            //raster icon from url
            var icon2 = new ol.style.Icon( ({
                src: image.getSrc()
            }))
            var iconStyle2 = new ol.style.Style({
                image: icon2
            });
            row = {};
            row.style = iconStyle2;
            row.title = lyr.get('title');               
        }
        else{ //ol.style.RegularShape?
            row = {};
            row.style = style;
            row.title = lyr.get('title');               
        }
    }else{
        row = {};
        row.style = style;
        row.title = lyr.get('title');
    }
    
  3. 还存储几何类型

    //geometry type
    var feats = lyr.getSource().getFeatures();
    if (feats && feats.length>0){
        if(feats[0].getGeometry() instanceof ol.geom.Point || feats[0].getGeometry() instanceof ol.geom.MultiPoint){
            row.geomType="point";
        }else if (feats[0].getGeometry() instanceof ol.geom.LineString || feats[0].getGeometry() instanceof ol.geom.MultiLineString){
            row.geomType="line";
        }else{
            row.geomType="polygon";
        }
    }
    
  4. 遍历存储的辅助图例行并构建所需的HTML元素,通常是"迷你地图"和图层名称

    for (i = 0; i < legendRows.length; i++) { 
        row = document.createElement("tr");
        //symbol
        cell = document.createElement("td");
        cell.style="width:35px";
        var div = document.createElement("div");
        div.style="width:32px; height:32px;";
        div.id = "mapLegendRowSymbolDiv" + i; 
        tble.appendChild(row);
        row.appendChild(cell);
        cell.appendChild(div);
        //layer title
        cell = document.createElement("td");      
        tble.appendChild(row);
        row.appendChild(cell);
        cell.innerHTML=legendRows[i].title; 
    }
    
    //append table
    $( "#legendText" ).empty();
    $( "#legendText" ).append(tble);
    
  5. 将HTML元素添加到页面后,启动地图并插入虚假功能以显示符号

    //loop legend rows and and insert the maps
    var extent = [0, 0, 32, 32];
    var projection = new ol.proj.Projection({
        code: 'xkcd-image',
        units: 'pixels',
        extent: extent
    });
    for (i = 0; i < legendRows.length; i++) { 
        //target div
        var targetDiv = document.getElementById("mapLegendRowSymbolDiv" + i);
        //layer for icon
        var sourceLegend = new ol.source.Vector({wrapX: false});
        var vectorLegend = new ol.layer.Vector({
            source: sourceLegend,
            style: legendRows[i].style
        });
        //map
        var mapLegend = new ol.Map({
            controls: [],
            layers: [
                new ol.layer.Image({
                    source: new ol.source.ImageStatic({
                        projection: projection,
                        imageExtent: extent
                    })
                }),
                vectorLegend
            ],
            target: targetDiv,
            view: new ol.View({
                projection: projection,
                center: ol.extent.getCenter(extent),
                zoom: 2,
                maxZoom: 2
            })
        });
        //icon feature depending on type
        var geom;
        if(legendRows[i].geomType=='point'){
            geom = new ol.geom.Point([16,16]);
        }else if(legendRows[i].geomType=='polygon'){
            var polyCoords = [];
            polyCoords.push([15.7, 15.7]);
            polyCoords.push([16.3, 15.7]);
            polyCoords.push([16.3, 16.3]);
            polyCoords.push([15.7, 16.3]);
            polyCoords.push([15.7, 15.7]);
            geom = new ol.geom.Polygon([polyCoords]);
        }else{
            var lineCoords = [];                
            lineCoords.push([15.6, 15.6]);
            lineCoords.push([16, 16]);
            lineCoords.push([16, 15.8]);
            lineCoords.push([16.4, 16.2]);
            geom = new ol.geom.LineString(lineCoords);
        }
        var feature = new ol.Feature({
            geometry: geom
        });
        vectorLegend.getSource().addFeature(feature);
    }   
    
  6. 有了这个,我就能够创建并更新一个单独的Legend对话框(jQuery UI):

    enter image description here

    我还没有经过多次测试,这种方法可能存在一些问题......

答案 2 :(得分:1)

作为一个选项,您可以创建具有相同属性的SVG元素,如OL3样式。这是圆形样式的示例(您需要类似的方法以及其他类型):

getIconLegend = function(style) {
    style = style.getImage();
    var radius = style.getRadius();
    var strokeWidth = style.getStroke().getWidth();
    var dx = radius + strokeWidth;

    var svgElem = $('<svg />')
        .attr({
            width: dx * 2,
            height: dx * 2
        });

    $('<circle />')
        .attr({
            cx: dx,
            cy: dx,
            r: radius,
            stroke: style.getStroke().getColor(),
            'stroke-width': strokeWidth,
            fill: style.getFill().getColor()
        })
        .appendTo(svgElem);

    // Convert DOM object to string to overcome from some SVG manipulation related oddities
    return $('<div>').append(svgElem).html();
}

由于使用jQuery的SVG操作与HTML元素略有不同,因此我将对象转换为字符串作为回报。详细信息可以从jquery's append not working with svg element?

找到

稍后,您可以使用

将图例图标粘贴到HTML
$('#legendText').prepend($(getIconLegend(yourFeature.getStyle())));

答案 3 :(得分:0)

如果使用WMS服务而不是WFS或者用于获取功能的任何方法,那么使用普通OpenLayers可以获得最接近的效果。 WMS的请求类型为GetLegendGraphic,顾名思义,它允许您动态获取显示应用于图层的样式的图像。

答案 4 :(得分:0)

半人半熟的传说可以做成:

用于WMS功能
借助Geoserver,GetLegendGraphic可以为每个WMS图层生成图像图例

用于WFS功能
对于每个图层,都可以基于图例的style属性来构建图例:

  • 填充颜色(例如:style.getFill().getColor()
  • 描边的属性(例如:style.getStroke().getColor()
  • 图像的图像,HTMLElement,取决于样式(例如:style.getImage().getImage()