1 <html xmlns="http://www.w3.org/1999/xhtml">
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">
11 map = new OpenLayers.Map('map');
12 var wms = new OpenLayers.Layer.WMS(
14 "http://labs.metacarta.com/wms/vmap0",
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
26 type: 5 + parseInt(5 * Math.random())
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
36 var myStyles = new OpenLayers.StyleMap({
37 "default": new OpenLayers.Style({
38 pointRadius: "${type}", // sized according to type attribute
40 strokeColor: "#ff9933",
43 "select": new OpenLayers.Style({
45 strokeColor: "#3399ff"
49 // Create a vector layer and give it your style map.
50 var points = new OpenLayers.Layer.Vector(
51 'Points', {styleMap: myStyles}
53 points.addFeatures(features);
54 map.addLayers([wms, points]);
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);
61 map.setCenter(new OpenLayers.LonLat(0, 0), 1);
65 <body onload="init()">
66 <h1 id="title">StyleMap Example</h1>
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.
80 <div id="map" class="smallmap"></div>
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.