flutter_map Docs
Project Links💝 Support Us
v8
v8
  • flutter_map
  • Why & How
    • 🌟Showcase & Case Studies
    • ❔How Does It Work?
      • Raster vs Vector Tiles
    • 👀Demo & Examples
  • Getting Started
    • 🚀What's New In v8?
    • Installation
  • Usage
    • Base Widget
      • Unbounded Horizontal Scrolling
    • Options
      • Interaction Options
      • Custom CRSs
    • Layers
    • Programmatic Interaction
      • Controllers & Cameras
      • External Custom Controllers
      • Listen To Events
    • Full API Reference
  • Layers
    • Tile Layer
      • Tile Providers
      • Caching
    • Marker Layer
    • Polygon Layer
    • Polyline Layer
    • Circle Layer
    • Overlay Image Layer
    • Attribution Layer
    • Layer Interactivity
      • Hit Testing Behaviour
  • Tile Servers
    • Using OpenStreetMap (direct)
    • Using Mapbox
    • Using Google Maps
    • Using Tracestrack
    • Using Thunderforest
    • Using Stadia Maps
    • Using Lima Labs
    • Using Bing Maps
    • Offline Mapping
    • Other Options
  • Plugins
    • Plugins List
    • Creating Plugins
      • Tile Providers
      • Layers
      • Caching Providers
  • Thanks
    • 💝Supporters
    • ✏️Credits & Contributing
Powered by GitBook

© flutter_map Authors & Maintainers

On this page
  • Interactivity
  • Multi-Worlds
  • Performance Optimizations
  • Painter Fill Method
  • Inverted Filling
  • Polygon Manipulation

Was this helpful?

Edit on GitHub
Export as PDF
  1. Layers

Polygon Layer

PreviousMarker LayerNextPolyline Layer

Last updated 20 days ago

Was this helpful?

You can add areas/shapes formed from coordinates to maps using PolygonLayer and Polygons.

PolygonLayer(
  polygons: [
    Polygon(
      points: [LatLng(30, 40), LatLng(20, 50), LatLng(25, 45)],
      color: Colors.blue,
    ),
  ],
),

Interactivity

PolygonLayers and Polygonss support hit detection and interactivity.

Multi-Worlds

The PolygonLayer paints its Polygons across all visible worlds by default. This can be changed.

Performance Optimizations

flutter_map includes many performance optimizations built in, especially as of v7. Some are enabled by default, but may be only 'weakly' applied, and others must be enabled manually. There are also some other actions that can be taken externally to improve performance

The following list is ordered from least 'intrusive'/extreme, to most intrusive. Remember to consider the potential risks of enabling an optimization before doing so.

The example application includes a stress test which loads multiple Polygons from an optimized format, with a total of 138,000 points.

Culling (enabled by default)

To improve performance, polygons that are entirely offscreen are effectively removed - they are not processed or painted/rendered. This is enabled by default, and may be disabled using the polygonCulling parameter, although this is not recommended.

Batching (enabled by default, but improvable with effort)

Overlapping colors that are not completely opaque will not receive the 'darkening'/layering effect - the overlapping area will just be the single colour.

To improve performance, polygons that are similar in appearance, and borders that are similar in appearance, are drawn to the underlying canvas in batches, to reduce the number of draw calls. This cannot be disabled.

To further improve performance, consider defining all Polygon points in a clockwise order, and place similar appearance Polygons adjacent to each other in the polygons list (where elevation does not matter).

Simplification (enabled by default, adjustable)

On layers with (many) only small polygons (those with few points), disabling simplification may yield better performance.

To adjust the quality and performance of the simplification, the maximum distance between removable points can be adjusted through the simplificationTolerance parameter. Increasing this value (from its default of 0.5) results in a more jagged, less accurate (lower quality) simplification, with improved performance; and vice versa. Many applications use a value in the range 1 - 1.5. To disable simplification, set simplificationTolerance to 0.


Simplification algorithms reduce the number of points in each line by removing unnecessary points that are 'too close' to other points which create tiny line segments invisible to the eye. This reduces the number of draw calls and strain on the raster/render thread. This should have minimal negative visual impact (high quality), but should drastically improve performance.

For this reason, polygons can be more simplified at lower zoom levels (more zoomed out) and less simplified at higher zoom levels (more zoomed in), where the effect of culling on performance improves and trades-off. This is done by scaling the simplificationTolerance parameter (see below) automatically internally based on the zoom level.

Painter Fill Method

See Painter Fill Method for more information. evenOdd is more performant, but at the expense of potential visual glitches.

Performant Rendering, with drawVertices (disabled by default)

Self-intersecting (complex) Polygons are not supported by the triangulation algorithm, and could cause errors.

Holes are supported.

This pathway may be slower than the standard pathway, especially when used on a large scale but with simplification disabled, or used on an especially small scale.

It is intended for use when prior profiling indicates more performance is required after other methods are already in use.

Rarely, some visible artefacts may be introduced by the triangulation algorithm.

Polygons (and similar other features) are usually drawn directly onto a Canvas, using built-in methods such as drawPolygon and drawLine. However, these can be relatively slow, and will slow the raster thread when used at a large scale.

Therefore, to improve performance, it's possible to optionally set the useAltRendering flag on the PolygonLayer. This will use an alternative, specialised, rendering pathway, which may lead to an overall performance improvement, particularly at a large scale.


There's two main steps to this alternative rendering algorithm:

Use No/Thin Borders or Cheaper StrokeCaps/StrokeJoins (external)

To further improve performance, consider using no border, or a hairline 1px border (remembering to consider the difference between device and logical pixels). Alternatively, consider using StrokeCap.butt/StrokeCap.square & StrokeJoin.miter/StrokeJoin.bevel. These are much cheaper for the rendering engine (particularly Skia), as it does not have to perform as many calculations.

Painter Fill Method

This section contains references to as-of-yet unreleased versions.

The default renderer supports two painter fill methods using different Flutter APIs. These fill methods change the way Flutter decides what is considered within a polygon (and should be filled), and what is outside. This can change the way particularly intersections and overlaps appear visually.

This can be set using the painterFillMethod property and the PolygonPainterFillMethod enum.

Many apps will not need to change from the default method. Before changing the method, profile and test for visual glitches thouroughly.

This gives the correct/intended/best visual results on native platforms. It's the default on native (non-web) platforms.

Additionally, it has slightly worse performance (especially at scale) than Even/Odd. The hit to performance is unlikely to be significant or even noticeable in many applications, but applications drawing many polygons may see a slow of about 2ms (as tested in the example app's stress test).

This gives the best performance, and works on the web. This is the default when targeting the web.

However, it yields unintended results in certain edge cases when polygons intersect when Inverted Filling is used, or when polygon holes can intersect with other holes.

Inverted Filling

This section contains references to as-of-yet unreleased versions.

On the web, inverted filling may not work as expected in some cases. It will not match the behaviour seen on native platforms.

Avoid allowing polygons to intersect, and avoid using holes within polygons. See the second image below for a demonstration of the issues.

Inverted filling (invertedFill) allows a color to be applied to all parts of the map outside a polygon. Transparently filled polygons will reveal the layers beneath without the inverted fill color.

Polygon Manipulation

'flutter_map' doesn't provide any public methods to manipulate polygons, as these would be deemed out of scope.

To improve performance, polygon outlines (points) are 'simplified' before the polygons are culled and painted/rendered. The well-known is used to perform this, and is enabled by default.

Cut each Polygon into multiple triangles through a process known as . flutter_map uses an earcutting algorithm through (a port of an algorithm initially developed at Mapbox intended for super-large scale triangulation).

Draw each triangle onto the canvas via the lower-level, faster method. Borders are then drawn as normal.

The PolygonPainterFillMethod.pathCombine option uses lesser-used Flutter APIs: the constructor. This allows for more intricate s, such as difference and union.

However, on the web, it causes major visual glitches due to a .

The PolygonPainterFillMethod.evenOdd option uses more simple Flutter APIs: the .evenOdd .

This is due to multiple limitations/bugs within Flutter. See and for the problematic bug reports.

However, some useful methods can be found in libraries such as 'latlong2' and . These can be applied to the input of Polygon's points argument, and the map will do it's best to try to render them. However, more complex polygons - such as those with holes - may be painted inaccurately, and may therefore require manual adjustment (of holePointsList, for example).

Layer Interactivity
Unbounded Horizontal Scrolling
Ramer–Douglas–Peucker algorithm
triangulation
dart_earcut
drawVertices
Path.combine
PathOperation
Flutter issue
PathFillType
rule
https://github.com/flutter/flutter/issues/124675
https://github.com/flutter/flutter/issues/149743
'poly_bool_dart'
PolygonLayer class - flutter_map library - Dart API
Logo
Polygon class - flutter_map library - Dart API
Logo
A variety of Polygons including labels, holes, mixed colors & opacities, and dotted borders
An example of a visual defect caused by using the .evenOdd setting
Inverted filling working correctly (native)
Inverted filling broken on web