1 <html xmlns="http://www.w3.org/1999/xhtml">
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">
15 <script src="../lib/OpenLayers.js"></script>
16 <script type="text/javascript">
18 // create a semi-random grid of features to be clustered
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}
33 var map, strategy, clusters;
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"],
43 var style = new OpenLayers.Style({
44 pointRadius: "${radius}",
47 strokeColor: "#cc6633",
48 strokeWidth: "${width}",
52 width: function(feature) {
53 return (feature.cluster) ? 2 : 1;
55 radius: function(feature) {
58 pix = Math.min(feature.attributes.count, 7) + 2;
65 strategy = new OpenLayers.Strategy.Cluster();
67 clusters = new OpenLayers.Layer.Vector("Clusters", {
68 strategies: [strategy],
69 styleMap: new OpenLayers.StyleMap({
73 strokeColor: "#32a8a9"
78 var select = new OpenLayers.Control.SelectFeature(
79 clusters, {hover: true}
81 map.addControl(select);
83 clusters.events.on({"featureselected": display});
85 map.addLayers([base, clusters]);
86 map.setCenter(new OpenLayers.LonLat(0, 0), 2);
89 $("reset").onclick = 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);
104 function display(event) {
105 var f = event.feature;
106 var el = $("output");
108 el.innerHTML = "cluster of " + f.attributes.count;
110 el.innerHTML = "unclustered " + f.geometry;
117 <body onload="init()">
118 <h1 id="title">Cluster Strategy Threshold</h1>
120 Demonstrates the use of the cluster strategy threshold property.
122 <div id="map" class="smallmap"></div>
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>
132 <p>Cluster details: <span id="output">hover over a feature to see details.</span></p>
135 <input id="distance" name="distance" type="text" value="" size="3" />
136 <label for="distance">distance</label>
139 <input id="threshold" name="threshold" type="text" value="" size="3" />
140 <label for="threshold">threshold</label>
143 <button id="reset">recluster</button>