Geofencing in Power Apps

Sometimes business processes will need to know the location of the users and depending on the returned value, different actions will be taken. For example, Office365 retail version can only be activated in the region where it is purchased, and the data sharing from your WhatsApp client to Facebook does not work in India thanks to Indian government’s fight against Facebook.

Two common approaches were normally used to determine a user’s location, IP Address or Global Positioning System (GPS). Checking location against IP Address is not very straightforward since it involves multiple checks with multiple levels of registration authorities and the returned result is still a region instead of a point location. On the other hand, GPS is more widely used due to its instant feedback signal, self localization without registration authority, easily accessible(all modern phones have an Assisted GPS chip in them) and high accuracy (Standard Positioning Service provides horizontal accuracy of better than 3.5 meters). The only shortcoming for GPS could be that it does not have a good signal when underground, i.e. tunnels, basement, etc.

Luckily Power Apps provides a simple method to extract GPS signal values from the host device so we can make use of them to build our own location based applications. Of course, the pre-requisite is for users to grant relevant device permissions when running the app for the first time and the device’s GPS function has been turned on. Otherwise, Power Apps will return empty values.

Functions to Extract GPS Values

The function to extract GPS values is simply called Location, and it is a basic function in the standard library of Power Apps which means you don’t need to add extra data connector when developing the app. It has three properties corresponding the three values used by GPS systems.

Location.Altitude

Returns a number that indicates the altitude, measured in meters, above sea level.

Location.Latitude

Returns a number, from –90 to 90, that indicates the latitude, as measured in degrees from the equator. A positive number indicates a location that’s north of the equator.

Location.Longitude

Returns a number, from –180 to 180, that indicates the longitude, as measured in degrees from Greenwich, England. A positive number indicates a location that’s east of Greenwhich.

When a user accesses your location based app for the first time, the device may prompt that user to allow access to this information like below screen shots.

Build Simple Geofencing

With the capabilities to extract the GPS location of the host device, we can build simple geofencing in Power Apps together with some simple math.

Get GPS Values of Reference Points for Geofencing

The GPS values of any location can be simply retrieved from Google maps. Open Google Map on your PC, right click on any location you want to use as your reference point, the Latitude and Longitude are directly displayed.

Build a Circle Shape Geofencing

We need to take two reference points in order to build a Circle Shape geofencing, O(O.Latitude, O.Longitude) and P(P.Latitude, P.Longitude).

The formula is rather straightforward:

(Location.Latitude - O.Latitude)^2 + (Location.Longitude - O.Longitude)^2 <= (P.Latitude - O.Latitude)^2 + (P.Longitude - O.Longitude)^2

Build a Rectangular Shape Geofencing

Again we need to take two reference points A(A.Latitude, A.Longitude) and B(B.Latitude, B.Longitude).

And the corresponding formula is:

Location.Latitude >= A.Latitude && Location.Latitude <= B.Latitude && Location.Longitude >= A.Longitude && Location.Longitude <= B.Longitude

Leave a Comment