Location-Based Geofencing & Distance Between Addresses

I’m working on creating a geofencing function for my application that verifies that tasks are signed off in the vicinity of the building where the work occurred. Here’s how it works so far:

In the Tasks table, I have the following fields:

  • {Original Address} This field stores the work address.
  • {Completed Address} This field records the location where the job sign-off took place.
  • {Max Distance} This field records the geofence size I want to apply around the work address.
  • {Distance Apart} This field calculates the distance between two Addresses
  • {Geofence Check} This field indicates if the address is inside or outside the geofence ring.

To calculate the distance between the two addresses, I use this formula in an Equation field called {Distance Apart}:

6371000 * 2 * asin(sqrt( power( sin((({Completed Address - Latitude} * 1000000 - {OriginalAddress - Latitude} * 1000000) * PI() / 180) / 1000000 / 2 ), 2 ) + cos({OriginalAddress - Latitude} * PI() / 180) * cos({Completed Address - Latitude} * PI() / 180) * power( sin(({Completed Address - Longitude} * 1000000 - {OriginalAddress - Longitude} * 1000000) * PI() / 180) / 1000000 / 2 ), 2 )))

Key Points

  1. The Equation field must have an output type of number, rounded to 3 decimal places (or better if future versions allow).
  2. The address fields must display their latitudes and longitudes.
  3. The equation returns the metric distance in metres, which is standard in Australia. The Earth’s average radius is 6,371,000 metres. If you prefer a different unit, you can adjust this:
  • Feet: 20,902,000
  • Miles: 3,958.8
  • Kilometres: 6,371
  1. Unfortunately, this equation doesn’t work if you’re a flat-earther.

This method can also be used to find records within a defined radius of a given location, making it versatile for other use cases as well.

This gives you an approximate distance ‘as the crow flies’. With the limited decimal places and the fact that the Earth sizes I used are simply averages, the result gives a “good enough” approximation for most purposes.

Once the distance is calculated, it’s a simple check to determine if the sign-off occurred inside or outside the geofenced area:

{Geofence Check} field formula: If( {Distance Apart} > {Max Distance}, “Outside”, “Inside”)

Thanks to ChatGPT for assisting with overcoming the problem of limited decimal places in the equation field and doing the mathematical heavy lifting.

Other Thoughts

  • Another cool thing would be to determine the distance between where two photos were taken using the photo’s EXIF metadata. Has anyone done any work on this? It could be a valuable extension of this function.
  • Has anyone worked out how to add your device’s default location into a data field? I haven’t figured out how to drop the current location of the user’s device into the {Completed Address} field, so that’s my next challenge.
  • I would also like to populate the latitude/longitude sections of an address if the person is situated in a place like a golf course or in the outback, where there isn’t a street address. My aim is to use this function to geolocate assets outside a building.

It would be much appreciated if you could comment on anything you have found that would add to this topic!

4 Likes

Thank you for sharing this!