#PowerPlatformTip 44: 'Calculate the Distance'
TL;DR: Calculate great-circle distance for free using the Haversine formula: copy the ready-made snippet and plug in your two lat/long pairs.
Here’s a cool tip to calculate the distance between two places without relying on premium services like Google or Bing maps.
💡 Challenge
How do you provide accurate, real-time location-based distance calculations inside PowerApps or Power Automate, without extra costs?
✅ Solution
You can use the Haversine formula, a proven geospatial calculation mechanism.
- There’s a ready-to-use code snippet available: CalculateDistance.json @ GitHub, Marcel Lehmann
- Use the formula in step 2 in Power Apps. For Power Automate, note that the flow expression language has no cosine or arcsine function (its math functions are
add,div,max,min,mod,mul,pow,rand,rangeandsub, see Microsoft Learn), so the formula can’t be copied into a flow expression. Run the calculation where those functions exist (for example in the app and pass the result to the flow, or in an external service such as an Azure Function that the flow calls); the linked JSON is a reference for the logic and has to be adapted before you use it. - Replace the latitude and longitude values for locations A and B with your own values.
🔧 How It’s Done
- Obtain coordinates:
- Manually input latitude and longitude for each location, or use the PowerApps location function to fetch the user/device position (
Location.Latitude,Location.Longitude). - Check that all four values exist before you calculate (test for blank, not for
0: a latitude or longitude of 0 is a valid value). If the device position isn’t available, offer the manual input instead. You only need to keep the resulting distance, not the raw coordinates.
- Manually input latitude and longitude for each location, or use the PowerApps location function to fetch the user/device position (
-
Drop the values into the shared script or formula:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20
// Example for PowerApps With( { r: 6371000, p: (Pi() / 180), latA: YourStartLatitude, lonA: YourStartLongitude, latB: YourEndLatitude, lonB: YourEndLongitude }, With( { a: Max(0, Min(1, // keep rounding errors inside 0..1 0.5 - Cos((latA - latB) * p) / 2 + Cos(latB * p) * Cos(latA * p) * (1 - Cos((lonA - lonB) * p)) / 2 )) }, Round((2 * r) * Asin(Sqrt(a)), 2) ) ) - For JSON users, import the code from the referenced snippet and wire up your variables according to context.
- The result is your distance in meters, output instantly in your app.
🎉 Result
Enjoy fast, cost-effective distance calculations for business scenarios such as logistics, travel, or field service. The result is the straight-line (great-circle) distance on a spherical Earth model with a fixed radius, not the route length, and it is only as exact as the coordinates you feed in. The two decimals are the rounding of the output, not the measuring accuracy.
🌟 Key Advantages
- No dependency on paid mapping services
- Works both in manual and automated scenarios
- Can leverage device location for dynamic, user-centered calculations
🛠️ FAQ
1. What is the Haversine formula?
A mathematical function for calculating the shortest distance between two points on the Earth, given their latitude and longitude.
2. How do I get my coordinates in PowerApps?
Use built-in location functions or user input fields to collect latitude and longitude.
3. Can I use this in Power Automate as well?
Not directly. The Power Apps formula above doesn’t run in a flow as it is, and flow expressions have no cosine or arcsine function. Calculate the distance in the app and pass the result to the flow, or call an external service (for example an Azure Function) from the flow.
🔗 Related Tips
🔸 #PowerPlatformTip 43: ‘With Function’ - the function keeping the Haversine formula above readable instead of a wall of nested math.
🔸 #PowerPlatformTip 72: ‘PowerApps with Google Maps API’ - a mapping alternative for when you need more than a raw distance number.
🎥 Video Tutorial
This tip is by Marcel Lehmann, Microsoft MVP (Business Applications) and founder of the Power Platform Academy, which runs free live workshops (in German). See the next free workshop dates (in German). For companies that want this implemented for them, see KMUpower: Power Platform consulting & automation for SMEs (DACH).