Subversion Repositories eFlore/Applications.moissonnage

Rev

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