1 <html xmlns="http://www.w3.org/1999/xhtml">
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">
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'} );
15 map.setCenter(new OpenLayers.LonLat(0, 0), 0);
17 // Strategy 1: Style features based on some attribute.
19 // create 50 random features in the northern hemisphere
20 // give them a "type" attribute that will be used to style
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()
28 type: 5 + parseInt(5 * Math.random())
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
40 layer1.addFeatures(features);
42 // Strategy 2: Style features based on something besides attributes.
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()
51 type: 5 + parseInt(5 * Math.random())
55 // create the layer styleMap by giving the default style a context
56 var colors = ["red", "green", "blue"];
58 getColor: function(feature) {
59 var region = parseInt((feature.geometry.x + 180) / 120);
60 return colors[region];
62 getType: function(feature) {
63 return feature.attributes["type"];
67 pointRadius: "${getType}", // using context.getType(feature)
68 fillColor: "${getColor}" // using context.getColor(feature)
70 var style = new OpenLayers.Style(template, {context: context});
71 var layer2 = new OpenLayers.Layer.Vector('Points', {
72 styleMap: new OpenLayers.StyleMap(style)
74 layer2.addFeatures(features);
77 map.addLayers([layer1, layer2]);
81 <body onload="init()">
82 <h1 id="title">Feature Styles Example</h1>
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..
93 <div id="map" class="smallmap"></div>
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>