web
You’re offline. This is a read only version of the page.
close
Skip to main content

Announcements

News and Announcements icon
Community site session details

Community site session details

Session Id :
Power Platform Community / Forums / Power Apps / Distance in miles usin...
Power Apps
Answered

Distance in miles using Haversine formula

(0) ShareShare
ReportReport
Posted on by 42

Hi,

 

I am trying to calculate a distance (as the crow flies) in my canvas app, in a label, between two sets of Longitudes and Latitudes. 

 

For example, this is my code so far for the label:

 

Text(
 Round(
 3959 * 2 * Atan2(
 Sqrt(
 Sin((52.45202297146137 * Pi()/180 - 50.84629323 * Pi()/180) / 2) ^ 2 + 
 Cos(50.84629323 * Pi()/180) * Cos(52.45202297146137 * Pi()/180) * 
 Sin((-1.9431014756895428 * Pi()/180 - 0.47030607 * Pi()/180) / 2) ^ 2
 ), 
 Sqrt(
 1 - (
 Sin((52.45202297146137 * Pi()/180 - 50.84629323 * Pi()/180) / 2) ^ 2 + 
 Cos(50.84629323 * Pi()/180) * Cos(52.45202297146137 * Pi()/180) * 
 Sin((-1.9431014756895428 * Pi()/180 - 0.47030607 * Pi()/180) / 2) ^ 2
 )
 )
 ), 
 2
 ) & " miles"
)

 

But this shows as 12285.87 miles.... when in actual fact the distance is 188 miles.... what am I doing wrong here?

 

Thanks

LM

Categories:
I have the same question (0)
  • mmbr1606 Profile Picture
    14,629 Super User 2026 Season 1 on at

    hey @LostMary 

     

    try this:

    Text(
     Round(
     3959 * 2 * Atan2(
     Sqrt(
     Sin((52.45202297146137 * Pi()/180 - 50.84629323 * Pi()/180) / 2) ^ 2 + 
     Cos(50.84629323 * Pi()/180) * Cos(52.45202297146137 * Pi()/180) * 
     Sin((-1.9431014756895428 * Pi()/180 - 0.47030607 * Pi()/180) / 2) ^ 2
     ), 
     Sqrt(
     1 - (
     Sin((52.45202297146137 * Pi()/180 - 50.84629323 * Pi()/180) / 2) ^ 2 + 
     Cos(50.84629323 * Pi()/180) * Cos(52.45202297146137 * Pi()/180) * 
     Sin((-1.9431014756895428 * Pi()/180 - 0.47030607 * Pi()/180) / 2) ^ 2
     )
     )
     ), 
     2
     ) & " miles"
    )

     

    Let me know if my answer helped solving your issue.

    If it did please accept as solution and give it a thumbs up so we can help others in the community.



    Greetings

  • LostMary Profile Picture
    42 on at

    Thanks @mmbr1606 but its still showing as 12285.87 miles 😞

  • mmbr1606 Profile Picture
    14,629 Super User 2026 Season 1 on at

    another guess:

    Text(
     Round(
     3959 * 2 * Atan2(
     Sqrt(
     Sin((52.45202297146137 * Pi()/180 - 50.84629323 * Pi()/180) / 2) ^ 2 + 
     Cos(50.84629323 * Pi()/180) * Cos(52.45202297146137 * Pi()/180) * 
     Sin((-1.9431014756895428 * Pi()/180 - (-0.47030607 * Pi()/180)) / 2) ^ 2
     ), 
     Sqrt(
     1 - (
     Sin((52.45202297146137 * Pi()/180 - 50.84629323 * Pi()/180) / 2) ^ 2 + 
     Cos(50.84629323 * Pi()/180) * Cos(52.45202297146137 * Pi()/180) * 
     Sin((-1.9431014756895428 * Pi()/180 - (-0.47030607 * Pi()/180)) / 2) ^ 2
     )
     )
     ), 
     2
     ) & " miles"
    )

     

    Let me know if my answer helped solving your issue.

    If it did please accept as solution and give it a thumbs up so we can help others in the community.



    Greetings

  • LostMary Profile Picture
    42 on at

    @mmbr1606 That says 12309 miles.......

  • Verified answer
    timl Profile Picture
    37,283 Super User 2026 Season 2 on at

    Hi @LostMary 

    The following formula should produce the correct result.

     

    With(
     {
     r: 3958.8,
     p: (Pi() / 180),
     latA: 52.45202297146137,
     lonA: -1.9431014756895428,
     latB: 50.84629323,
     lonB: 0.47030607
     },
     (2 * r) * 
     Asin(Sqrt(0.5 - Cos((latA - latB) * p)/2 + Cos(latB * p) * Cos(latA * p) * 
     (1 - Cos((lonA - lonB) * p)) / 2))
    )

     

    The result of this is 151 miles.

     

    timl_0-1705488117649.png

     

    This is the same result that you get from online calculators, and it's more or less what I see when I type those co-ordinates into Google Maps.

     

    timl_1-1705488194299.png

     

     I have a post here that describes this formula in more detail:
    http://powerappsguide.com/blog/post/formulas-calculate-the-distance-between-2-points-longitude-latitude

     

Under review

Thank you for your reply! To ensure a great experience for everyone, your content is awaiting approval by our Community Managers. Please check back later.

Helpful resources

Quick Links

Season of Sharing Community Challenge Winners!

Congratulations to our community stars!

Kudos to our 2025 Community Spotlight Honorees

Expanding mentorship, skilling, and AI innovation

Congratulations to the July Top 10 Community Leaders!

These are the community rock stars!

Leaderboard > Power Apps

#1
WarrenBelz Profile Picture

WarrenBelz 401 Most Valuable Professional

#2
11manish Profile Picture

11manish 201 Super User 2026 Season 2

#3
MS.Ragavendar Profile Picture

MS.Ragavendar 128 Super User 2026 Season 2

Last 30 days Overall leaderboard