سؤال

I tried some ways but cant seem to find the proper one,
How can I ad a style to this layer:

        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
                })
            })
        });

Just like in the layer below:

            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()]
                    }
            );

The differences are that in the first variable Im using Vector and in the second GML
Im still a beginner in this stuff, any help would be really appreciated.

هل كانت مفيدة؟

المحلول

You can define style like this:

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;

And then pass it in the options when you create Vector layer:

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
                })
            })
        });
مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top