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
- Just copy the JSON logic and paste into your Power Platform workflow (typically as an inline calculation or custom connector).
- 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).
- 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:
// Example for PowerApps With( { r: 6371000, p: (Pi() / 180), latA: YourStartLatitude, lonA: YourStartLongitude, latB: YourEndLatitude, lonB: YourEndLongitude }, Round( (2 * r) * Asin( Sqrt( 0.5 - Cos((latA - latB) * p) / 2 + Cos(latB * p) * Cos(latA * p) * (1 - Cos((lonA - lonB) * p)) / 2 ) ), 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 or flow.
🎉 Result
Enjoy accurate, fast, and cost-effective distance calculations for any business scenario, including logistics, travel, or field service.
🌟 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
Q1: What is the Haversine formula?
A mathematical function for calculating the shortest distance between two points on the Earth, given their latitude and longitude.
Q2: How do I get my coordinates in PowerApps?
Use built-in location functions or user input fields to collect latitude and longitude.
Q3: Can I use this in Power Automate as well?
Yes, simply adapt the code for use in expressions, custom connectors, or flow steps.
Need Help with Your Power Platform Project?
Get expert guidance through personalized workshops, consulting, and training tailored to your specific needs.
Request WorkshopConsulting and process automation for SMBs is delivered by KMUpower. Prefer structured, German-language courses? The Power Platform Academy offers live Power Platform Schulung and hands-on Power Platform courses for Power Apps, Power Automate and Copilot.