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!

Im very much interested in geo fecing some of my forms. We have truck driver that need to register with us before arriving so we can assign a door to them. we dont want the to be able to register if they are not within 5 miles of the building. SO if the try to register outside the five miles, it would be a hard stop and a message would come up that they are outside the 5 mile radius. Have you done any further development on this front?

For this use case, I would not recommend using a formula field, as I describe above, because formula fields only calculate after the record is created, which makes them unsuitable for enforcing a true “hard stop” on form submission.

Instead, the appropriate approach in Tadabase is to use the Get Distance pipe. This allows you to calculate the distance between the driver’s current GPS location and a fixed facility location before the record is saved, and then evaluate that result using the page’s validation rules .

To support this, you will need a facility or warehouse record in your database that contains either a full address or stored latitude/longitude values that Tadabase can reference. The Get Distance pipe compares the driver’s current GPS coordinates against the stored facility location.

On the form, the rules should handle three outcomes:

  • If the calculated distance is greater than 5 miles, block submission and display a message indicating the driver is outside the allowed radius
  • If the distance is within 5 miles, allow submission to proceed
  • If the distance cannot be calculated at all (for example, the device cannot determine the driver’s location or the facility location is missing), block submission and display clear instructions on what the driver should do next, such as contacting dispatch or checking in on arrival

By explicitly handling the “location cannot be determined” scenario, you avoid denying access to drivers who are physically on-site but experiencing GPS or device limitations, while still preventing registrations from outside the allowed radius.

Hi Co1n,

Thank you for the clarification and advise. I will do some testing with the Get Distance Pipe.

Thanks again.