[GIS] Changing the line color of a marker box using OpenLayers

javascriptopenlayers-2web-mapping

I'm trying to figure out how to change the line color of a marker box in openlayers. I've been learning from the following example:

enter image description here

<!DOCTYPE html>
<html>

<head>
        <meta http-equiv="Content-Type" content="text/html; charset=utf-8">
        <meta name="viewport" content="width=device-width, initial-scale=1.0, maximum-scale=1.0, user-scalable=0">
        <meta name="apple-mobile-web-app-capable" content="yes">
        <title>OpenLayers Boxes Vector Example</title>
        <link rel="stylesheet" href="http://openlayers.org/dev/theme/default/style.css" type="text/css">
        <link rel="stylesheet" href="http://openlayers.org/dev/examples/style.css" type="text/css">
        <script src="http://openlayers.org/dev/OpenLayers.js"></script>
        <script type="text/javascript">
            var box_extents = [
                [-10, 50, 5, 60],
                [-75, 41, -71, 44],
                [-122.6, 37.6, -122.3, 37.9],
                [10, 10, 20, 20]
            ];
            var map;
            function init(){           


                map = new OpenLayers.Map('map');

                var ol_wms = new OpenLayers.Layer.WMS( "OpenLayers WMS",
                    "http://vmap0.tiles.osgeo.org/wms/vmap0?", {layers: 'basic'} );

                var boxes  = new OpenLayers.Layer.Vector( "Boxes" );            

                for (var i = 0; i < box_extents.length; i++) {
                    ext = box_extents[i];
                    bounds = OpenLayers.Bounds.fromArray(ext);

                    box = new OpenLayers.Feature.Vector(bounds.toGeometry());
                    boxes.addFeatures(box);
                }

                map.addLayers([ol_wms, boxes]);
                map.addControl(new OpenLayers.Control.LayerSwitcher());
                var sf = new OpenLayers.Control.SelectFeature(boxes);
                map.addControl(sf);
                sf.activate();
                map.zoomToMaxExtent();
            }
        </script>
    </head>
    <body onload="init()">
        <h1 id="title">Boxes Example Vector</h1>

        <div id="tags">
            box, vector, annotation
        </div>

        <p id="shortdesc">
            Demonstrate marker and box type annotations on a map.
        </p>

        <div id="map" class="smallmap"></div>

        <div id="docs"></div>
    </body>

</html>

What I tried doing was replacing the boxes line with the following:

            //var boxes  = new OpenLayers.Layer.Vector( "Boxes" );
            var styleMapBoxes = new OpenLayers.StyleMap({borderColor: '#008000'});
        var boxes  = new OpenLayers.Layer.Boxes( 'Boxes', {styleMap: styleMapBoxes});  

but it did not work. I've read through the example here, but still do not understand how I can edit the box style. According to the documentation borderColor is a valid attribute.

Best Answer

You should use fillColor and strokeColor to style your items. Have a look on the documentation page.

You will find below an example based on your code :

<script type="text/javascript">
        var box_extents = [
            [-10, 50, 5, 60],
            [-75, 41, -71, 44],
            [-122.6, 37.6, -122.3, 37.9],
            [10, 10, 20, 20]
        ];
        var map;
        function init(){           


            map = new OpenLayers.Map('map');

            var ol_wms = new OpenLayers.Layer.WMS( "OpenLayers WMS",
                "http://vmap0.tiles.osgeo.org/wms/vmap0?", {layers: 'basic'} );

    var style = new OpenLayers.Style({
        fillColor: "#008000",
        strokeColor: "#000000",
        strokeWidth: 2
    });

            var boxes  = new OpenLayers.Layer.Vector( "Boxes" ,{
        styleMap: new OpenLayers.StyleMap(style)
    })         

            for (var i = 0; i < box_extents.length; i++) {
                ext = box_extents[i];
                bounds = OpenLayers.Bounds.fromArray(ext);

                box = new OpenLayers.Feature.Vector(bounds.toGeometry());
                boxes.addFeatures(box);
            }

            map.addLayers([ol_wms, boxes]);
            map.addControl(new OpenLayers.Control.LayerSwitcher());
            var sf = new OpenLayers.Control.SelectFeature(boxes);
            map.addControl(sf);
            sf.activate();
            map.zoomToMaxExtent();
        }
    </script>

>> See the online example.