[Pkg-javascript-commits] [leaflet-markercluster] 223/479: Add zoomToShowLayer
Jonas Smedegaard
dr at jones.dk
Thu Oct 16 16:00:33 UTC 2014
This is an automated email from the git hooks/post-receive script.
js pushed a commit to branch master
in repository leaflet-markercluster.
commit 8f4dde032bdfc999953bd7db1331967dc288cc5e
Author: danzel <danzel at localhost.geek.nz>
Date: Tue Sep 11 16:24:05 2012 +1200
Add zoomToShowLayer
---
README.md | 6 +-
src/MarkerClusterGroup.js | 145 +++++++++++++++++++++++++---------------------
2 files changed, 85 insertions(+), 66 deletions(-)
diff --git a/README.md b/README.md
index 0963812..400c57f 100644
--- a/README.md
+++ b/README.md
@@ -88,7 +88,11 @@ markers.on('clusterclick', function (a) {
```
### Adding and removing Markers
-addLayer and removeLayer are supported and they should work for most uses.
+addLayer, removeLayer and clearLayers are supported and they should work for most uses.
+
+### Other Methods
+ hasLayer(layer): Returns true if the given layer (marker) is in the MarkerClusterGroup
+ zoomToShowLayer(layer, callback): Zooms to show the given marker (spidifying if required), calls the callback when the marker is visible on the map
### Handling LOTS of markers
The Clusterer can handle 10000 or even 50000 markers (in chrome). IE9 has some issues with 50000.
diff --git a/src/MarkerClusterGroup.js b/src/MarkerClusterGroup.js
index 0865ec0..e7904d6 100644
--- a/src/MarkerClusterGroup.js
+++ b/src/MarkerClusterGroup.js
@@ -108,71 +108,6 @@ L.MarkerClusterGroup = L.FeatureGroup.extend({
return this;
},
- //Remove the given object from the given array
- _arraySplice: function (anArray, obj) {
- for (var i = anArray.length - 1; i >= 0; i--) {
- if (anArray[i] === obj) {
- anArray.splice(i, 1);
- return;
- }
- }
- },
-
- _removeLayer: function (marker, removeFromDistanceGrid) {
- var gridClusters = this._gridClusters,
- gridUnclustered = this._gridUnclustered,
- map = this._map;
-
- //Remove the marker from distance clusters it might be in
- if (removeFromDistanceGrid) {
- for (var z = this._maxZoom; z >= 0; z--) {
- if (!gridUnclustered[z].removeObject(marker, map.project(marker.getLatLng(), z))) {
- break;
- }
- }
- }
-
- //Work our way up the clusters removing them as we go if required
- var cluster = marker.__parent,
- markers = cluster._markers,
- otherMarker;
-
- //Remove the marker from the immediate parents marker list
- this._arraySplice(markers, marker);
-
- while (cluster) {
- cluster._childCount--;
-
- if (cluster._zoom < 0) {
- //Top level, do nothing
- break;
- } else if (removeFromDistanceGrid && cluster._childCount <= 1) { //Cluster no longer required
- //We need to push the other marker up to the parent
- otherMarker = cluster._markers[0] === marker ? cluster._markers[1] : cluster._markers[0];
-
- //Update distance grid
- gridClusters[cluster._zoom].removeObject(cluster, map.project(cluster._cLatLng, cluster._zoom));
- gridUnclustered[cluster._zoom].addObject(otherMarker, map.project(otherMarker.getLatLng(), cluster._zoom));
-
- //Move otherMarker up to parent
- this._arraySplice(cluster.__parent._childClusters, cluster);
- cluster.__parent._markers.push(otherMarker);
- otherMarker.__parent = cluster.__parent;
-
- if (cluster._icon) {
- //Cluster is currently on the map, need to put the marker on the map instead
- L.FeatureGroup.prototype.removeLayer.call(this, cluster);
- L.FeatureGroup.prototype.addLayer.call(this, otherMarker);
- }
- } else {
- cluster._recalculateBounds();
- cluster._updateIcon();
- }
-
- cluster = cluster.__parent;
- }
- },
-
clearLayers: function () {
//Need our own special implementation as the LayerGroup one doesn't work for us
@@ -213,6 +148,20 @@ L.MarkerClusterGroup = L.FeatureGroup.extend({
return res;
},
+ zoomToShowLayer: function (layer, callback) {
+ layer.__parent.zoomToBounds();
+ setTimeout(function () {
+ if (layer._icon) {
+ callback();
+ } else {
+ layer.__parent.spiderfy();
+ setTimeout(function () {
+ callback();
+ }, L.DomUtil.TRANSITION ? 250 : 0); //TODO: This is hardcoded based on the time to spiderfy
+ }
+ }, L.DomUtil.TRANSITION ? 600 : 0); //TODO: This is hardcoded based on the leaflet time to zoom
+ },
+
//Overrides FeatureGroup.onAdd
onAdd: function (map) {
L.FeatureGroup.prototype.onAdd.call(this, map);
@@ -261,6 +210,72 @@ L.MarkerClusterGroup = L.FeatureGroup.extend({
L.FeatureGroup.prototype.onRemove.call(this, map);
},
+
+ //Remove the given object from the given array
+ _arraySplice: function (anArray, obj) {
+ for (var i = anArray.length - 1; i >= 0; i--) {
+ if (anArray[i] === obj) {
+ anArray.splice(i, 1);
+ return;
+ }
+ }
+ },
+
+ _removeLayer: function (marker, removeFromDistanceGrid) {
+ var gridClusters = this._gridClusters,
+ gridUnclustered = this._gridUnclustered,
+ map = this._map;
+
+ //Remove the marker from distance clusters it might be in
+ if (removeFromDistanceGrid) {
+ for (var z = this._maxZoom; z >= 0; z--) {
+ if (!gridUnclustered[z].removeObject(marker, map.project(marker.getLatLng(), z))) {
+ break;
+ }
+ }
+ }
+
+ //Work our way up the clusters removing them as we go if required
+ var cluster = marker.__parent,
+ markers = cluster._markers,
+ otherMarker;
+
+ //Remove the marker from the immediate parents marker list
+ this._arraySplice(markers, marker);
+
+ while (cluster) {
+ cluster._childCount--;
+
+ if (cluster._zoom < 0) {
+ //Top level, do nothing
+ break;
+ } else if (removeFromDistanceGrid && cluster._childCount <= 1) { //Cluster no longer required
+ //We need to push the other marker up to the parent
+ otherMarker = cluster._markers[0] === marker ? cluster._markers[1] : cluster._markers[0];
+
+ //Update distance grid
+ gridClusters[cluster._zoom].removeObject(cluster, map.project(cluster._cLatLng, cluster._zoom));
+ gridUnclustered[cluster._zoom].addObject(otherMarker, map.project(otherMarker.getLatLng(), cluster._zoom));
+
+ //Move otherMarker up to parent
+ this._arraySplice(cluster.__parent._childClusters, cluster);
+ cluster.__parent._markers.push(otherMarker);
+ otherMarker.__parent = cluster.__parent;
+
+ if (cluster._icon) {
+ //Cluster is currently on the map, need to put the marker on the map instead
+ L.FeatureGroup.prototype.removeLayer.call(this, cluster);
+ L.FeatureGroup.prototype.addLayer.call(this, otherMarker);
+ }
+ } else {
+ cluster._recalculateBounds();
+ cluster._updateIcon();
+ }
+
+ cluster = cluster.__parent;
+ }
+ },
+
//Overrides FeatureGroup._propagateEvent
_propagateEvent: function (e) {
if (e.target instanceof L.MarkerCluster) {
--
Alioth's /usr/local/bin/git-commit-notice on /srv/git.debian.org/git/pkg-javascript/leaflet-markercluster.git
More information about the Pkg-javascript-commits
mailing list