Areas alarms
Trigger operations when users enter or exit defined geographic areas using the built-in AlarmService class.
Add areas to monitor
Define geographic areas and invoke the monitorArea method on your AlarmService instance. You can monitor three types: RectangleGeographicArea, CircleGeographicArea, and PolygonGeographicArea.
RectangleGeographicArea rect = RectangleGeographicArea(
{ 40.71713, -74.00396 }, // top-left
{ 40.71357, -73.99925 } // bottom-right
);
CircleGeographicArea circle = CircleGeographicArea(
{ 40.77430, -73.97085 }, // center
100 // radius in meters
);
PolygonGeographicArea polygon = PolygonGeographicArea({
{ 40.77158, -73.97833 }, // point 1
{ 40.76882, -73.98061 }, // point 2
{ 40.76659, -73.97666 }, // point 3
{ 40.76945, -73.97441 } // point 4
});
alarmService->monitorArea(rect, "areaRect");
alarmService->monitorArea(circle, "areaCircle");
alarmService->monitorArea(polygon, "areaPolygon");
Assign a unique identifier to each area. This lets you determine which zone a user has entered or exited.
Get monitored areas
Access active geofences via the getMonitoredAreas method. It returns a list of AlarmMonitoredArea objects containing the parameters you provided to monitorArea.
List<AlarmMonitoredArea> monitorAreas = alarmService->getMonitoredAreas();
for (auto monitorArea : monitorAreas)
{
GeographicArea area = monitorArea.area;
String id = monitorArea.id;
}
When defining a PolygonGeographicArea, always "close" the shape by making the first and last coordinates identical. Otherwise, the SDK may return polygons that don't match the one you provided.
Unmonitor an area
Remove a monitored area by calling the unmonitorArea method with the same GeographicArea instance you provided to monitorArea.
RectangleGeographicArea rect = RectangleGeographicArea(
{ 40.71713, -74.00396 }, // top-left
{ 40.71357, -73.99925 } // bottom-right
);
alarmService->monitorArea(rect);
alarmService->unmonitorArea(rect);
You can also use the unmonitorAreas method by passing a list of IDs:
alarmService->unmonitorAreas({"firstIdToUnmonitor", "secondIdToUnmonitor"});
Get notified when users enter or exit areas
Attach an AlarmListener with the onBoundaryCrossed callback to your AlarmService. This callback returns two arrays: entered area IDs and exited area IDs.
class MyAlarmListener : public IAlarmListener
{
void onBoundaryCrossed( const AlarmMonitoredAreaList &enteredAreas, const AlarmMonitoredAreaList &exitedAreas ) override
{
for (auto area : enteredAreas)
{
GEM_INFO_LOG("ENTERED AREA: %s", area.id);
}
for (auto area : exitedAreas)
{
GEM_INFO_LOG("EXITED AREA: %s", area.id);
}
}
};
auto myListener = StrongPointerFactory<MyAlarmListener>();
alarmService = AlarmService::produce(myListener);
Get user location areas
Retrieve zones the user is currently inside by calling the getInsideAreas method:
auto insideAreas = alarmService->getInsideAreas();
For the insideAreas getter to return a non-empty list, the user must be inside at least one monitored area and must move or change position within that area.
To retrieve exited zones, call the getOutsideAreas method.