如何为同一图层中的每个要素设置不同的颜色样式-OpenLayers 2 [英] How to Style each feature in the same layer with different colors - OpenLayers 2

查看:903
本文介绍了如何为同一图层中的每个要素设置不同的颜色样式-OpenLayers 2的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

下面给出的代码段简要介绍了如何向矢量层添加要素.

The snippet given below gives a brief idea on how I am adding features to the vector layer.

vectors = new OpenLayers.Layer.Vector("Vector Layer");

drawControls = {                    
    Point1: new OpenLayers.Control.DrawFeature(vectors, OpenLayers.Handler.Point),
    Point2: new OpenLayers.Control.DrawFeature(vectors, OpenLayers.Handler.Point),
    Polygon1: new OpenLayers.Control.DrawFeature(vectors, OpenLayers.Handler.Polygon),
    Polygon2: new OpenLayers.Control.DrawFeature(vectors, OpenLayers.Handler.Polygon) 
}

for( var key in drawControls ) {
    map.addControl(drawControls[key]);
}

如何为每个功能设置不同的颜色?

How do I style each feature with different colours?.

我知道我可以使用下面给出的代码对矢量层进行整体样式设置:

I know I can style the vector layer as a whole with the code given below :

var defStyle_layer = { 
    strokeColor: "blue",
    fillColor: "blue"
};

var layer_style= OpenLayers.Util.applyDefaults(defStyle_layer, OpenLayers.Feature.Vector.style["default"]);

var layer_sm= new OpenLayers.StyleMap({
    'default': layer_style
}); 

vectors = new OpenLayers.Layer.Vector("Vector Layer", {stylemap:layer_sm })

我需要帮助分别设计同一矢量层中的每个要素的样式. 我需要将所有这些功能放置在同一层中的原因是因为我想拥有"UNDO"选项

I need help with styling each feature in the same vector layer separately. The reason I need to place all these features in the same layer is because I want to have "UNDO" option

推荐答案

我找到了我发布的问题的解决方案:这是下面给出的代码片段:

I found solution to the question posted by me: Here is the code snippet given below:

<script src="lib/OpenLayers.js"></script>
<script src="lib/GeometricNet/UndoRedo.js"></script>
.......
.......
Rest of the code
.......
.......
var defStyle_point1 = {strokeColor: "Darkblue", strokeOpacity: "0.5", strokeWidth:3,  cursor: "pointer", fillColor: "Darkblue"};
var point1_style = OpenLayers.Util.applyDefaults(defStyle_point1,OpenLayers.Feature.Vector.style["default"]);
var point1_sm = new OpenLayers.StyleMap({
                    'default': point1_style
                    }); 

var defStyle_polygon1 = {strokeColor: "Red", strokeOpacity: "0.5", strokeWidth: 3, cursor: "pointer", fillColor: "Red"};
var polygon1_style = OpenLayers.Util.applyDefaults(defStyle_polygon1, Openlayers.Feature.Vector.style["default"]);
var polygon1_sm = new OpenLayers.StyleMap({
                    'default': polygon1_style
                    }); 


point1_layer= new OpenLayers.Layer.Vector("Point1 Layer",{styleMap: point1_sm});
polygon1_layer= new OpenLayers.Layer.Vector("Polygon1 Layer",{styleMap: polygon1_sm});

drawControls = {                    
                Point1: new OpenLayers.Control.DrawFeature(point1_layer, OpenLayers.Handler.Point),
                Polygon1: new OpenLayers.Control.DrawFeature(polygon1_layer,OpenLayers.Handler.Polygon),
               }

for( var key in drawControls ) {
            map.addControl(drawControls[key]);
}

undoRedo = new UndoRedo([point1_layer,polygon1_layer]);

这篇关于如何为同一图层中的每个要素设置不同的颜色样式-OpenLayers 2的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆