Subversion Repositories eFlore/Applications.moissonnage

Rev

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

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