Subversion Repositories eFlore/Applications.moissonnage

Rev

Rev 43 | Details | Compare with Previous | Last modification | View Log | RSS feed

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