2

Is it possible to cluster polygons in Leaflet?

In a similar way to the Marker Cluster plugin

3

You would probably be interested in this post:

Feature Request - Ability to cluster polygons (Leaflet.markercluster issue #612)

You might be interested in Leaflet.Deflate plugin, but be careful when removing the marker (see #580).

Another possibility would be to "add" methods getLatLng and setLatLng to your polygons, so that MCG can handle them like points. But the result might not be like what you expect, as the polygons would cluster based on the position of their center (or whatever position you return through getLatLng), not on their shape.

[…]

Demo: http://jsfiddle.net/ve2huzxw/237/

Instead of using regular L.polygon, you would first create a custom class and use it to instantiate your shapes. In the below code, we customize L.rectangle, but you can do the same for other vectors.

// Create a new vector type with getLatLng and setLatLng methods.
// Of course you can do the same with other polygons.
L.RectangleClusterable = L.Rectangle.extend({
  _originalInitialize: L.Rectangle.prototype.initialize,

  initialize: function (bounds, options) {
    this._originalInitialize(bounds, options);
    this._latlng = this.getBounds().getCenter(); // Define the polygon "center".
  },

  getLatLng: function () {
    return this._latlng;
  },

  // dummy method.
  setLatLng: function () {}
});

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.