Subversion Repositories eFlore/Applications.moissonnage

Rev

Rev 42 | Go to most recent revision | View as "text/plain" | Blame | Compare with Previous | Last modification | View Log | RSS feed



L.Cluster = L.Marker.extend({
        
        initialize: function(marker, proprietes, options) {
                L.Marker.prototype.initialize.call(this, marker, options);
                this._proprietes = [proprietes];
        },
        
        ajouterPoint: function(proprietes) {
                this._proprietes.push(proprietes);
                this.setIcon(new L.Icon({ iconUrl : clusterImageUrl, iconSize : [20, 20] }));
        },
        
        recupererPoints: function() {
                return this._proprietes;
        },
        
        obtenirNombrePoints: function() {
                return this._proprietes.length;
        },
        
        supprimerSource: function(sourceSupression) {
                var index = 0;
                while (index < this._markers.length) {
                        if (this._proprietes[index].source == sourceSupression) {
                                this._proprietes.splice(index, 1);
                        } else {
                                index ++;
                        }
                }
                if (this._markers.length == 0) {
                        if (this._map != null) {
                                this._map.removeLayer(this);
                        }
                        delete this;
                }
        },
        
        supprimer: function() {
                while (this._proprietes.length > 0) {
                        this._proprietes.splice(0, 1);
                }
                if (this._map != null) {
                        this._map.removeLayer(this);
                }
                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(point, proprietes) {
                if (this._resolution == 0) {
                        this._resolution = (2 * Math.PI * 6378137) / (256 * Math.pow(2, this._map.getZoom()));
                }
                var seuilCluster = this._distance * this._resolution;
                var indexClusterPlusProche = this._chercherVoisinPlusProche(point);
                var ajoute = false;
                if (indexClusterPlusProche != null) {
                        var distance = this._layers[indexClusterPlusProche].getLatLng().distanceTo(point);
                        if (distance < seuilCluster) {
                                this._layers[indexClusterPlusProche].ajouterPoint(point, proprietes);
                                ajoute = true;
                        }
                }
                if (!ajoute) {
                        var cluster = creerMarqueur(point, proprietes);
                        this._layers[this._nombreClusters] = cluster;
                        this._nombreClusters ++;
                }
        },
        
        _chercherVoisinPlusProche: function(point) {
                var distance = -1;
                var plusProche = null;
                for (var numeroLayer in this._layers) {
                        if (typeof(this._layers[numeroLayer]) != 'undefined') {
                                var pointCluster = this._layers[numeroLayer].getLatLng();
                                if (distance == -1 || pointCluster.distanceTo(point) < distance) {
                                        plusProche = numeroLayer;
                                        distance = pointCluster.distanceTo(point);
                                }
                        }
                }
                return plusProche;
        },
        
        afficherClusters: function() {
                for (var numeroLayer in this._layers) {
                        if (typeof(this._layers[numeroLayer]) != 'undefined') {
                                var layer = this._layers[numeroLayer];
                                if (layer.obtenirNombrePoints() > 1) {
                                        layer.setIcon(new L.Icon({ iconUrl : clusterImageUrl, iconSize : [20, 20] }))
                                }
                                layer.addTo(map);
                        }
                }
        },
        
        supprimerPoint: function(layer) {
                this.removeLayer(layer);
        }
        
});