Subversion Repositories eFlore/Applications.moissonnage

Rev

Rev 37 | Rev 43 | Go to most recent revision | Only display areas with differences | Ignore whitespace | Details | Blame | Last modification | View Log | RSS feed

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