Do you know how to have a nice clustering in OpenLayers such as this google example ?
-
1Hi, you are not talking about Openlayer markers right? ( http://dev.openlayers.org/docs/files/OpenLayers/Marker-js.html ) – eMarine Sep 07 '15 at 10:17
-
@eMarine no about marker clusters – apneadiving Sep 07 '15 at 10:22
6 Answers
You can add label to pointStyle in above example and explain context of this label. Your code should be something like this:
var pointStyle = new OpenLayers.Style({
// ...
'label': "${label}",
// ...
}, {
context: {
// ...
label: function(feature) {
// clustered features count or blank if feature is not a cluster
return feature.cluster ? feature.cluster.length : "";
}
// ..
}
});
var styleMap = new OpenLayers.StyleMap({
'default': pointStyle,
});
var googleLikeLayer = new OpenLayers.Layer.Vector("GoogleLikeLayer", {
// ...
styleMap : styleMap,
// ...
});

- 488
- 8
- 18
Use OpenLayers.Strategy.Cluster
for clustering.
-
-
so what do you want? you have answer to you question - you use Clustering strategy and style it with the same images as Google does! – igorti Jul 10 '11 at 16:55
-
The example I gave presents clusters with numbers inside the design + they auto-disappear to let the marker appear when relevant. – apneadiving Jul 10 '11 at 16:57
I have just implemented a so called AnimatedCluster strategy for OpenLayers. You can see a bit more about it at: http://www.acuriousanimal.com/2012/08/19/animated-marker-cluster-strategy-for-openlayers.html
It is only a firts version but adds a nice animation to the clusters. There are many things to improve but it is a starting point.

- 4,800
- 3
- 32
- 41
-
Do you mind to adapt your wonderful AnimatedCluster strategy to fit also a last versions of OpenLayers, i.e. 2.13+? I've tested it a bit, but seems there is a certain conflict in animations between cluster easing and layer's zoom change (they made animation for this in last versions). Pity there are no clusters in OpenLayers 3 implemented yet, maybe it will be good field to adapt AnimatedClusters too? ;) (btw, OL3 looks great already). – unibasil Feb 21 '14 at 01:20
-
-
updated to http://www.acuriousanimal.com/2012/08/19/animated-marker-cluster-strategy-for-openlayers.html. Thanks !!! – acanimal Apr 15 '16 at 14:40
There's a great clustering example available in OpenLayers 3.
I created a jsFiddle from the code so you can play with it.
Basically you have to create an ol.source.Cluster
with a grouping distance from an ol.source.Vector
formed by an array of ol.Feature
. Each ol.Feature
created from your source coordinates in the form of ol.geom.Point
.
var features = [
new ol.Feature(new ol.geom.Point([lon1, lat1])),
new ol.Feature(new ol.geom.Point([lon2, lat2])),
...
];
var cluster = new ol.source.Cluster({
distance: 50,
source: new ol.source.Vector({ features: features });
});
var map = new ol.Map({
layers: [
new ol.source.MapQuest({layer: 'sat'}), // Map
new ol.layer.Vector({ source: cluster }) // Clusters
],
renderer: 'canvas',
target: 'map'
});

- 5,818
- 5
- 42
- 58
Here is the JSfiddle for clustering based on custom attributes added to the layers. I struggled a bit with this so putting it here; Also shows creating a summary pie graph image when zoomed out with the clustered data http://jsfiddle.net/alexcpn/518p59k4/
Also created a small openlayer tutorial to explain this OpenLayers Advanced Clustering
var getClusterCount = function (feature) {
var clustercount = {};
var planningcount = 0;
var onaircount = 0;
var inerrorcount = 0;
for (var i = 0; i < feature.cluster.length; i++) {
if (feature.cluster[i].attributes.cluster) {
//THE MOST IMPORTANT LINE IS THE ONE BELOW, While clustering open layers removes the orginial feature layer with its own. So to get the attributes of the feature you have added add it to the openlayer created feature layer
feature.attributes.cluster = feature.cluster[i].attributes.cluster;
switch (feature.cluster[i].attributes.cluster) {
......
return clustercount;
};

- 5,287
- 3
- 59
- 71
you can do this with as igorti has said. the soltion is using OpenLayers.Strategy.Cluster class and styling your layer with OpenLayers.Style class...
for styling :
var pointStyle = new OpenLayers.Style({
'default': new OpenLayers.Style({
'pointRadius': '${radius}',
'externalGraphic': '${getgraph}'
....
},{
context:{
radius: function(feature){
return Math.min(feature.attributes.count,7)+3;
},{
getgraph : function(feature){
return 'ol/img/googlelike.png';
}}}};
it must helps you, more power to you!

- 2,274
- 1
- 19
- 45
-
1I fear there won't be the number of markers displayed on the clusterer. It's still not what I am looking for. – apneadiving Jul 13 '11 at 11:53