[Pkg-javascript-commits] [leaflet-markercluster] 08/479: Working towards removing markers from a MarkerClusterGroup. Currently works for markers that are not in a cluster or those that are that are in clusters of size >= 2.
Jonas Smedegaard
dr at jones.dk
Thu Oct 16 16:00:03 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 4df7bdc9c2f6b67386403b4a3b42dbc3d6f70004
Author: danzel <danzel at localhost.geek.nz>
Date: Thu Jul 12 15:20:29 2012 +1200
Working towards removing markers from a MarkerClusterGroup. Currently works for markers that are not in a cluster or those that are that are in clusters of size >= 2.
---
example/marker-clustering.html | 18 +++++++++++++-----
src/MarkerCluster.js | 39 +++++++++++++++++++++++++++++++++++++++
src/MarkerClusterGroup.js | 36 ++++++++++++++++++++++++++++++++++++
3 files changed, 88 insertions(+), 5 deletions(-)
diff --git a/example/marker-clustering.html b/example/marker-clustering.html
index 63bfbf7..e7b9a66 100644
--- a/example/marker-clustering.html
+++ b/example/marker-clustering.html
@@ -58,7 +58,8 @@
<body>
<div id="map"></div>
- <button id="populate">Populate with 10 markers</button>
+ <button id="populate">Populate 1 marker</button>
+ <button id="remove">Remove 1 marker</button>
<script type="text/javascript">
@@ -70,10 +71,13 @@
var map = new L.Map('map', {center: latlng, zoom: 15, layers: [cloudmade]});
var markers = new L.MarkerClusterGroup();
+ var markersList = [];
function populate() {
for (var i = 0; i < 100; i++) {
- markers.addLayer(new L.Marker(getRandomLatLng(map)));
+ var m = new L.Marker(getRandomLatLng(map));
+ markersList.push(m);
+ markers.addLayer(m);
}
return false;
}
@@ -115,11 +119,15 @@
northEast = bounds.getNorthEast(),
lngSpan = northEast.lng - southWest.lng,
latSpan = northEast.lat - southWest.lat;
-
- markers.addLayer(new L.Marker(new L.LatLng(
+var m = new L.Marker(new L.LatLng(
southWest.lat + latSpan * 0.5,
- southWest.lng + lngSpan * 0.5)));
+ southWest.lng + lngSpan * 0.5));
+markersList.push(m);
+ markers.addLayer(m);
};
+ L.DomUtil.get('remove').onclick = function () {
+ markers.removeLayer(markersList.pop());
+ }
</script>
</body>
</html>
diff --git a/src/MarkerCluster.js b/src/MarkerCluster.js
index 72b35c0..9a34e86 100644
--- a/src/MarkerCluster.js
+++ b/src/MarkerCluster.js
@@ -69,6 +69,45 @@ L.MarkerCluster = L.Marker.extend({
L.FeatureGroup.prototype.addLayer.call(this._group, this);
},
+ //Removes the given node from this marker cluster (or its child as required)
+ //Returns true if it (or its childCluster) removes the marker
+ _recursivelyRemoveChildMarker: function(layer) {
+ var markers = this._markers,
+ childClusters = this._childClusters,
+ newChildCount = 0,
+ i;
+
+ //Check our children
+ for (i = markers.length - 1; i >= 0; i--) {
+ if (markers[i] == layer) {
+ markers.splice(i, 1);
+ //TODO? Recalculate bounds
+
+ this._childCount--;
+ if (this._icon) {
+ this.setIcon(this._group.options.iconCreateFunction(this._childCount));
+ }
+ return true;
+ }
+ }
+
+ //Otherwise check our childClusters
+ for (i = childClusters.length - 1; i >= 0; i--) {
+ if (childClusters[i]._recursivelyRemoveChildMarker(layer)) {
+ this._childCount--;
+ //TODO: If child is now 1 then remove it and add a marker
+ //TODO? Recalculate bounds
+
+ if (this._icon) {
+ this.setIcon(this._group.options.iconCreateFunction(this._childCount));
+ }
+ return true;
+ }
+ }
+
+ return false;
+ },
+
_recursivelyAnimateChildrenIn: function (center, depth) {
var markers = this._markers,
markersLength = markers.length,
diff --git a/src/MarkerClusterGroup.js b/src/MarkerClusterGroup.js
index 0a846d4..4f94c17 100644
--- a/src/MarkerClusterGroup.js
+++ b/src/MarkerClusterGroup.js
@@ -181,6 +181,7 @@ L.MarkerClusterGroup = L.FeatureGroup.extend({
current = this._markersAndClustersAtZoom[zoom],
newClusters = this._cluster(current.clusters, current.unclustered, [layer], zoom);
+ //TODO: At the moment we blow away all other zoom levels, but really we could probably get away with not doing that
this._highestZoom = this._zoom = zoom;
this._markersAndClustersAtZoom = {};
this._markersAndClustersAtZoom[zoom] = newClusters;
@@ -211,6 +212,41 @@ L.MarkerClusterGroup = L.FeatureGroup.extend({
},
removeLayer: function (layer) {
+ var current = this._markersAndClustersAtZoom[this._map._zoom],
+ i = current.unclustered.indexOf(layer),
+ cluster, result;
+
+ //Remove the marker
+ L.FeatureGroup.prototype.removeLayer.call(this, layer);
+
+ if (i !== -1) //Is unclustered at the current zoom level
+ {
+ current.unclustered.splice(i, 1);
+
+ //blow away all parent levels as they are now wrong
+ for (i in this._markersAndClustersAtZoom)
+ {
+ if (i > this._map._zoom) {
+ delete this._markersAndClustersAtZoom[i];
+ }
+ }
+ //TODO: This could probably use something like below and then remove the marker from the map
+ }
+ else //it is a child of a cluster
+ {
+ //Find the cluster
+ for (i = current.clusters.length - 1; i >= 0; i--) {
+ var c = current.clusters[i];
+
+ if (c._recursivelyRemoveChildMarker(layer)) {
+ if (c._childCount == 1) {
+ //TODO remove cluster and add individual marker
+ }
+ break;
+ }
+ };
+ }
+
//TODO Hrm....
//Will need to got through each cluster and find the marker, removing it as required, possibly turning its parent from a cluster into an individual marker.
--
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