1 | <!DOCTYPE html> |
---|
2 | <html> |
---|
3 | <head> |
---|
4 | <meta http-equiv="Content-Type" content="text/html; charset=utf-8"> |
---|
5 | <meta name="viewport" content="width=device-width, initial-scale=1.0, maximum-scale=1.0, user-scalable=0"> |
---|
6 | <meta name="apple-mobile-web-app-capable" content="yes"> |
---|
7 | <title>OpenLayers Vector Styles</title> |
---|
8 | <link rel="stylesheet" href="../theme/default/style.css" type="text/css"> |
---|
9 | <link rel="stylesheet" href="style.css" type="text/css"> |
---|
10 | <script src="../lib/OpenLayers.js"></script> |
---|
11 | <script type="text/javascript"> |
---|
12 | var map; |
---|
13 | |
---|
14 | function init(){ |
---|
15 | map = new OpenLayers.Map('map', {maxResolution:'auto'}); |
---|
16 | var wms = new OpenLayers.Layer.WMS( "OpenLayers WMS", |
---|
17 | "http://vmap0.tiles.osgeo.org/wms/vmap0", {layers: 'basic'} ); |
---|
18 | map.addLayer(wms); |
---|
19 | map.setCenter(new OpenLayers.LonLat(0, 0), 0); |
---|
20 | |
---|
21 | // Strategy 1: Style features based on some attribute. |
---|
22 | |
---|
23 | // create 50 random features in the northern hemisphere |
---|
24 | // give them a "type" attribute that will be used to style |
---|
25 | // them by size |
---|
26 | var features = new Array(50); |
---|
27 | for (var i=0; i<features.length; i++) { |
---|
28 | features[i] = new OpenLayers.Feature.Vector( |
---|
29 | new OpenLayers.Geometry.Point( |
---|
30 | (360 * Math.random()) - 180, 90 * Math.random() |
---|
31 | ), { |
---|
32 | type: 5 + parseInt(5 * Math.random()) |
---|
33 | } |
---|
34 | ); |
---|
35 | } |
---|
36 | |
---|
37 | // allow testing of specific renderers via "?renderer=Canvas", etc |
---|
38 | var renderer = OpenLayers.Util.getParameters(window.location.href).renderer; |
---|
39 | renderer = (renderer) ? [renderer] : OpenLayers.Layer.Vector.prototype.renderers; |
---|
40 | |
---|
41 | // create the layer styleMap with a simple symbolizer template |
---|
42 | var layer1 = new OpenLayers.Layer.Vector('Points', { |
---|
43 | styleMap: new OpenLayers.StyleMap({ |
---|
44 | pointRadius: "${type}", // based on feature.attributes.type |
---|
45 | fillColor: "#666666" |
---|
46 | }), |
---|
47 | renderers: renderer |
---|
48 | }); |
---|
49 | layer1.addFeatures(features); |
---|
50 | |
---|
51 | // Strategy 2: Style features based on something besides attributes. |
---|
52 | |
---|
53 | // create 50 random features in the southern hemisphere |
---|
54 | var features = new Array(50); |
---|
55 | for (var i=0; i<features.length; i++) { |
---|
56 | features[i] = new OpenLayers.Feature.Vector( |
---|
57 | new OpenLayers.Geometry.Point( |
---|
58 | (360 * Math.random()) - 180, -90 * Math.random() |
---|
59 | ), { |
---|
60 | type: 5 + parseInt(5 * Math.random()) |
---|
61 | } |
---|
62 | ); |
---|
63 | } |
---|
64 | // create the layer styleMap by giving the default style a context |
---|
65 | var colors = ["red", "green", "blue"]; |
---|
66 | var context = { |
---|
67 | getColor: function(feature) { |
---|
68 | var region = parseInt((feature.geometry.x + 180) / 120); |
---|
69 | return colors[region]; |
---|
70 | }, |
---|
71 | getSize: function(feature) { |
---|
72 | return feature.attributes["type"] / map.getResolution() * .703125; |
---|
73 | } |
---|
74 | }; |
---|
75 | var template = { |
---|
76 | pointRadius: "${getSize}", // using context.getSize(feature) |
---|
77 | fillColor: "${getColor}" // using context.getColor(feature) |
---|
78 | }; |
---|
79 | var style = new OpenLayers.Style(template, {context: context}); |
---|
80 | var layer2 = new OpenLayers.Layer.Vector('Points', { |
---|
81 | styleMap: new OpenLayers.StyleMap(style), |
---|
82 | renderers: renderer |
---|
83 | }); |
---|
84 | layer2.addFeatures(features); |
---|
85 | |
---|
86 | |
---|
87 | map.addLayers([layer1, layer2]); |
---|
88 | } |
---|
89 | </script> |
---|
90 | </head> |
---|
91 | <body onload="init()"> |
---|
92 | <h1 id="title">Feature Styles Example</h1> |
---|
93 | |
---|
94 | <div id="tags"> |
---|
95 | vector, feature, stylemap, light |
---|
96 | </div> |
---|
97 | |
---|
98 | <p id="shortdesc"> |
---|
99 | To style features with a custom function that evaluates each feature, use |
---|
100 | the context option of an OpenLayers.Style object. If the context object |
---|
101 | contains a function and this function is referenced in a symbolizer, the |
---|
102 | function will be called with the feature as an argument.. |
---|
103 | </p> |
---|
104 | |
---|
105 | <div id="map" class="smallmap"></div> |
---|
106 | |
---|
107 | <div id="docs"> |
---|
108 | <p>Features in the northern hemisphere are styled according to their |
---|
109 | "type" attribute. This is accomplished with a simple template that |
---|
110 | is evaluated with the feature attributes as context.</p> |
---|
111 | <p>Features in the sourthern hemisphere are styled according to a |
---|
112 | combination of their attributes and non-attribute properties. This |
---|
113 | is accomplished using an advanced template that calls functions |
---|
114 | on the context object passed to the Style constructor.</p> |
---|
115 | </div> |
---|
116 | </body> |
---|
117 | </html> |
---|