]> dev.renevier.net Git - syp.git/blob - openlayers/examples/strategy-cluster-threshold.html
initial commit
[syp.git] / openlayers / examples / strategy-cluster-threshold.html
1 <html xmlns="http://www.w3.org/1999/xhtml">
2     <head>
3         <title>OpenLayers Cluster Strategy Threshold</title>
4         <link rel="stylesheet" href="../theme/default/style.css" type="text/css" />
5         <link rel="stylesheet" href="style.css" type="text/css" />
6         <style type="text/css">
7             ul {
8                 list-style: none;
9                 padding-left: 2em;
10             }
11             #reset {
12                 margin-left: 2em;
13             }
14         </style>
15         <script src="../lib/OpenLayers.js"></script>
16         <script type="text/javascript">
17
18             // create a semi-random grid of features to be clustered
19             var dx = 3;
20             var dy = 3;
21             var px, py;
22             var features = [];
23             for(var x=-45; x<=45; x+=dx) {
24                 for(var y=-22.5; y<=22.5; y+=dy) {
25                     px = x + (2 * dx * (Math.random() - 0.5));
26                     py = y + (2 * dy * (Math.random() - 0.5));
27                     features.push(new OpenLayers.Feature.Vector(
28                         new OpenLayers.Geometry.Point(px, py), {x: px, y: py}
29                     ));
30                 }
31             }
32
33             var map, strategy, clusters;
34             function init() {
35                 map = new OpenLayers.Map('map');
36                 var base = new OpenLayers.Layer.WMS("OpenLayers WMS", 
37                     ["http://t3.labs.metacarta.com/wms-c/Basic.py",
38                      "http://t2.labs.metacarta.com/wms-c/Basic.py",
39                      "http://t1.labs.metacarta.com/wms-c/Basic.py"], 
40                     {layers: 'satellite'}
41                 );
42
43                 var style = new OpenLayers.Style({
44                     pointRadius: "${radius}",
45                     fillColor: "#ffcc66",
46                     fillOpacity: 0.8,
47                     strokeColor: "#cc6633",
48                     strokeWidth: "${width}",
49                     strokeOpacity: 0.8
50                 }, {
51                     context: {
52                         width: function(feature) {
53                             return (feature.cluster) ? 2 : 1;
54                         },
55                         radius: function(feature) {
56                             var pix = 2;
57                             if(feature.cluster) {
58                                 pix = Math.min(feature.attributes.count, 7) + 2;
59                             }
60                             return pix;
61                         }
62                     }
63                 });
64                 
65                 strategy = new OpenLayers.Strategy.Cluster();
66
67                 clusters = new OpenLayers.Layer.Vector("Clusters", {
68                     strategies: [strategy],
69                     styleMap: new OpenLayers.StyleMap({
70                         "default": style,
71                         "select": {
72                             fillColor: "#8aeeef",
73                             strokeColor: "#32a8a9"
74                         }
75                     })
76                 });
77                 
78                 var select = new OpenLayers.Control.SelectFeature(
79                     clusters, {hover: true}
80                 );
81                 map.addControl(select);
82                 select.activate();
83                 clusters.events.on({"featureselected": display});
84
85                 map.addLayers([base, clusters]);
86                 map.setCenter(new OpenLayers.LonLat(0, 0), 2);
87                 
88                 reset();
89                 $("reset").onclick = reset;
90
91             }
92             
93             function reset() {
94                 var distance = parseInt($("distance").value);
95                 var threshold = parseInt($("threshold").value);
96                 strategy.distance = distance || strategy.distance;
97                 strategy.threshold = threshold || strategy.threshold;
98                 $("distance").value = strategy.distance;
99                 $("threshold").value = strategy.threshold || "null";
100                 clusters.removeFeatures(clusters.features);
101                 clusters.addFeatures(features);
102             }
103             
104             function display(event) {
105                 var f = event.feature;
106                 var el = $("output");
107                 if(f.cluster) {
108                     el.innerHTML = "cluster of " + f.attributes.count;
109                 } else {
110                     el.innerHTML = "unclustered " + f.geometry;
111                 }
112             }
113             
114             
115         </script>
116     </head>
117     <body onload="init()">
118         <h1 id="title">Cluster Strategy Threshold</h1>
119         <p id="shortdesc">
120             Demonstrates the use of the cluster strategy threshold property.
121         </p>
122         <div id="map" class="smallmap"></div>
123         <div id="docs">
124             <p>The Cluster strategy lets you display points representing clusters
125             of features within some pixel distance.  You can control the behavior
126             of the cluster strategy by setting its distance and threshold properties.
127             The distance determines the search radius (in pixels) for features to
128             cluster.  The threshold determines the minimum number of features to
129             be considered a cluster.</p>
130         </div>
131         <br />
132         <p>Cluster details: <span id="output">hover over a feature to see details.</span></p>
133         <ul>
134             <li>
135                 <input id="distance" name="distance" type="text" value="" size="3" />
136                 <label for="distance">distance</label>
137             </li>
138             <li>
139                 <input id="threshold" name="threshold" type="text" value="" size="3" />
140                 <label for="threshold">threshold</label>
141             </li>
142         </ul>
143         <button id="reset">recluster</button>
144     </body>
145 </html>