angular-ui / ui-leaflet

AngularJS directive to embed an interact with maps managed by Leaflet library
http://angular-ui.github.io/ui-leaflet
Other
314 stars 134 forks source link

Clustering GeoJson Points #30

Open nmccready opened 9 years ago

nmccready commented 9 years ago

From @vipervf1 on July 7, 2014 19:51

Is there an easy way to use the clustering plugin when using geojson points instead of markers?

Copied from original issue: tombatossals/angular-leaflet-directive#415

nmccready commented 9 years ago

From @mathiasmuller4sh on July 10, 2014 15:10

Hi, I'am working on the same problem.

I've made a little hack in the index.html and it seems to do the tricks.

EDIT: Now, need to add a listener to remove cluster on geojson layer remove

    <script src="lib/js/leaflet/dist/leaflet.js" type="text/javascript"></script>
    <script src="lib/js/leaflet.markercluster/dist/leaflet.markercluster.js" type="text/javascript"></script>
    <script>
        L.GeoJSON = L.GeoJSON.extend({
            addTo: function(map) {
                var markers = new L.MarkerClusterGroup({
                    spiderfyOnMaxZoom: false,
                    showCoverageOnHover: false,
                    zoomToBoundsOnClick: false,
                    disableClusteringAtZoom: 19
                });
                map.addLayer(markers);
                markers.addLayer(this);
                return this;
            }
        });
        L.geoJson = function (geojson, options) {
            return new L.GeoJSON(geojson, options);
        };
    </script>
nmccready commented 9 years ago

From @mathiasmuller4sh on July 11, 2014 10:28

Complete hack with remove listener. I use this code in my angular controller

        L.GeoJSON = L.GeoJSON.extend({
            addTo: function(map) {
                var self = this;
                map.addLayer(this.markers);
                var parentRemove = L.GeoJSON.prototype.onRemove;
                L.GeoJSON.prototype.onRemove = function (map) {
                    self.markers.removeLayer(self);
                    delete self.markers;
                    parentRemove(map);
                };
                return this;
            }
        });
        L.geoJson = function (geojson, options) {
            var geoJSON = new L.GeoJSON(geojson, options);
            var markers = new L.MarkerClusterGroup({
                spiderfyOnMaxZoom: false,
                showCoverageOnHover: true,
                zoomToBoundsOnClick: true,
                disableClusteringAtZoom: 18
            });
            markers.setGeoJSON = function(data) {
                geoJSON.setGeoJSON(data);
            };
            markers.addLayer(geoJSON);
            geoJSON.markers = markers;
            return  markers;
        };
nmccready commented 9 years ago

From @stefan2k on October 27, 2014 13:33

Nice workaround!

nmccready commented 9 years ago

From @briankostar on May 14, 2015 21:16

this saved me some time! thanks

nmccready commented 9 years ago

:+1:

nmccready commented 9 years ago

Someone want to work on flushing this out into the geojson directive?

nmccready commented 9 years ago

From @briankostar on May 14, 2015 21:36

I'm still learning, but I'll take a crack at it this weekend.

nmccready commented 9 years ago

From @mahmoudmy on June 17, 2015 15:1

Very useful, thanks!