| 109 | | <html xmlns="http://www.w3.org/1999/xhtml"> |
|---|
| 110 | | <head> |
|---|
| 111 | | <title>OpenLayers Vector Styles</title> |
|---|
| 112 | | <link rel="stylesheet" href="../theme/default/style.css" type="text/css" /> |
|---|
| 113 | | <style type="text/css"> |
|---|
| 114 | | #map { |
|---|
| 115 | | width: 800px; |
|---|
| 116 | | height: 400px; |
|---|
| 117 | | border: 1px solid black; |
|---|
| 118 | | } |
|---|
| 119 | | </style> |
|---|
| 120 | | <script src="../lib/OpenLayers.js"></script> |
|---|
| 121 | | <script type="text/javascript"> |
|---|
| 122 | | var map; |
|---|
| 123 | | |
|---|
| 124 | | function init(){ |
|---|
| 125 | | map = new OpenLayers.Map('map', {maxResolution:'auto'}); |
|---|
| 126 | | var wms = new OpenLayers.Layer.WMS( "OpenLayers WMS", |
|---|
| 127 | | "http://labs.metacarta.com/wms/vmap0", {layers: 'basic'} ); |
|---|
| 128 | | map.addLayer(wms); |
|---|
| 129 | | map.setCenter(new OpenLayers.LonLat(0, 0), 0); |
|---|
| 130 | | |
|---|
| 131 | | // Strategy 1: Style features based on some attribute. |
|---|
| 132 | | |
|---|
| 133 | | // create 50 random features in the northern hemisphere |
|---|
| 134 | | // give them a "type" attribute that will be used to style |
|---|
| 135 | | // them by size |
|---|
| 136 | | var features = new Array(50); |
|---|
| 137 | | for (var i=0; i<features.length; i++) { |
|---|
| 138 | | features[i] = new OpenLayers.Feature.Vector( |
|---|
| 139 | | new OpenLayers.Geometry.Point( |
|---|
| 140 | | (360 * Math.random()) - 180, 90 * Math.random() |
|---|
| 141 | | ), { |
|---|
| 142 | | type: 5 + parseInt(5 * Math.random()) |
|---|
| 143 | | } |
|---|
| 144 | | ); |
|---|
| 145 | | } |
|---|
| 146 | | |
|---|
| 147 | | // create the layer styleMap with a simple symbolizer template |
|---|
| 148 | | var layer1 = new OpenLayers.Layer.Vector('Points', { |
|---|
| 149 | | styleMap: new OpenLayers.StyleMap({ |
|---|
| 150 | | pointRadius: "${type}", // based on feature.attributes.type |
|---|
| 151 | | fillColor: "#666666" |
|---|
| 152 | | }) |
|---|
| 153 | | }); |
|---|
| 154 | | layer1.addFeatures(features); |
|---|
| 155 | | |
|---|
| 156 | | // Strategy 2: Style features based on something besides attributes. |
|---|
| 157 | | |
|---|
| 158 | | // create 50 random features in the southern hemisphere |
|---|
| 159 | | var features = new Array(50); |
|---|
| 160 | | for (var i=0; i<features.length; i++) { |
|---|
| 161 | | features[i] = new OpenLayers.Feature.Vector( |
|---|
| 162 | | new OpenLayers.Geometry.Point( |
|---|
| 163 | | (360 * Math.random()) - 180, -90 * Math.random() |
|---|
| 164 | | ), { |
|---|
| 165 | | type: 5 + parseInt(5 * Math.random()) |
|---|
| 166 | | } |
|---|
| 167 | | ); |
|---|
| 168 | | } |
|---|
| 169 | | // create the layer styleMap by giving the default style a context |
|---|
| 170 | | var colors = ["red", "green", "blue"]; |
|---|
| 171 | | var context = { |
|---|
| 172 | | getColor: function(feature) { |
|---|
| 173 | | var region = parseInt((feature.geometry.x + 180) / 120); |
|---|
| 174 | | return colors[region]; |
|---|
| 175 | | }, |
|---|
| 176 | | getType: function(feature) { |
|---|
| 177 | | return feature.attributes["type"]; |
|---|
| 178 | | } |
|---|
| 179 | | }; |
|---|
| 180 | | var template = { |
|---|
| 181 | | pointRadius: "${getType}", // using context.getType(feature) |
|---|
| 182 | | fillColor: "${getColor}" // using context.getColor(feature) |
|---|
| 183 | | }; |
|---|
| 184 | | var style = new OpenLayers.Style(template, {context: context}); |
|---|
| 185 | | var layer2 = new OpenLayers.Layer.Vector('Points', { |
|---|
| 186 | | styleMap: new OpenLayers.StyleMap(style) |
|---|
| 187 | | }); |
|---|
| 188 | | layer2.addFeatures(features); |
|---|
| 189 | | |
|---|
| 190 | | |
|---|
| 191 | | map.addLayers([layer1, layer2]); |
|---|
| 192 | | } |
|---|
| 193 | | </script> |
|---|
| 194 | | </head> |
|---|
| 195 | | <body onload="init()"> |
|---|
| 196 | | <h1 id="title">Feature Styles Example</h1> |
|---|
| 197 | | |
|---|
| 198 | | <div id="tags"></div> |
|---|
| 199 | | |
|---|
| 200 | | <p id="shortdesc"> |
|---|
| 201 | | Shows how to create a feature styles. |
|---|
| 202 | | </p> |
|---|
| 203 | | |
|---|
| 204 | | <div id="map"></div> |
|---|
| 205 | | |
|---|
| 206 | | <div id="docs"> |
|---|
| 207 | | <p>Features in the northern hemisphere are styled according to their |
|---|
| 208 | | "type" attribute. This is accomplished with a simple template that |
|---|
| 209 | | is evaluated with the feature attributes as context.</p> |
|---|
| 210 | | <p>Features in the sourthern hemisphere are styled according to a |
|---|
| 211 | | combination of their attributes and non-attribute properties. This |
|---|
| 212 | | is accomplished using an advanced template that calls functions |
|---|
| 213 | | on the context object passed to the Style constructor.</p> |
|---|
| 214 | | </div> |
|---|
| 215 | | </body> |
|---|
| 216 | | </html> |
|---|