如何为我的功能(Vector LineString)添加样式?

时间:2014-03-13 14:14:59

标签: openlayers gis

我很困惑如何为我的功能添加样式,该功能包含一个Vector内部的LineString。我是否将样式添加到LineString或Vector?我想改变线条的颜色和大小,如果可能的话可能会使线条不透明。

mapObject = new OpenLayers.Layer.Vector("Vector");
var p1 = new OpenLayers.Geometry.Point(mapObjectTopLeftLon, mapObjectTopLeftLat);
var p2 = new OpenLayers.Geometry.Point(mapObjectTopRightLon, mapObjectTopRightLat);
var p3 = new OpenLayers.Geometry.Point(mapObjectBottomRightLon, mapObjectBottomRightLat);
var p4 = new OpenLayers.Geometry.Point(mapObjectBottomLeftLon, mapObjectBottomLeftLat);
var p5 = new OpenLayers.Geometry.Point(mapObjectTopLeftLon, mapObjectTopLeftLat);

var pnt = [];
pnt.push(p1,p2,p3,p4,p5);

mapObject.addFeatures([new OpenLayers.Feature.Vector(new OpenLayers.Geometry.LineString(pnt))]);

map.addLayer(mapObject);

1 个答案:

答案 0 :(得分:1)

实际上,您将样式添加到矢量图层,其中存储了要素,在您的情况下,它被命名为mapObject(但应该类似于featureLayer或myVectorLayer等)。 Here你可以找到一个关于stylig的博客,矢量图层和OpenLayers.StyleMap肯定会在这种情况下有用。

如果您需要更多样式(您可以区分许多不同的功能),您还可以根据"规则"更改各个功能的样式。 (OpenLayers.Rule)并且在OpenLayers.Filter.Comparison的帮助下,您可以为您的功能分配不同的符号。样式的一个示例:

myOpenLayersStyles.commentLayer = new OpenLayers.StyleMap({  //creates style for the vectorLayer features
    "default": new OpenLayers.Style({
        pointRadius: 20,
        fillColor: "#0000ff",
        fillOpacity: 1,
        strokeColor: "#0000ff",
        strokeWidth: 3,
        strokeOpacity: .7,
        cursor: "pointer",
        cursor: "hand" 
    },{
        rules: [
           new OpenLayers.Rule({

                    filter: new OpenLayers.Filter.Comparison({
                        type: OpenLayers.Filter.Comparison.EQUAL_TO,
                        property: "completed", 
                        value: 1
                    }),

                    symbolizer: {
                        graphicHeight: 25,
                        externalGraphic: "Img/comment_completed.png"
                    }
                }),
                new OpenLayers.Rule({
                    filter: new OpenLayers.Filter.Comparison({
                        type: OpenLayers.Filter.Comparison.EQUAL_TO,
                        property: "completed",
                        value: 0
                    }),
                    symbolizer: {
                        externalGraphic: "Img/comment.png",
                        graphicHeight: 25

                    }
                })            
            ]
      }
   )
        });