Offline Geofencing with ArcGIS Runtime

10-10-2017 07:51 AM
Labels (1)
Esri Frequent Contributor
1 0 1,012

Geofencing is a concept in location aware apps that is used to determine if your current location is approaching, entering, dwelling, or leaving a point of interest (POI). For example, you may want to alert drivers of upcoming road closures, home buyers of newly available houses nearby, or tourists of an attraction they are approaching. This can be done with ease using ArcGIS Runtime's GeometryEngine. The GeometryEngine provides several static functions that allow you to ask questions about the spatial relationship of 2 or more geometries. For example, some questions you can ask are "does geometry 1 intersect geometry 2", "does geometry 1 completely contain geometry 2", and "how many meters is it from geometry 1 to geometry 2". The great thing about the GeometryEngine is that this works completely client side without any network connectivity, which means there is no internet usage, and thus limited battery consumption. By using the GeometryEngine, you can perform Geofencing with the following workflow:

  • Obtain your device's current location from the location display, and project it to the map's spatial reference
connect(m_mapView->locationDisplay(), &LocationDisplay::locationChanged, this, [this](Location location)
  Point currentLocation = GeometryEngine::project(location.position(), SpatialReference::webMercator());
  • Obtain a POI polygon. This could be created programatically and displayed as a Graphic, or it could be stored in a FeatureTable and displayed as a FeatureLayer
  • Once you have access to the Geometry object, perform the GeometryEngine function. The following code assumes you have created a polygon Graphic named m_graphic. 
    m_within = GeometryEngine::intersects(currentLocation, m_graphic->geometry());
  • If the GeometryEngine function returns true, alert the user. 
    if (m_within)
  • From here, you can take it further, and keep track of if the previous location was within the POI or not, which will tell you if you are entering, dwelling, or leaving the POI. You could also use the GeometryEngine::distance function to send an alert when you get near the POI.

A basic sample of this workflow can be found on GitHub. Give it a try, and see if you can enhance it to fit your organization's needs and use cases.

About the Author
I'm a Geographer working in Product Development at Esri, focusing my time on the ArcGIS Runtime SDKs. I'm an Esri Certified ArcGIS Desktop Professional (10 years experience working with ArcGIS) with a wide range of technical skills including native application development, spatial databases, desktop/web GIS, and scripting. My Master's degree is in GIS with a focus in Natural Resource Management. Currently, I'm most interested in building cross-platform and lightweight apps using our ArcGIS Runtime SDK for Qt.