Canvas Apps Expressions

G – Geofencing – How to implement in Power Apps?

A fence
A fence

Introduction

Geofencing is an important concept that gets used a lot. It has its uses for several different use cases. For example, if you want to find all technicians within a certain radius of a service address (typical field service scenario).

Implementing geofencing

Typically, geofencing involves drawing a circle around a given point. While this approach will not give you an exact radius around a given location, it is a good approximation that has worked for me for most use cases. The approach involves the following steps:

  1. Gather a user's current location (to learn more about signals, click here)
  2. Add/subtract a predefined or configurable range to the user's latitude and longitude
  3. Then filter the list of accounts/contacts/service locations/technicians that have latitude/longitude within that range

Here is a sample code for geofencing:
OnSelect of a button:

Set(
    gblLatRangeSetting,
    Value(TextInput4.Text)
);
Set(
    gblLongRangeSetting,
    Value(TextInput4.Text)
);
Set(
    gblUserLat,
    Location.Latitude
);
Set(
    gblUserLong,
    Location.Longitude
);
Set(
    gblLatValidationLowerEnd,
    Value(gblUserLat - gblLatRangeSetting) * 1000
);
Set(
    gblLatValidationUpperEnd,
    Value(gblUserLat + gblLatRangeSetting) * 1000
);
Set(
    gblLongValidationLowerEnd,
    Value(gblUserLong - gblLongRangeSetting) * 1000
);
Set(
    gblLongValidationUpperEnd,
    Value(gblUserLong + gblLongRangeSetting) * 1000
);

Items property of a gallery:

Filter(
    colAddress,
    (Lat >= RoundDown(
        gblLatValidationLowerEnd / 1000,
        5
    ) && Lat <= RoundDown( gblLatValidationUpperEnd / 1000, 5 )) && (Long >= RoundDown(
        gblLongValidationLowerEnd / 1000,
        5
    ) && Long <= RoundDown(
        gblLongValidationUpperEnd / 1000,
        5
    ))
)

This creates a square but is a good approximation of geofencing for all intents and purposes.

Stay tuned for the remaining 19 tips!

Recent articles

Leave a Reply