Saturday, January 06, 2007

Excel formula to calculate distance between 2 latitude, longitude (lat/lon) points (GPS positions)

I've been looking for this one for ages, and the one I created based on an equation I found a little while ago didn't work too well :(

Investigation

Calculate distance, bearing and more between two Latitude/Longitude points shows lots of equations in math, and implemented in JavaScript. Chris Veness's page also contains an Excel formula of the ‘Haversine’ equation (actually, using the "spherical law of cosines") for distances between points in kilometres:

=ACOS(SIN(Lat1)*SIN(Lat2) +COS(Lat1)*COS(Lat2)*COS(Lon2-Lon1)) *6371

But it wasn't working, because my GPS points (Lat/Lon) were in decimal degrees, not radians. So I used the formula I found at Latitude And Longitude In Excel (under the "Great Circle Distances" section) by Chip Pearson:

=RadiusEarth*ACOS(COS(RADIANS(90-Lat1)) *COS(RADIANS(90-Lat2)) +SIN(RADIANS(90-Lat1)) *SIN(RADIANS(90-Lat2)) *COS(RADIANS(Long1-Long2)))

where Chip gives RadiusEarth as 6378.135 kilometres. This didn't match up with distances I had when calculated from other systems. But Chris's page has:

R = earth’s radius (mean radius = 6,371km)

Digging deeper, I found Chris's Vincenty formula for distance between two Latitude/Longitude points page which includes a table on different datum models (treating Earth as an ellipsoid), it shows WGS-84 & GRS-80 having the greatest radius on an ellipsoid as 6378.135km & the smallest as 6356.752km.

So Chip was using the maximum radius of the Earth, not the mean radius like Chris (I'm not sure where Chris gets the mean radius from). Substituting Chris's R for RadiusEarth in Chip's formula gives:

Solution

=ACOS(COS(RADIANS(90-Lat1)) *COS(RADIANS(90-Lat2)) +SIN(RADIANS(90-Lat1)) *SIN(RADIANS(90-Lat2)) *COS(RADIANS(Long1-Long2))) *6371

and my final version that uses Excel cell references is:

=ACOS(COS(RADIANS(90-A2)) *COS(RADIANS(90-A3)) +SIN(RADIANS(90-A2)) *SIN(RADIANS(90-A3)) *COS(RADIANS(B2-B3))) *6371

PS. To calculate distances in miles, substitute R (6371) with 3958.756 (and for nautical miles, use 3440.065).

339 comments:

«Oldest   ‹Older   201 – 339 of 339
Anonymous said...

Excellent, I need to adapt to get it to work in Qlikview

Unknown said...

Your excel formula has very worked well for me too. THANK YOU.

I have also been looking for the excel formulas for determining (1) the bearing between the two positions, and (2) a second position from the first position + heading w/distance.

I found several references to these formulas on the web but I am unable to get them work properly. Any assistance greatly appreciated.

Mark W

Anonymous said...

is the answer to formula in nautical miles or regular miles???

After examining the formulas, I am thinking it is solving in nautical miles???

Can anyone help ?

Holly Heyser said...

LOL, I don't understand a thing you wrote - this is way beyond my training - but I plugged in your formula and it worked, so thank you!

Anonymous said...

guys can someone tell me wat does radians mean

and also tell me wat does Acos mean and how these should be calculated

Anonymous said...

Thanks!

Anonymous said...

This is just an incredible time saver. Thank you very much!
You are the man!

Anonymous said...

Thanks man, really useful..I appreciate this very much..

-meIndonesia

K said...

Thanks!

Unknown said...

Thank you. Most practical solution I have found.

Anonymous said...

BRILLIANT!!! THANKS A LOT!

Anonymous said...

Could someone explain how this actually works. I know why we conver to radians but not sure what the ACOS, SIN and COS functions are doing?

Bryan A. said...

So I guess this has been said more than a few times above, but thank you again for posting this.

Alfonso J said...

awesome!!
Thank you very much=)

Anonymous said...

How about an excel formula to give ending lat and long coordinates from a given beginning lat, long, azimuth and distance in miles

Anonymous said...

God bless u for this.wowwow.thank u so much

Anonymous said...

Is there a way to do multiple locations like 1000 places and find which is the nearest?
I assume triangulate but how?
Thanks

Anonymous said...

This is a very helpful tool.

Okofo

Anonymous said...

Thank you so

Sean Adams said...

Wow...thank you so much for this. You saved us SOOOO much time and energy. Cheers! God Bless

Anonymous said...

Thank you for making this available. It is a significant help for geo-analysis.

-Mike H.

Unknown said...

A cool site for those of you wanting to calculate your journey between two places www.DistanceCalculator.co.za

Jual GPS Magellan said...

thanks for size idea jus for GPS. .realy good!!!

Anonymous said...

Big THANKS!!

Anonymous said...

Thanks, have used this in a libreoffice spreadsheet - no changes needed :)

Anonymous said...

AMAZING!!!! AMAZING!!! AMAZING!!!

Anonymous said...

Awesome. You are the man!!!

Anonymous said...

Excellent!

Works perfectly fine with PostgreSQL, too.

Thank you very much!

Anonymous said...

Somebody help me pls!
I have these coordinates:
47,49760 19,05395419
47,46597137 19,04359477
but in my excel the formula doesn't work..anybody?

Anonymous said...

Thx, this is very helful.

Unknown said...

fantastic. Thank you. It doesn't seem to differentiate between E/W coordinates as well as N/S. Slight error when running coordinates across the equator

Anonymous said...

After much internet research your walk through was the absolute BEST! Thanks for the help

Unknown said...

Hello,

I tried that calculation in order to find the distance between two points. I converted my latitudes and longitudes into radians but I take very large values (e.g. 9726 Km instead of 150 Km). Are there any suggestions?

Moreover, why is that subtraction of lats and lons from 90?

Unknown said...

Hello,

I tried to use this relation but I didn't take any reasonable results (e.g. I took 9730 Km instead of 150 Km). Are there any suggestions?

Why is that subtraction between lats and lons with 90?

I tried this subtraction and I tried radian conversion as well but it didn't work.

Thank you,

Stavros

Anonymous said...

Excellenté

Anonymous said...

2015 and the thank yous are still coming. This one from Ireland. Thank you, thank you, thank you.

Anonymous said...

I'll like to add my thanks also, you cut my workload by a huge amount.

Anonymous said...

WOW, THIS IS AMAZING. You are a rockstar! Thank you

Anonymous said...

Thank you a lot! Just what I needed and works perfectly (validated on google maps).
Greatings from Brazil.

Anonymous said...

Thank you so much!!! Very Usefull

Tomas said...

Nice , but I am afraid it do not take in attention, south/north hemisphere and mabe as well east/west.

But anway this formula help me to short time, to develop my one.

Good job .

Anonymous said...

EXCELLENT - thank you

Shaggy said...

Nicely done sir, takes the output from a phone app, giving a 99.77% accuracy over 1300 readings :-)

Sarah Bell said...

This is great! Thanks!

G1 said...

Second time using this - firstly in January 2014 and now in Jan 16 used for vessels now using it for aircraft

Anonymous said...

Hi, Can you anyone please help what is in the cells A2, A3, B2 and B3.
i'm looking for distance between two zip codes.

Thanks,

Suresh said...

Can anyone share sample excel file

Unknown said...

Thank you so much for the share. Your time is greatly appreciated.

I apply your formula =ACOS(COS(RADIANS(90-A2)) *COS(RADIANS(90-A3)) +SIN(RADIANS(90-A2)) *SIN(RADIANS(90-A3)) *COS(RADIANS(B2-B3))) *6371

but don't seem to get the same results as I do in Bing maps.In one case I am seeing 12.22km on excel and 22.1km in Bing maps. Am I doing something wrong. Sincerely. Peter

Anonymous said...

Thank you! :-)

Online Math Tutors said...

Thank you for this helpful post!

ignite ice said...

Thank you. This is most excellent!

Online Math Tutors said...

Thanks for the tips! It's very helpful. Looking forward to read more from you.

Anonymous said...

Thank you so much Sir, GOD BLESS YOU.

VET said...

Great! Thank you a lot!

lerrigatto said...

Still the only working formula on the internet!
Thank you so much.

Anonymous said...

So useful! Thanks now in 2016!

Unknown said...

that was awesome. my assignment was to calculate the distance between points and by applying the GIS and drawing the poly-line then measure the poly line length. but it make the easy wasy. this distance is in simple mile????

Anonymous said...

Thanks a billion. You shall continue to grow in knowledge as i have learned this selfless attitude of sharing information to even people i know not from what you just did.

Anonymous said...

Totally awesome! Thank you!

Unknown said...

Awesome !!!
Thank You.

OHM

Unknown said...

Really awesome !!!

Thank you.

OHM

Anonymous said...

You are an absolute genius and saved me a lot of work. Cheers from Australia.

Anonymous said...

Amazing!

Unknown said...

Excellent! and SO helpful! THANK YOU!
From that formula I have created a ready-to-use Excel spreadsheet. It is presently on the web at:
http://bit.ly/1WKbHu2

Anonymous said...

Another thank you from me! This would have taken a lot of time, instead it was easy as eating pie!
Cheers from Chile!

Anonymous said...

I gotta say, I have probably used this page 20 times in the past couple months. Solid work. Thanks a ton.

Unknown said...

Thank so much , you saved my time!!! elias

Unknown said...

Thank so much, you saved much of my time!!

Anonymous said...

This is really helpful - thank you so much!

John said...

I don't care if this post is more than 2 years old. It's great. Thank you, sir.

Unknown said...

You saved my day. Thanks a lot.
Jamal Zraqou.

Anonymous said...

i found this very helpful...but whats the significance of 'A' before 'cos' in the formular. =ACOS(COS(RADIANS(90-A2)) *COS(RADIANS(90-C2)) +SIN(RADIANS(90-A2)) *SIN(RADIANS(90-C2)) *COS(RADIANS(B2-D2))) *6371

Anonymous said...

The mile output appears to be too high - am I missing something? Thanks!

Anonymous said...

Thank you so much! This cost me a half a day of my life today!!

Unknown said...

This is perfect!! Can I be cheeky and ask for a script in R for this?

Unknown said...

This is perfect!! Can I be cheeky and ask for the R script for this?

Anonymous said...

I would love to join the praise BUT the formula only works for me for some of the distances I calculate - does anyone have an idea what's wrong? (e.g. it does not work for 12.99922, 48.967642 and 11.575529, 48.137039 - the formula gives me 182 km, but in reality, it is 139 km) - HELP, please!!!!

Anonymous said...

Thank you so much! That you make all of our work much more easier. However, I have one question that I really couldn't understand. Why does not the distance between some of the same two coordinates come out as exactly "0"? It comes out as "0,00009493" and "0,00013425". It would be really helpful if you or somebody else can answer this.

Unknown said...

What do the variable A2, A3, B2, and B3 stand for?
I can be contacted at rryyaann50@gmail.com.....please help

Anonymous said...

What do the variable A2, A3, B2, and B3 stand for?
I can be contacted at rryyaann50@gmail.com......please help

HLeunam said...

GREATT!!

George Bogaski said...

I sure appreciate your work on this!

Anonymous said...

Sick formula. Thanks!

Williams said...

Amazing!! Thank yoU!

Anonymous said...

What can I say except thanks!

Anonymous said...

I'll jump on now, just to keep the thanks flowing.

Saved my team a ton of time, and now I'm a hero. But really, you're the only hero I see!

Chetan Mahadik said...

2017!
Still helpful. Thanks man..

Anonymous said...

Worked perfect!

Anonymous said...

Awesome thanks!

Smitha said...

Thanks a ton! Very useful.

Anonymous said...

Thanks loads! I noticed that this post was put up 10 years ago! Helping people since 2007!

Sumanji said...

Dude, legendary, this was so helpful - thanks!

Anonymous said...

thank you .....

Anonymous said...

Thank you very much , after lots of struggle and the find your blog.

Anonymous said...

Amazing!! thanks so much!

Anonymous said...

Brillian!

John said...

nice write up.Calculate area of any building with Google maps.

Martin said...

Thank you so much.

Anonymous said...

Thank you very much!!

kvetha said...

Thanks a lot!

Unknown said...

I love you

Lou said...

Just want to let you know that 10 years later, your work is still appreciated. Thanks for developing this simple tool and providing the discussion on radius.

Amol said...

Thanks man it work for me to create sheet for my client Model X

Tharaka said...

Great help

Anonymous said...

Thank you so so so much!

Anonymous said...

I'm really grateful for this.
But somehow I encountered some problems when I calculate the distance of a lat long point towards itself.
in my opinion (CMIIW), the distances should be equal to zero, since it reflects the distance towards itself. However, based on your final function the result return to >1 miles distances instead.
here is the function I attached to my worksheet,

=ACOS(COS(RADIANS(90-$C$9))*COS(RADIANS(C9)+SIN(RADIANS(90-$C$9))*SIN(RADIANS(90-C9))*COS(RADIANS($D$9-D9)*6371)))

can you explain to me why?

Anonymous said...

right oN!

AMISS said...

Thanks, just what I need

AMISS said...

Thanks, just what I need

Mehvish Majeed said...

doesn't the formula SQRT((X2-X1)^2+(Y2-Y1)^2) work same?

Mehvish Majeed said...

doesn't the formula SQRT((X2-X1)^2+(Y2-Y1)^2) work same?

Prism - Breaking the obvious into its elements said...

Thanks
Superb stuff

Anonymous said...

Marvelous !!!!

Anonymous said...

Thanks a lot..

Unknown said...

Thanks man! it is very helpful!

Anonymous said...

Cheers guys... years later this is still fab!

Unknown said...

Note precise distance calculation on ellipsoid surface requires way more complicated approach. Vincenty formulae is one of the solutions. For Excel implementation see https://github.com/tdjastrzebski/VincentyExcel

Unknown said...

Bless you!.
Saved my day.

Anonymous said...

Bless you!
You saved my day.

Brad Hawkins said...

Thank you, this formula saved me so much work.

Unknown said...

Mad props Sir!

Anonymous said...

Thank you, thank you! It's been over four years since the last comment (almost exactly 11 years since original post), but I agree with previous posts that I couldn't just use your info and leave! V cool how many people you've helped, and the info is still relevant now - granted I'm currently still on Office 2013 so...

Ram said...

Thank you...Really helpful...

Unknown said...

To be thanked, to be commended.

Vatan said...

Thanks for such a help

Unknown said...

Great work thank you for the effort this is the only excel formula that I could find to work.

Anonymous said...

GENIUS!!!!! Exactly what I was looking for. THANKS!!!!

Unknown said...

What format do you enter Lat / Long into cells and what format do you use for the cell (number, general, custom, etc) for your calculation to work? Thanks

Unknown said...

Just Awesome!! You helped a lot..

One request....could you please share excel formula to calculate orientation between two points in degrees...??


Thanks in advance.... :)

Anonymous said...

Simply Brilliant. Thanks

Anonymous said...

10 years later, still a superstar!!!! thank you! :)

Anonymous said...

Thank you in 2019!!!

Anonymous said...

Maaaaan, it's 2019 and I'm from Brazil... But THANKS!!!!

Anonymous said...

2019 and this blog post is still relevant. Well done, you nailed it. Thanks!

Anonymous said...

This formula is accurate. Its result perfectly matches the direct site survey result. Thank you so much!

Anonymous said...

Life. Saver.

Anonymous said...

Muchísimas gracias !!! te escribo +10 años después de tu post y me resultó muy útil.

Anonymous said...

Miles Gracias por este post! + de 10 años después y me es de gran ayuda

Anonymous said...

You have my vote for president of america :)

«Oldest ‹Older   201 – 339 of 339   Newer› Newest»