HOW TO build a geofence with a Map, Circle and LocationSensor Components

Skip to first unread message


Jul 30, 2019, 5:45:04 PM7/30/19
to MIT App Inventor Forum
Geofence - using the MIT Map and Circle components to define a circular geofence by SteveJG

This example uses the Map, Circle and LocationSensor components to define whether the Android is inside a circle geofence of 8 meters radius or outside.


The example app samples the device's location every 30 seconds.  Some GPS hardware might obtain updated satellite fixes more rapidly or others, with fewer receive channels, might require more time (most cell phones).  Thirty seconds is hard coded.  You can code a faster TimeInterval but the GPS will not update until it receives a different satellite fix.  Unconvinced?  Create a similar app using the AdvancedLocationExtension without the Map component.  Sorry, the ALE does not work in conjunction with the MIT Map component. The ALE is able to show how frequently the Location updates.  Small TimeIntervals seem to work in the LocationSensor, but despite the instruction to poll at the faster rate, the locations do not actually update that fast.

For purposes of this tutorial, the position of the target geofence circle on your map is determined by adding 0.00008 degrees to the actual device location as a demonstration.  0.00001 is about 1.11 meters .  The example uses (a offset of the device location latitude and longitude for the circle's location to demonstrate the principle.  Use a fixed location in your app to place the circle geofence,  Write your own code to be able to place it where you actually want it


Button1 (Check Status) forces a GPS update immediately (if possible).

The demonstration provides a notation about current GPS Accuracy property in meters.  The distance to the target geofence circle (if the device is not within the geofence already) is calculated.  It will report whether the device is within the geofence or not (inside/outside).  An OpenStreetMap tile is shown at a zoom of 18 (so the app can show /  resolve sufficient detail of the circle geofence (8 meter red circle) and the current GPS Accuracy circle (pale blue circle).

The best location resolution is obtainable with higher quality GPS receiver models. The more receive GPS channels available in the GPS hardware the better.  The display image is from a Samsung A10 tablet.  Most older and cell phone Android devices will not provide as good resolution. Resolution and best Accuracy (low numbers) depends on many factors (is it raining, is device is indoors in a steel frame building, is located in a city canyon where building obscure the view etc.

What is reasonable to expect from your GPS and app.

Can your Android find your geofenced area?   Maybe. Whether it can determine whether you device is within or outside the bounds of the circle geofence depends on the size of the geofence circle, the setting of TimeInterval and how fast the Android is moving (in a moving car, carried while walking etc.)

 Your Android  may be traveling too fast to resolve the geofence.  Traveling at 60 mph ( 88 feet/sec (26.8 meters/sec) you travel 264 feet ( about 80.5 meters) in 3 seconds at 60 mph. Be aware, 0.00001 degree of latitude is approximately 1.11 meters at the equator (3.6 feet)  (  ) .     If your device travels at 60 mph, you blow through the 8 meter radius circle (16 m in diameter) geofence in less than a second.   If you sample at each thirty seconds (a reasonable rate) then it is easy for your device to be outside of the circle at a sampling point and then the device may not sample again until you are on the other side of the geofence.  You could try more rapid sampling rates but your GPS certainly will not refresh within a second.   

It is difficult for your app and device to determine whether your device is within or outside of small areas (geofences) with any reliability.   The example uses a Circle with a radius of 8 meters.   Try a radius of 100 meters.  You can, however monitor how close you are to the geofence (i.e. distance to the geofence from your present location) but only to the extent Accuracy is rather low values and sampling is sufficient to obtain valid location of the moving Android in relation to the frequency of sampling.


Your GPS really does not 'know' exactly where it is at any moment.  The green circle is where the Android actually is, the other marker pins show where the LocationSensor (resting on a desk ,  reporting its location once a minute, thought the device was ... occasionally over 50 meters away from the actual location.

 A cell phone GPS cannot resolve better than within two meters; professional papers indicate most cell phones are capable of resolving about 5 m most of the time but often are only capable of resolving 30 m.  The Accuracy is variable (it changes for each satellite fix) so when the LocationSensor posts a location, that location is only about 68 percent reliable.  If you move too fast near or through a small circular geofence , the device might not notice the geofence circle. Consequently, small geofences cannot be reliable when used using a cell phone GPS with any guarantee of registering where one is inside or outside the geofence.   

To test if your geofence works,   remove the Blocks that show latitude / longitude plus 0.00008 degrees  and replace them with the coordinates you really want.  Then walk through it.  Outside of the circle area will report  outside   and inside, well, you get the idea.   Experiment. You can also set the Circle radius to 100 meters.and leave the other blocks alone... the app should tell you you are   inside .   Does it?   Also experiment with the distance to the edge of the circle ...using the distance.   When you are within the Circle, the distance is 0, outside, it could be almost anything.

You can code this so that when distance is zero, you use the Sound component to produce a beep  or use the TextToSpeech component to say   you arrived, lucky your ring fence was large enough to register or something.




Jul 31, 2019, 12:28:11 PM7/31/19
to MIT App Inventor Forum
Reply all
Reply to author
0 new messages