Geofencing (v2.1.x - Android)

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

CREATING GEOFENCE MANAGER

Sdk class contains a methoid getGeofenceManager 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:

  • GeofenceCircular

  • GeofencePolygonal

GEOFENCE EVENTS

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

  • void onGeofenceEnter(String id, String tag);

  • void onGeofenceExit(String id, String tag);

You can add listeners using addGeofenceListener method:

sdk.getGeofenceManager.addGeofenceListener(listener);

Don't forget to detach listeners using removeGeofenceListener method:

sdk.getGeofenceManager.removeGeofenceListener(listener);

EDITING GEOFENCES

For a circular geofence it’s possible to change its center and radius using setCenter(MapCoordinates center) and setRadius(double radius) methods respectively.

To modify a polygonal geofence use setPolygon(List<MapCoordinates> polygon) method.

To update a geofence when its corresponding editable GeoObject is modified (for example, when a user drags a GeoCircle to a different location), use GeoObjectListener.onGeoObjectChanged method:

private final Map<GeoObject, Geofence> geofences = new HashMap<>(); private class GeoObjectListener implements MapView.GeoObjectListener { ... @Override public void onGeoObjectChanged(GeoObject geoObject) { Geofence geofence = geofences.get(geoObject); if (geofence != null) { if (geofence instanceof GeofenceCircular) { GeoCircle geoCircle = ((GeoCircle) geoObject); ((GeofenceCircular) geofence).setCenter(geoCircle.getCenter()); ((GeofenceCircular) geofence).setRadius(geoCircle.getRadius()); } else if (geofence instanceof GeofencePolygonal) { GeoPolygon geoPolygon = ((GeoPolygon) geoObject); ((GeofencePolygonal) geofence).setPolygon(geoPolygon.getVertices()); } } } ... }

See Interacting with GeoObjects for details.