Make events for clusters start with cluster, clusterclick etc.

This commit is contained in:
danzel
2012-07-24 13:26:47 +12:00
parent a8dc5f315b
commit 602a5ae1d2
5 changed files with 34 additions and 37 deletions

View File

@@ -53,11 +53,9 @@
} }
var polygon; var polygon;
var polygonCluster; var polygonCluster;
markers.on('mouseover', function (a) { markers.on('clustermouseover', function (a) {
if (a.layer instanceof L.MarkerCluster) {
console.log('cluster ' + a.layer.getAllChildMarkers().length); console.log('cluster ' + a.layer.getAllChildMarkers().length);
if (polygon) { if (polygon) {
@@ -65,13 +63,10 @@
} }
polygon = new L.Polygon(a.layer.getConvexHull()); polygon = new L.Polygon(a.layer.getConvexHull());
map.addLayer(polygon); map.addLayer(polygon);
} else {
console.log('marker ' + a.layer);
}
}); });
markers.on('mouseout', function (a) { markers.on('clustermouseout', function (a) {
if (a.layer instanceof L.MarkerCluster && polygon) { if (polygon) {
map.removeLayer(polygon); map.removeLayer(polygon);
polygon = null; polygon = null;
} }

View File

@@ -15,6 +15,7 @@
<script src="../src/MarkerClusterGroup.js"></script> <script src="../src/MarkerClusterGroup.js"></script>
<script src="../src/MarkerCluster.js"></script> <script src="../src/MarkerCluster.js"></script>
<script src="../src/MarkerCluster.QuickHull.js"></script> <script src="../src/MarkerCluster.QuickHull.js"></script>
<script src="../src/MarkerCluster.Spiderfier.js"></script>
</head> </head>
<body> <body>
@@ -54,9 +55,11 @@
southWest.lng + lngSpan * Math.random()); southWest.lng + lngSpan * Math.random());
} }
//Zoom on cluster click //Zoom or spiderfy on cluster click
markers.on('click', function (a) { markers.on('clusterclick', function (a) {
if (a.layer instanceof L.MarkerCluster) { if (map.getMaxZoom() === map.getZoom()) {
a.layer.spiderfy();
} else {
a.layer.zoomToBounds(); a.layer.zoomToBounds();
} }
}); });
@@ -64,17 +67,15 @@
//Show convex hull (boundary) polygon on mouse over //Show convex hull (boundary) polygon on mouse over
var polygon; var polygon;
var polygonCluster; var polygonCluster;
markers.on('mouseover', function (a) { markers.on('clustermouseover', function (a) {
if (a.layer instanceof L.MarkerCluster) {
if (polygon) { if (polygon) {
map.removeLayer(polygon); map.removeLayer(polygon);
} }
polygon = new L.Polygon(a.layer.getConvexHull()); polygon = new L.Polygon(a.layer.getConvexHull());
map.addLayer(polygon); map.addLayer(polygon);
}
}); });
markers.on('mouseout', function (a) { markers.on('clustermouseout', function (a) {
if (a.layer instanceof L.MarkerCluster && polygon) { if (polygon) {
map.removeLayer(polygon); map.removeLayer(polygon);
polygon = null; polygon = null;
} }

View File

@@ -1,4 +1,4 @@
<!DOCTYPE html> <!DOCTYPE html>
<html> <html>
<head> <head>
<title>Leaflet debug page</title> <title>Leaflet debug page</title>
@@ -52,13 +52,9 @@
southWest.lng + lngSpan * Math.random()); southWest.lng + lngSpan * Math.random());
} }
markers.on('click', function (a) { markers.on('clusterclick', function (a) {
if (a.layer instanceof L.MarkerCluster) {
console.log('cluster ' + a.layer.getAllChildMarkers().length); console.log('cluster ' + a.layer.getAllChildMarkers().length);
a.layer.spiderfy(); a.layer.spiderfy();
} else {
console.log('marker ' + a.layer);
}
}); });
populate(); populate();

View File

@@ -50,10 +50,8 @@
southWest.lng + lngSpan * Math.random()); southWest.lng + lngSpan * Math.random());
} }
markers.on('click', function (a) { markers.on('clusterclick', function (a) {
if (a.layer instanceof L.MarkerCluster) {
a.layer.zoomToBounds(); a.layer.zoomToBounds();
}
}); });
populate(); populate();

View File

@@ -32,6 +32,13 @@ L.MarkerClusterGroup = L.FeatureGroup.extend({
this._currentShownBounds = null; 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) { _sqDist: function (p1, p2) {
var dx = p2.x - p1.x, var dx = p2.x - p1.x,