More work towards _recursivelyAddLayer. Still some bugs

This commit is contained in:
danzel
2012-07-20 14:44:21 +12:00
parent cc677f0ce9
commit a39f71fad5
2 changed files with 41 additions and 16 deletions
+11 -1
View File
@@ -82,6 +82,7 @@ L.MarkerCluster = L.Marker.extend({
added = false; added = false;
if (!this._haveGeneratedChildClusters && this._canAcceptPosition(layer.getLatLng(), zoom)) { if (!this._haveGeneratedChildClusters && this._canAcceptPosition(layer.getLatLng(), zoom)) {
//Don't need to cluster it in as we haven't clustered
this._addChild(layer); this._addChild(layer);
added = true; added = true;
} else { } else {
@@ -97,9 +98,18 @@ L.MarkerCluster = L.Marker.extend({
} }
} }
//Couldn't add it to a child, but it should be part of us
if (!added && this._canAcceptPosition(layer.getLatLng(), zoom)) { if (!added && this._canAcceptPosition(layer.getLatLng(), zoom)) {
//Add to ourself instead //Add to ourself instead
this._addChild(layer); //TODO: This should be done in a clustering aware way var newCluster = this._group._clusterOne(this._markers, layer, zoom);
if (newCluster && newCluster._markers[0]._icon) {
//Remove children and add cluster
newCluster._recursivelyRemoveChildrenFromMap(newCluster._bounds, 0);
newCluster._addToMap();
}
this._addChild(newCluster || layer);
added = true; added = true;
} }
+30 -15
View File
@@ -176,6 +176,29 @@ L.MarkerClusterGroup = L.FeatureGroup.extend({
this._map.on('moveend', this._moveEnd, this); this._map.on('moveend', this._moveEnd, this);
}, },
//Takes a list of markers and clusters the new marker in to them
//Will return null or the new MarkerCluster. The clustered in marker is removed from the given array
_clusterOne: function (unclusteredMarkers, newMarker, zoom) {
var markerPos = newMarker._projCenter || this._map.project(newMarker.getLatLng(), zoom),
clusterRadiusSqrd = this.options.maxClusterRadius * this.options.maxClusterRadius,
i, m, mPos;
for (i = unclusteredMarkers.length - 1; i >= 0; i--) {
m = unclusteredMarkers[i];
mPos = m._projCenter || this._map.project(m.getLatLng(), zoom);
if (this._sqDist(markerPos, mPos) <= clusterRadiusSqrd) {
//Create a new cluster with these 2
var newCluster = new L.MarkerCluster(this, m, newMarker);
unclusteredMarkers.splice(i, 1);
return newCluster;
}
}
return null;
},
//Takes a list of objects that have a 'getLatLng()' function (Marker / MarkerCluster) //Takes a list of objects that have a 'getLatLng()' function (Marker / MarkerCluster)
//Performs clustering on them (using a greedy algorithm) and returns those clusters. //Performs clustering on them (using a greedy algorithm) and returns those clusters.
//toCluster: List of Markers/MarkerClusters to cluster. MarkerClusters MUST be first in the list //toCluster: List of Markers/MarkerClusters to cluster. MarkerClusters MUST be first in the list
@@ -208,21 +231,13 @@ L.MarkerClusterGroup = L.FeatureGroup.extend({
//otherwise, look through all of the markers we haven't managed to cluster and see if we should form a cluster with them //otherwise, look through all of the markers we haven't managed to cluster and see if we should form a cluster with them
if (!used) { if (!used) {
for (j = 0 ; j < unclustered.length; j++) { var newCluster = this._clusterOne(unclustered, point, hasChildClusters);
if (this._sqDist(point._projCenter, unclustered[j]._projCenter) <= clusterRadiusSqrd) { if (newCluster) {
//Create a new cluster with these 2 newCluster._haveGeneratedChildClusters = hasChildClusters;
var newCluster = new L.MarkerCluster(this, point, unclustered[j]); newCluster._projCenter = this._map.project(newCluster.getLatLng(), zoom);
newCluster._haveGeneratedChildClusters = hasChildClusters; clusters.push(newCluster);
clusters.push(newCluster); } else {
//Didn't manage to use it
newCluster._projCenter = this._map.project(newCluster.getLatLng(), zoom);
unclustered.splice(j, 1);
used = true;
break;
}
}
if (!used) {
unclustered.push(point); unclustered.push(point);
} }
} }