L.Cluster = L.Marker.extend({ initialize: function(marker, options) { L.Marker.prototype.initialize.call(this, marker, options); this._markers = [this]; }, ajouterMarker: function(marker) { this._markers.push(marker); }, recupererMarkers: function() { return this._markers; }, obtenirNombreMarkers: function() { return this._markers.length; }, supprimerSource: function(sourceSupression) { var index = 0; while (index < this._markers.length) { if (this._markers[index].source == sourceSupression) { this._markers.splice(index, 1); } else { index ++; } } if (this._markers.length == 0) { if (this._map != null) { this._map.removeLayer(this); //this._map.getPanes().markerPane.removeChild(this._icon); } delete this; } }, supprimer: function() { while (this._markers.length > 0) { this._markers.splice(0, 1); } if (this._map != null) { this._map.removeLayer(this); //this._map.getPanes().markerPane.removeChild(this._icon); } delete this; } }); L.ClusterGroup = L.FeatureGroup.extend({ initialize: function(layers) { L.FeatureGroup.prototype.initialize.call(this, layers); this._distance = 10; this._resolution = 0; this._nombreClusters = 0; }, ajouterPoint: function(layer) { if (this._resolution == 0) { this._resolution = (2 * Math.PI * 6378137) / (256 * Math.pow(2, this._map.getZoom())); } var seuilCluster = this._distance * this._resolution; if (typeof(layer._latlng) != 'undefined') { var indexClusterPlusProche = this._chercherVoisinPlusProche(layer); var ajoute = false; if (indexClusterPlusProche != null) { var distance = this._layers[indexClusterPlusProche].getLatLng().distanceTo(layer.getLatLng()); if (distance < seuilCluster) { this._layers[indexClusterPlusProche].ajouterMarker(layer); ajoute = true; } } if (!ajoute) { this._layers[this._nombreClusters] = layer; this._nombreClusters ++; } } }, _chercherVoisinPlusProche: function(layer) { var distance = -1; var plusProche = null; for (var numeroLayer in this._layers) { if (typeof(this._layers[numeroLayer]) != 'undefined') { var centre = this._layers[numeroLayer].getLatLng(); if (distance == -1 || centre.distanceTo(layer.getLatLng()) < distance) { plusProche = numeroLayer; distance = centre.distanceTo(layer.getLatLng()); } } } return plusProche; }, afficherClusters: function() { for (var numeroLayer in this._layers) { if (typeof(this._layers[numeroLayer]) != 'undefined') { var layer = this._layers[numeroLayer]; if (layer.obtenirNombreMarkers() > 1) { layer.setIcon(new L.Icon({ iconUrl : clusterImageUrl, iconSize : [20, 20] })) } layer.addTo(map); } } }, supprimerPoint: function(layer) { layer.supprimer(); } });