Add analytics
[bus.git] / openlayers / examples / styles-context.html
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
<html xmlns="http://www.w3.org/1999/xhtml">
  <head>
    <title>OpenLayers Vector Styles</title>
    <link rel="stylesheet" href="../theme/default/style.css" type="text/css" />
    <link rel="stylesheet" href="style.css" type="text/css" />
    <script src="../lib/OpenLayers.js"></script>
    <script type="text/javascript">
        var map;
 
        function init(){
            map = new OpenLayers.Map('map', {maxResolution:'auto'});
            var wms = new OpenLayers.Layer.WMS( "OpenLayers WMS",
                "http://labs.metacarta.com/wms/vmap0", {layers: 'basic'} );
            map.addLayer(wms);
            map.setCenter(new OpenLayers.LonLat(0, 0), 0);
            
            // Strategy 1:  Style features based on some attribute.
            
            // create 50 random features in the northern hemisphere
            // give them a "type" attribute that will be used to style
            // them by size
            var features = new Array(50);
            for (var i=0; i<features.length; i++) {
                features[i] = new OpenLayers.Feature.Vector(
                    new OpenLayers.Geometry.Point(
                        (360 * Math.random()) - 180, 90 * Math.random()
                    ), {
                        type: 5 + parseInt(5 * Math.random())
                    }
                );
            }
            
            // create the layer styleMap with a simple symbolizer template
            var layer1 = new OpenLayers.Layer.Vector('Points', {
                styleMap: new OpenLayers.StyleMap({
                    pointRadius: "${type}", // based on feature.attributes.type
                    fillColor: "#666666"
                })
            });
            layer1.addFeatures(features);
 
            // Strategy 2:  Style features based on something besides attributes.
 
            // create 50 random features in the southern hemisphere
            var features = new Array(50);
            for (var i=0; i<features.length; i++) {
                features[i] = new OpenLayers.Feature.Vector(
                    new OpenLayers.Geometry.Point(
                        (360 * Math.random()) - 180, -90 * Math.random()
                    ), {
                        type: 5 + parseInt(5 * Math.random())
                    }
                );
            }
            // create the layer styleMap by giving the default style a context
            var colors = ["red", "green", "blue"];
            var context = {
                getColor: function(feature) {
                    var region = parseInt((feature.geometry.x + 180) / 120);
                    return colors[region];
                },
                getType: function(feature) {
                    return feature.attributes["type"];
                }
            };
            var template = {
                pointRadius: "${getType}", // using context.getType(feature)
                fillColor: "${getColor}" // using context.getColor(feature)
            };
            var style = new OpenLayers.Style(template, {context: context});
            var layer2 = new OpenLayers.Layer.Vector('Points', {
                styleMap: new OpenLayers.StyleMap(style)
            });
            layer2.addFeatures(features);
 
 
            map.addLayers([layer1, layer2]);
        }
    </script>
  </head>
  <body onload="init()">
    <h1 id="title">Feature Styles Example</h1>
 
    <div id="tags"></div>
 
    <p id="shortdesc">
        To style features with a custom function that evaluates each feature, use
        the context option of an OpenLayers.Style object.  If the context object
        contains a function and this function is referenced in a symbolizer, the
        function will be called with the feature as an argument..
    </p>
 
    <div id="map" class="smallmap"></div>
 
    <div id="docs">
        <p>Features in the northern hemisphere are styled according to their
        "type" attribute.  This is accomplished with a simple template that
        is evaluated with the feature attributes as context.</p>
        <p>Features in the sourthern hemisphere are styled according to a
        combination of their attributes and non-attribute properties.  This
        is accomplished using an advanced template that calls functions
        on the context object passed to the Style constructor.</p>
    </div>
  </body>
</html>