| 37 |
alex |
1 |
|
|
|
2 |
|
|
|
3 |
L.Cluster = L.Marker.extend({
|
|
|
4 |
|
|
|
5 |
initialize: function(marker, options) {
|
|
|
6 |
L.Marker.prototype.initialize.call(this, marker, options);
|
|
|
7 |
this._markers = [this];
|
|
|
8 |
},
|
|
|
9 |
|
|
|
10 |
ajouterMarker: function(marker) {
|
|
|
11 |
this._markers.push(marker);
|
|
|
12 |
},
|
|
|
13 |
|
|
|
14 |
recupererMarkers: function() {
|
|
|
15 |
return this._markers;
|
|
|
16 |
},
|
|
|
17 |
|
|
|
18 |
obtenirNombreMarkers: function() {
|
|
|
19 |
return this._markers.length;
|
|
|
20 |
},
|
|
|
21 |
|
|
|
22 |
supprimerSource: function(sourceSupression) {
|
|
|
23 |
var index = 0;
|
|
|
24 |
while (index < this._markers.length) {
|
|
|
25 |
if (this._markers[index].source == sourceSupression) {
|
|
|
26 |
this._markers.splice(index, 1);
|
|
|
27 |
} else {
|
|
|
28 |
index ++;
|
|
|
29 |
}
|
|
|
30 |
}
|
|
|
31 |
if (this._markers.length == 0 && this._map != null) {
|
|
|
32 |
this._map.removeLayer(this);
|
|
|
33 |
delete this;
|
|
|
34 |
}
|
|
|
35 |
}
|
|
|
36 |
|
|
|
37 |
});
|
|
|
38 |
|
|
|
39 |
|
|
|
40 |
|
|
|
41 |
L.ClusterGroup = L.FeatureGroup.extend({
|
|
|
42 |
|
|
|
43 |
initialize: function(layers) {
|
|
|
44 |
L.FeatureGroup.prototype.initialize.call(this, layers);
|
|
|
45 |
this._distance = 10;
|
|
|
46 |
this._resolution = 0;
|
|
|
47 |
this._nombreClusters = 0;
|
|
|
48 |
},
|
|
|
49 |
|
|
|
50 |
ajouterPoint: function(layer) {
|
|
|
51 |
if (this._resolution == 0) {
|
|
|
52 |
this._resolution = (2 * Math.PI * 6378137) / (256 * Math.pow(2, this._map.getZoom()));
|
|
|
53 |
}
|
|
|
54 |
var seuilCluster = this._distance * this._resolution;
|
|
|
55 |
if (typeof(layer._latlng) != 'undefined') {
|
|
|
56 |
var indexClusterPlusProche = this._chercherVoisinPlusProche(layer);
|
|
|
57 |
var ajoute = false;
|
|
|
58 |
if (indexClusterPlusProche != null) {
|
|
|
59 |
var distance = this._layers[indexClusterPlusProche].getLatLng().distanceTo(layer.getLatLng());
|
|
|
60 |
if (distance < seuilCluster) {
|
|
|
61 |
this._layers[indexClusterPlusProche].ajouterMarker(layer);
|
|
|
62 |
ajoute = true;
|
|
|
63 |
}
|
|
|
64 |
}
|
|
|
65 |
if (!ajoute) {
|
|
|
66 |
this._layers[this._nombreClusters] = layer;
|
|
|
67 |
this._nombreClusters ++;
|
|
|
68 |
}
|
|
|
69 |
}
|
|
|
70 |
},
|
|
|
71 |
|
|
|
72 |
_chercherVoisinPlusProche: function(layer) {
|
|
|
73 |
var distance = -1;
|
|
|
74 |
var plusProche = null;
|
|
|
75 |
for (var numeroLayer in this._layers) {
|
|
|
76 |
if (typeof(this._layers[numeroLayer]) != 'undefined') {
|
|
|
77 |
var centre = this._layers[numeroLayer].getLatLng();
|
|
|
78 |
if (distance == -1 || centre.distanceTo(layer.getLatLng()) < distance) {
|
|
|
79 |
plusProche = numeroLayer;
|
|
|
80 |
distance = centre.distanceTo(layer.getLatLng());
|
|
|
81 |
}
|
|
|
82 |
}
|
|
|
83 |
}
|
|
|
84 |
return plusProche;
|
|
|
85 |
},
|
|
|
86 |
|
|
|
87 |
afficherClusters: function() {
|
|
|
88 |
for (var numeroLayer in this._layers) {
|
|
|
89 |
if (typeof(this._layers[numeroLayer]) != 'undefined') {
|
|
|
90 |
var layer = this._layers[numeroLayer];
|
|
|
91 |
if (layer.obtenirNombreMarkers() > 1) {
|
|
|
92 |
layer.setIcon(new L.Icon({ iconUrl : clusterImageUrl, iconSize : [20, 20] }))
|
|
|
93 |
}
|
|
|
94 |
layer.addTo(map);
|
|
|
95 |
}
|
|
|
96 |
}
|
|
|
97 |
},
|
|
|
98 |
|
|
|
99 |
supprimerPoint: function(layer) {
|
|
|
100 |
this.removeLayer(layer);
|
|
|
101 |
delete layer;
|
|
|
102 |
}
|
|
|
103 |
|
|
|
104 |
});
|