]> dev.renevier.net Git - syp.git/blob - openlayers/examples/stylemap.html
initial commit
[syp.git] / openlayers / examples / stylemap.html
1 <html xmlns="http://www.w3.org/1999/xhtml">
2   <head>
3     <title>OpenLayers StyleMap</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');
12             var wms = new OpenLayers.Layer.WMS(
13                 "OpenLayers WMS",
14                 "http://labs.metacarta.com/wms/vmap0",
15                 {layers: 'basic'}
16             );
17             
18             // Create 50 random features, and give them a "type" attribute that
19             // will be used to style them by size.
20             var features = new Array(50);
21             for (var i=0; i<features.length; i++) {
22                 features[i] = new OpenLayers.Feature.Vector(
23                     new OpenLayers.Geometry.Point(
24                         (360 * Math.random()) - 180, (180 * Math.random()) - 90
25                     ), {
26                         type: 5 + parseInt(5 * Math.random())
27                     }
28                 );
29             }
30             
31             // Create a styleMap to style your features for two different
32             // render intents.  The style for the 'default' render intent will
33             // be applied when the feature is first drawn.  The style for the
34             // 'select' render intent will be applied when the feature is
35             // selected.
36             var myStyles = new OpenLayers.StyleMap({
37                 "default": new OpenLayers.Style({
38                     pointRadius: "${type}", // sized according to type attribute
39                     fillColor: "#ffcc66",
40                     strokeColor: "#ff9933",
41                     strokeWidth: 2
42                 }),
43                 "select": new OpenLayers.Style({
44                     fillColor: "#66ccff",
45                     strokeColor: "#3399ff"
46                 })
47             });
48             
49             // Create a vector layer and give it your style map.
50             var points = new OpenLayers.Layer.Vector(
51                 'Points', {styleMap: myStyles}
52             );
53             points.addFeatures(features);
54             map.addLayers([wms, points]);
55             
56             // Create a select feature control and add it to the map.
57             var select = new OpenLayers.Control.SelectFeature(points, {hover: true});
58             map.addControl(select);
59             select.activate();
60             
61             map.setCenter(new OpenLayers.LonLat(0, 0), 1);
62         }
63     </script>
64   </head>
65   <body onload="init()">
66     <h1 id="title">StyleMap Example</h1>
67
68     <div id="tags"></div>
69
70     <p id="shortdesc">
71         Shows how to use a StyleMap to style features with rule based styling.
72         A style map references on or more OpenLayers.Style objects.  These
73         OpenLayers.Style objects are collections of OpenLayers.Rule objects
74         that determine how features are styled.  An OpenLayers.Rule object
75         combines an OpenLayers.Filter object with a symbolizer.  A filter is used
76         to determine whether a rule applies for a given feature, and a symbolizer
77         is used to draw the feature if the rule applies.
78     </p>
79
80     <div id="map" class="smallmap"></div>
81
82     <div id="docs">
83         <p>A style map is used with vector layers to define styles for various
84         rendering intents.  The style map used here has styles defined for the
85         "default" and "select" rendering intents.  This map also has an active
86         select feature control.  When you hover over features, they are selected
87         and drawn with the style corresponding the the "select" render intent.
88         </p>
89     </div>
90   </body>
91 </html>