Draw Polygon Mode
Polygon Mode allows you to create polygons on the map by clicking to add vertices and closing the shape.
You can enable Draw Polygon Mode like this:
map.gm.enableDraw('polygon');
map.gm.disableDraw();
map.gm.toggleDraw('polygon');
map.gm.drawEnabled('polygon');
// Or like this:
map.gm.enableMode('draw', 'polygon');
map.gm.disableMode('draw', 'polygon');
map.gm.toggleMode('draw', 'polygon');
map.gm.isModeEnabled('draw', 'polygon');
The following events are available on a map instance:
| Event | Params | Description | Output |
|---|---|---|---|
gm:drawstart | event | Fired when polygon drawing starts. | map, shape |
gm:create | event | Fired when a polygon is created. | map, shape, feature |
gm:drawupdate | event | Fired after a draft vertex is added or removed. | shape, coordinates, map |
gm:draw:rejected | event | Fired when the ring is refused. Drawing stays open. | mode, shape, reason, map |
Options
const gm = new Geoman(map, {
settings: {
// 'split' (default) unkinks a self-crossing ring into a MultiPolygon.
// 'reject' fires gm:draw:rejected and leaves drawing open.
polygonSelfIntersection: 'reject',
// Store a single-part result as a Polygon instead of a one-part MultiPolygon.
polygonAsPolygon: true,
},
});
Three exactly collinear clicks do not create a polygon. A thin triangle that is not collinear in longitude/latitude is still accepted. Closing the ring calls settings.beforeChange once, with the geometry that would be created.
Behavior
Polygon drawing operates by:
- Clicking on the map to place the first vertex
- Moving the mouse to see a preview of the polygon segment
- Clicking to add additional vertices
- Closing the polygon by clicking on the first vertex