Subversion Repositories eFlore/Applications.moissonnage

Rev

Rev 37 | Rev 43 | Go to most recent revision | Details | Compare with Previous | Last modification | View Log | RSS feed

Rev Author Line No. Line
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
		}
42 alex 31
		if (this._markers.length == 0) {
32
			if (this._map != null) {
33
				this._map.getPanes().markerPane.removeChild(this._icon);
34
			}
37 alex 35
			delete this;
36
		}
42 alex 37
	},
38
 
39
	supprimer: function() {
40
		while (this._markers.length > 0) {
41
			this._markers.splice(0, 1);
42
		}
43
		if (this._map != null) {
44
			this._map.getPanes().markerPane.removeChild(this._icon);
45
		}
46
		delete this;
37 alex 47
	}
48
 
49
});
50
 
51
 
52
 
53
L.ClusterGroup = L.FeatureGroup.extend({
54
 
55
	initialize: function(layers) {
56
		L.FeatureGroup.prototype.initialize.call(this, layers);
57
		this._distance = 10;
58
		this._resolution = 0;
59
		this._nombreClusters = 0;
60
	},
61
 
62
	ajouterPoint: function(layer) {
63
		if (this._resolution == 0) {
64
			this._resolution = (2 * Math.PI * 6378137) / (256 * Math.pow(2, this._map.getZoom()));
65
		}
66
		var seuilCluster = this._distance * this._resolution;
67
		if (typeof(layer._latlng) != 'undefined') {
68
			var indexClusterPlusProche = this._chercherVoisinPlusProche(layer);
69
			var ajoute = false;
70
			if (indexClusterPlusProche != null) {
71
				var distance = this._layers[indexClusterPlusProche].getLatLng().distanceTo(layer.getLatLng());
72
				if (distance < seuilCluster) {
73
					this._layers[indexClusterPlusProche].ajouterMarker(layer);
74
					ajoute = true;
75
				}
76
			}
77
			if (!ajoute) {
78
				this._layers[this._nombreClusters] = layer;
79
				this._nombreClusters ++;
80
			}
81
		}
82
	},
83
 
84
	_chercherVoisinPlusProche: function(layer) {
85
		var distance = -1;
86
		var plusProche = null;
87
		for (var numeroLayer in this._layers) {
88
			if (typeof(this._layers[numeroLayer]) != 'undefined') {
89
				var centre = this._layers[numeroLayer].getLatLng();
90
				if (distance == -1 || centre.distanceTo(layer.getLatLng()) < distance) {
91
					plusProche = numeroLayer;
92
					distance = centre.distanceTo(layer.getLatLng());
93
				}
94
			}
95
		}
96
		return plusProche;
97
	},
98
 
99
	afficherClusters: function() {
100
		for (var numeroLayer in this._layers) {
101
			if (typeof(this._layers[numeroLayer]) != 'undefined') {
102
				var layer = this._layers[numeroLayer];
103
				if (layer.obtenirNombreMarkers() > 1) {
104
					layer.setIcon(new L.Icon({ iconUrl : clusterImageUrl, iconSize : [20, 20] }))
105
				}
106
				layer.addTo(map);
107
			}
108
		}
109
	},
110
 
111
	supprimerPoint: function(layer) {
42 alex 112
		layer.supprimer();
37 alex 113
	}
114
 
115
});