Skip to main content

Geofencing Helper Mode

The Geofencing Helper Mode validates every feature that is drawn or edited against a set of reference features and emits a violation event when a feature breaks the configured rules. Use it to keep geometry inside an allowed area (containment) or to keep it out of restricted zones (intersection).

Geofencing does not cancel the operation by itself — it reports violations through events so your application decides how to react (warn the user, revert the change, block saving, etc.).

You can enable Geofencing Helper Mode with the following methods:

map.gm.enableMode('helper', 'geofencing');
map.gm.disableMode('helper', 'geofencing');
map.gm.toggleMode('helper', 'geofencing');
map.gm.isModeEnabled('helper', 'geofencing');

Configuring the reference features

The geofencing helper tracks two independent groups of reference features:

GroupRuleViolation
containmentDrawn/edited features must stay inside at least one containment feature.containment_violation
intersectionDrawn/edited features must not intersect any intersection feature.intersection_violation

Reference features are registered on the helper instance, which is reachable through gm.actionInstances:

import type { FeatureData } from '@geoman-io/maplibre-geoman-pro';

// the geofencing helper must be enabled before you access it
map.gm.enableMode('helper', 'geofencing');

const geofencing = map.gm.actionInstances['helper__geofencing'];

// keep drawing/editing inside these features
geofencing.addFeatures('containment', allowedAreaFeatures);

// disallow overlapping these features
geofencing.addFeatures('intersection', restrictedZoneFeatures);

// later, remove individual reference features…
geofencing.removeFeatures('intersection', [someFeatureData]);

addFeatures / removeFeatures take an array of FeatureData (the wrapped features Geoman manages — see Working with the Features Instance). When the mode is disabled both groups are cleared.

If no containment features are registered, every feature passes the containment check. If no intersection features are registered, the intersection check never triggers.

Events for Geofencing Helper Mode

The following events are available on a map instance:

EventParamsDescriptionOutput
gm:globalgeofencingmodetoggledeventFired when Geofencing Helper Mode is toggled.enabled, map
gm:geofencing_violationeventFired when a drawn/edited feature breaks a rule.see below

The violation event carries the following fields:

interface GmGeofencingViolationEvent {
name: '_gm:helper:geofencing_violation';
mode: 'geofencing';
// which operation produced the feature under test
actionType: 'draw' | 'edit';
// the rule that was broken
action: 'containment_violation' | 'intersection_violation';
}

Example Usage

map.on('gm:geofencing_violation', (event) => {
if (event.action === 'containment_violation') {
console.warn('Feature left the allowed area');
} else if (event.action === 'intersection_violation') {
console.warn('Feature overlaps a restricted zone');
}
});

Behavior

The geofencing helper:

  1. Listens for the before_create (draw) and before_update (edit) feature events.
  2. Runs the new geometry through the containment and intersection checks.
  3. Fires a gm:geofencing_violation event for each rule that is broken.

Default Control Options

OptionDefault
title"Geofencing"
iconbuilt-in geofencing icon
uiEnabledtrue
activefalse

Live Geofencing Example

Draw or edit features with the geofencing helper enabled — register containment/intersection reference features (see above) and listen for gm:geofencing_violation to react.

Building something complex? Our team can help with architecture and integration. Get in touch →