From 602a5ae1d2b0d6967df07fdcc96dcc7b249ae255 Mon Sep 17 00:00:00 2001 From: danzel Date: Tue, 24 Jul 2012 13:26:47 +1200 Subject: [PATCH] Make events for clusters start with cluster, clusterclick etc. --- example/marker-clustering-convexhull.html | 21 +++++++---------- example/marker-clustering-everything.html | 25 +++++++++++---------- example/marker-clustering-spiderfier.html | 12 ++++------ example/marker-clustering-zoomtobounds.html | 6 ++--- src/MarkerClusterGroup.js | 7 ++++++ 5 files changed, 34 insertions(+), 37 deletions(-) diff --git a/example/marker-clustering-convexhull.html b/example/marker-clustering-convexhull.html index 704fddbe2..518301cd0 100644 --- a/example/marker-clustering-convexhull.html +++ b/example/marker-clustering-convexhull.html @@ -53,25 +53,20 @@ } - var polygon; var polygonCluster; - markers.on('mouseover', function (a) { - if (a.layer instanceof L.MarkerCluster) { - console.log('cluster ' + a.layer.getAllChildMarkers().length); + markers.on('clustermouseover', function (a) { + console.log('cluster ' + a.layer.getAllChildMarkers().length); - if (polygon) { - map.removeLayer(polygon); - } - polygon = new L.Polygon(a.layer.getConvexHull()); - map.addLayer(polygon); - } else { - console.log('marker ' + a.layer); + if (polygon) { + map.removeLayer(polygon); } + polygon = new L.Polygon(a.layer.getConvexHull()); + map.addLayer(polygon); }); - markers.on('mouseout', function (a) { - if (a.layer instanceof L.MarkerCluster && polygon) { + markers.on('clustermouseout', function (a) { + if (polygon) { map.removeLayer(polygon); polygon = null; } diff --git a/example/marker-clustering-everything.html b/example/marker-clustering-everything.html index ebe416f89..be6ef724e 100644 --- a/example/marker-clustering-everything.html +++ b/example/marker-clustering-everything.html @@ -15,6 +15,7 @@ + @@ -54,9 +55,11 @@ southWest.lng + lngSpan * Math.random()); } - //Zoom on cluster click - markers.on('click', function (a) { - if (a.layer instanceof L.MarkerCluster) { + //Zoom or spiderfy on cluster click + markers.on('clusterclick', function (a) { + if (map.getMaxZoom() === map.getZoom()) { + a.layer.spiderfy(); + } else { a.layer.zoomToBounds(); } }); @@ -64,17 +67,15 @@ //Show convex hull (boundary) polygon on mouse over var polygon; var polygonCluster; - markers.on('mouseover', function (a) { - if (a.layer instanceof L.MarkerCluster) { - if (polygon) { - map.removeLayer(polygon); - } - polygon = new L.Polygon(a.layer.getConvexHull()); - map.addLayer(polygon); + markers.on('clustermouseover', function (a) { + if (polygon) { + map.removeLayer(polygon); } + polygon = new L.Polygon(a.layer.getConvexHull()); + map.addLayer(polygon); }); - markers.on('mouseout', function (a) { - if (a.layer instanceof L.MarkerCluster && polygon) { + markers.on('clustermouseout', function (a) { + if (polygon) { map.removeLayer(polygon); polygon = null; } diff --git a/example/marker-clustering-spiderfier.html b/example/marker-clustering-spiderfier.html index 592e1b5c3..47729ba0e 100644 --- a/example/marker-clustering-spiderfier.html +++ b/example/marker-clustering-spiderfier.html @@ -1,4 +1,4 @@ - + Leaflet debug page @@ -52,13 +52,9 @@ southWest.lng + lngSpan * Math.random()); } - markers.on('click', function (a) { - if (a.layer instanceof L.MarkerCluster) { - console.log('cluster ' + a.layer.getAllChildMarkers().length); - a.layer.spiderfy(); - } else { - console.log('marker ' + a.layer); - } + markers.on('clusterclick', function (a) { + console.log('cluster ' + a.layer.getAllChildMarkers().length); + a.layer.spiderfy(); }); populate(); diff --git a/example/marker-clustering-zoomtobounds.html b/example/marker-clustering-zoomtobounds.html index fff181ba5..23bfafe19 100644 --- a/example/marker-clustering-zoomtobounds.html +++ b/example/marker-clustering-zoomtobounds.html @@ -50,10 +50,8 @@ southWest.lng + lngSpan * Math.random()); } - markers.on('click', function (a) { - if (a.layer instanceof L.MarkerCluster) { - a.layer.zoomToBounds(); - } + markers.on('clusterclick', function (a) { + a.layer.zoomToBounds(); }); populate(); diff --git a/src/MarkerClusterGroup.js b/src/MarkerClusterGroup.js index 21cae4169..856bb3dd9 100644 --- a/src/MarkerClusterGroup.js +++ b/src/MarkerClusterGroup.js @@ -32,6 +32,13 @@ L.MarkerClusterGroup = L.FeatureGroup.extend({ this._currentShownBounds = null; }, + //Overrides FeatureGroup._propagateEvent + _propagateEvent: function (e) { + if (e.target instanceof L.MarkerCluster) { + e.type = 'cluster' + e.type; + } + L.FeatureGroup.prototype._propagateEvent.call(this, e); + }, _sqDist: function (p1, p2) { var dx = p2.x - p1.x,