3

http://www.cv.nctu.edu.tw/shapefile-js-openlayers/ol_simple.html

this is my website for test.

I add two layers in openlayers.

And I want to change color in one of two layers.

var myStyle = new OpenLayers.Style({fill: true,fillColor: "#ff0000"});
var BounderyshpLayer = new OpenLayers.Layer.Vector({projection: new OpenLayers.Projection('EPSG:3826'),style: myStyle});

enter image description here

why is it not work!?

1
  • You should remove the extra comma after sm
    – Thomas B
    Commented Feb 2, 2015 at 7:49

1 Answer 1

2

you could use this:

var myStyle= {fillColor: "#ff0000"};

             var BounderyshpLayer = new OpenLayers.Layer.Vector({
             projection: new OpenLayers.Projection('EPSG:3826'),
             style: myStyle
             });

see: http://jsfiddle.net/expedio/2xvfbtdw/

or even use a stylemap with different style for default and select:

var defStyle = {fillColor: "#ff0000",strokeColor: "green", strokeOpacity: "0.7", strokeWidth: 3, cursor: "pointer"};
        var sty = OpenLayers.Util.applyDefaults(defStyle, OpenLayers.Feature.Vector.style["default"]);

        var sm = new OpenLayers.StyleMap({
            'default': sty,
            'select': {strokeColor: "red", fillColor: "green"}
        });

        var BounderyshpLayer = new OpenLayers.Layer.Vector({
             projection: new OpenLayers.Projection('EPSG:3826'),
             styleMap: sm,
             });

see: http://jsfiddle.net/expedio/yxr5wdjo/

nice website with Information about the Feature-Style and Stylemap: http://www.peterrobins.co.uk/it/olstyle.html

PS: Just for demonstration purposes: If you open your website and use the console of your debug-tool (firebug for example) you run this code in your developer tools (non permanent change of course):

map.layers[2].style={fillColor: "#ff0000",fillOpacity:"0.5"};map.layers[2].redraw();

have a look at the screenshot:

enter image description here

2
  • I tried your method. But it is not working, too.Can you help me again!? Commented Feb 1, 2015 at 12:40
  • can your post you last try here to see whats wrong? I just update my answer with something that you can also try...
    – Thomas B
    Commented Feb 1, 2015 at 13:30

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.