Geofencing (v2.1.x - iOS)

The core component of Navmii SDK responsible for geofencing is NMGeofenceManager. This class provides an API for managing user geofences and transition event notifications.

 

CREATING GEOFENCE MANAGER

NMSdk class contains a property geofenceManager which allows Navmii SDK user to access geofence manager instance. The geofence manager instance can only be accessed if the SDK is started.

 

GEOFENCE TYPES

The geofence manager supports two types of geofence geometry:

  • NMGeofenceCircular

  • NMGeofencePolygonal

 

GEOFENCE EVENTS

The geofence manager provides a NMGeofenceListener interface to handle transition events:

  • - (void)onGeofenceEnter:(nonnull NSString *)geofenceId geofenceTag:(nullable NSString *)geofenceTag;

  • - (void)onGeofenceExit:(nonnull NSString *)geofenceId geofenceTag:(nullable NSString *)geofenceTag;

You can add listeners using addGeofenceListener method:

[sdk.geofenceManager addGeofenceListener:geofenceListener];

Don't forget to detach listeners using removeGeofenceListener method:

[sdk.geofenceManager removeGeofenceListener:geofenceListener];

 

EDITING GEOFENCES

For a circular geofence it’s possible to change its center and radius using center and radius properites respectively.

To modify a polygonal geofence use coordinates property.

To update a geofence when its corresponding editable NMGeoObject is modified (for example, when a user drags a NMGeoCircle to a different location), use NMGeoObjectListener onGeoObjectChanged: method:

- (void)onGeoObjectChanged:(NSUInteger)geoObjectID { NMGeoObject *geoObject = [self geoObjectForId:geoObjectID fromArray:geoObjects]; NMGeofence *geofence = [self geoGeofenceForGeoObject:geoObject fromArray:geofences]; if ([geoObject isKindOfClass:[NMGeoCircle class]] && [geofence isKindOfClass:[NMGeofenceCircular class]]) { NMGeoCircle *circle = (NMGeoCircle *)geoObject; NMGeofenceCircular *geofenceCircular = (NMGeofenceCircular *)geofence; geofenceCircular.radius = circle.radius; geofenceCircular.center = circle.center; } else if ([geoObject isKindOfClass:[NMGeoPolygon class]] && [geofence isKindOfClass:[NMGeofencePolygonal class]]) { NMGeoPolygon *polygon = (NMGeoPolygon *)geoObject; NMGeofencePolygonal *geofencePolygonal = (NMGeofencePolygonal *)geofence; geofencePolygonal.coordinates = [polygon.vertices copy]; } }