OpenLayers – How to Get the Bigger Polygon of a Multipolygon

geometrymulti-polygonopenlayerspolygon

I have a Multipolygon that has 5 separate polygons and I want to get only the geometry of the biggest polygon.

How can I get the largest polygon from the multipolygon?

Best Answer

If you sort the polygons in descending size order it will be the first entry in the array

multipolygon.getPolygons().sort(function(polygon1, polygon2) {return polygon2.getArea() - polygon1.getArea();})[0]