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