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
<SPAN class="token function">connect</SPAN><SPAN class="punctuation token">(</SPAN>m_mapView<SPAN class="operator token">-</SPAN><SPAN class="operator token">></SPAN><SPAN class="token function">locationDisplay</SPAN><SPAN class="punctuation token">(</SPAN><SPAN class="punctuation token">)</SPAN><SPAN class="punctuation token">,</SPAN> <SPAN class="operator token">&</SPAN>LocationDisplay<SPAN class="operator token">::</SPAN>locationChanged<SPAN class="punctuation token">,</SPAN> <SPAN class="keyword token">this</SPAN><SPAN class="punctuation token">,</SPAN> <SPAN class="punctuation token">[</SPAN><SPAN class="keyword token">this</SPAN><SPAN class="punctuation token">]</SPAN><SPAN class="punctuation token">(</SPAN>Location location<SPAN class="punctuation token">)</SPAN>
<SPAN class="punctuation token">{</SPAN>
Point currentLocation <SPAN class="operator token">=</SPAN> GeometryEngine<SPAN class="operator token">::</SPAN><SPAN class="token function">project</SPAN><SPAN class="punctuation token">(</SPAN>location<SPAN class="punctuation token">.</SPAN><SPAN class="token function">position</SPAN><SPAN class="punctuation token">(</SPAN><SPAN class="punctuation token">)</SPAN><SPAN class="punctuation token">,</SPAN> SpatialReference<SPAN class="operator token">::</SPAN><SPAN class="token function">webMercator</SPAN><SPAN class="punctuation token">(</SPAN><SPAN class="punctuation token">)</SPAN><SPAN class="punctuation token">)</SPAN><SPAN class="punctuation token">;</SPAN>
<SPAN class="punctuation token">.</SPAN><SPAN class="punctuation token">.</SPAN><SPAN class="punctuation token">.</SPAN><SPAN class="line-numbers-rows"><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN></SPAN>- 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 <SPAN class="operator token">=</SPAN> GeometryEngine<SPAN class="operator token">::</SPAN><SPAN class="token function">intersects</SPAN><SPAN class="punctuation token">(</SPAN>currentLocation<SPAN class="punctuation token">,</SPAN> m_graphic<SPAN class="operator token">-</SPAN><SPAN class="operator token">></SPAN><SPAN class="token function">geometry</SPAN><SPAN class="punctuation token">(</SPAN><SPAN class="punctuation token">)</SPAN><SPAN class="punctuation token">)</SPAN><SPAN class="punctuation token">;</SPAN><SPAN class="line-numbers-rows"><SPAN></SPAN></SPAN>
- If the GeometryEngine function returns true, alert the user.
<SPAN class="keyword token">if</SPAN> <SPAN class="punctuation token">(</SPAN>m_within<SPAN class="punctuation token">)</SPAN>
<SPAN class="token function">alertUser</SPAN><SPAN class="punctuation token">(</SPAN><SPAN class="string token">"within"</SPAN><SPAN class="punctuation token">)</SPAN><SPAN class="punctuation token">;</SPAN><SPAN class="line-numbers-rows"><SPAN></SPAN><SPAN></SPAN></SPAN>
- 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.