1

我尝试了一些方法,但似乎找不到合适的方法,
如何在该图层中添加样式:

        var line_1 = new OpenLayers.Layer.Vector("Line nr 1", {
            projection: map.displayProjection,
            strategies: [new OpenLayers.Strategy.Fixed()],
            protocol: new OpenLayers.Protocol.HTTP({
                url: "lines/line_1.kml",
                format: newOpenLayers.Format.KML({
                    extractStyles: true,
                    extractAttributes: true
                })
            })
        });

就像在下面的图层中一样:

            var line_1 = new OpenLayers.Layer.GML('Line nr - 1', 
                    "lines/line_1.kml",
                    {
                            visibility: true,
                            format: OpenLayers.Format.KML,
                            style: {strokeWidth: 4, strokeColor: "#ff0000", strokeOpacity: 1 },
                            projection: map.displayProjection,
                            strategies: [new OpenLayers.Strategy.Fixed()]
                    }
            );

不同之处在于,在第一个变量中 Im 使用Vector,而在第二个GML
中, 我仍然是这方面的初学者,任何帮助都将不胜感激。

4

1 回答 1

3

您可以像这样定义样式:

var style = OpenLayers.Util.extend({}, OpenLayers.Feature.Vector.style['default']);
style.fillOpacity = 0.2;
style.graphicOpacity = 1;
style.strokeWidth = 4; 
style.strokeColor = "#ff0000";
style.strokeOpacity = 1;

然后在创建向量层时将其传递到选项中:

var line_1 = new OpenLayers.Layer.Vector("Line nr 1", {
            style : style,
            projection: map.displayProjection,
            strategies: [new OpenLayers.Strategy.Fixed()],
            protocol: new OpenLayers.Protocol.HTTP({
                url: "lines/line_1.kml",
                format: newOpenLayers.Format.KML({
                    extractStyles: true,
                    extractAttributes: true
                })
            })
        });
于 2011-10-15T15:24:46.367 回答