Skip to main content

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:

EventParamsDescriptionOutput
gm:drawstarteventFired when polygon drawing starts.map, shape
gm:createeventFired when a polygon is created.map, shape, feature
gm:drawupdateeventFired after a draft vertex is added or removed.shape, coordinates, map
gm:draw:rejectedeventFired 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:

  1. Clicking on the map to place the first vertex
  2. Moving the mouse to see a preview of the polygon segment
  3. Clicking to add additional vertices
  4. Closing the polygon by clicking on the first vertex

Live Draw Polygon Example​