Location - Finding the closest location and and sorting records by distance, based on the current location of the user

Given a data source that contains a list of locations how do we find the record that matches the location nearest to where the user currently is? Also, how can we sort the records in a data source by the relative distance?

In this post, we'll walk through the formula that enables us to carry out this task

Demonstration of technique

To demonstrate this technique, let's take a map of the following underground stations. The latitude and longitude values are as follows:

Bayswater 51.512254, -0.187522
Queensway 51.510473, -0.186959
Lancaster gate 51.511721, -0.175290


Let's now assume that we're at the given co-ordinate:
51.512611, -0.182105

How do we find the closest underground station, and return a list of the stations sorted in terms of relative distance?

Setting-up our source data

In this example, we'll use the following collection as a data source. In practice, we could data from a SharePoint list or any other data source.

ClearCollect(colLocations,
{StationName:"Bayswater", StationLat:51.512254, StationLong:-0.187522},
{StationName:"Queensway", StationLat:51.510473, StationLong:-0.186959},
{StationName:"Lancaster Gate", StationLat:51.511721, StationLong:-0.175290}
)

Building a formula to calculate and to sort records by distance

To carry out the distance calculation, we apply the Haversine formula that I described in my earlier post here.


We call the AddColumns function to add a column called "Distance" to our data source. We use the Haversine formula to caulate the relative distance between the static input coordinate, and the coordinates from the data source.

We can then apply the SortByColumns function around the return value of AddColumns to return the records sorted by distance ascending order. Here's our final formula.

SortByColumns(
AddColumns(
colLocations,
"Distance",
With(
{
r: 6371,
p: (Pi() / 180),
latA: 51.512611,
lonA: -0.182105,
latB: StationLat,
lonB: StationLong
},
(2 * r) *
Asin(Sqrt(0.5 - Cos((latA - latB) * p)/2 + Cos(latB * p) *
Cos(latA * p) * (1 - Cos((lonA - lonB) * p)) / 2))
)
),
"Distance",
Ascending
)

Here, latA and lonA refer to the input coordinate. We can substitute this as required, or substitute these values with calls to Location.Latitude and Location.Longitude to return the current location of the user.

Testing this formula

To test this formula, the screenshot beneath shows the result when we set the items property of a data table to this formula. As we can see, the closest underground station is Bayswater, with a distance of 0.3km.

Conclusion

In this post, we looked at how to apply the Haversine formula to return a list of records, sorted by the distance relative to an input coordinate.

Related posts

FormuIas - Is it possible to call a user-defined function recursively in Power Apps?
January 31, 2024
Formulas - A beginners guide on how to create and call user-defined functions (UDFs)
January 28, 2024
Formula - How to add a button that converts degrees Centigrade to Fahrenheit and vice versa
May 08, 2023
Formula - How to convert a single delimited string to rows and columns
April 05, 2023
Data - How to group data in a gallery and calculate sums
January 20, 2023
Formula - How to calculate compound interest
November 24, 2022
Utilities - The best way to peform OCR on images of Power Apps Formulas
October 11, 2022
Example - How to use a drop down control to convert currencies
September 27, 2022
Formula - How to parse JSON in Power Apps- 4 examples
September 15, 2022
Data - How to get a row by ordinal number
April 28, 2022
Formula - What to do when the If statement doesn't work?
December 17, 2021
Formula - Boolean And / Or operators - What is the order of precedence?
December 16, 2021
Controls - How to set the data source of a Combo Box to a comma separated string
November 16, 2021
Numbers - 10 examples of how to round numbers
August 18, 2021
Formula - Difference between round, square, and curly brackets
July 20, 2021
Top 3 highlights of upcoming enhancements to the Power Apps language (Power FX)
May 26, 2021
Email - Sending email attachments with the Office 365 Outlook connector
March 30, 2021
Formula - What to try when numbers don't format correctly
March 24, 2021
Controls - How to convert HTML to Text
March 23, 2021
Formulas - how to return all days between two dates
March 15, 2021
Formula - How to create comma separated (CSV) list of items
February 25, 2021
Formula - How to use the IF and Switch functions - 3 common examples
February 12, 2021
Formulas - How to cope with weekends and public holidays in date calculations
January 21, 2021