Skip to main content

Distance between Latitude and Longitude points

Submitted by mferanda on

Due to arduino hobby, a project led me to messing with a GPS module. So, I got a little interested in lat/long distance calculations and had to even refresh myself a bit on it.

First off… let’s look at lat and long itself.

What is Latitude and Longitude in one sentence? Latitude and Longitude are two separate sets of numbers providing a location on our planet.

Latitude and Longitude can come in two forms:
•    Degrees, Minutes, and Seconds
•    Radian Unit / Decimal Notation

Degrees, Minutes, and Seconds

Let’s work on a longer explanation and example. Take a round ball and draw two lines all around it, in your mind or otherwise. This will be from the center of the ball going top to bottom (Prime Meridian) all around and then center from left to right like a belt (Equator). You should then have two circles around your ball that will make a + at the point they meet. That is Lat: 0, Long: 0. If your lat and long is both 0, then you’re probably standing on an imaginary island off the cost of Africa. Let’s call it the island of geocoding failures.

From there with your ball, you could slowly start drawing circles of equal distance around the globe. Maybe 4 on each side to make 20 separated lines. Lines from the equator will just ring around the ball. Lines from the Prime Maridian will always cross the top and bottom of the ball. Once you’re done drawing the rest of your lines, your ball could look something like this:

Globe with angle degree points off of the various centers

Here's another example with less lines

Globe with angles

In the first picture above, we can see the center of the Earth with a point marker for 40N, 60W. Imagine that you’re standing in the center of that ball and all the lines are around you. Let’s say your hand is stretched out like a stiff-arm karate chop and you’re pointed to 0, 0. Now, you need to 40N, 60W. First, start moving your arm up a bit (North) until you’ve moved to a 40 angle. After that, you’re going to move your arm from 40N, 0 over to 60 to the right. Congratulations, you’re now a human lat and long example.

Latitude runs from 0-90 degrees… you’re either North or South of the equator around the globe up to 90. Then Longitude, you can go 180 degrees either direction as West or East. After your degrees, further accuracy is then provided by minutes and seconds. There’s 60 minutes between each degree and 60 seconds between each minute.

“But Mike, the latitude and longtitude numbers I’ve seen look like decimals. Not this degree, minutes North South West East whatever stuff!”

On to some basic math in converting this to decimals!

Radian Unit / Decimal Notation

This is just the decimal form of the degrees, minutes, and seconds for Latitude and Longitude. You can easily convert yourself…

First, you have to figure out if your main degrees are positive or negative. North and East are positive numbers while South and West are negative numbers. 0-90 and 0-180 stays the same with the only change being the positive or negative.

Second, conversion of your minutes and seconds is done by turning all your minutes into seconds and adding your seconds to it. From there, figure out your percentage to 3600. Seconds / 3600 = Decimal

Example:
Lat: 52N 10' 23”
Lat: 52.173056

Long: 32W 45' 12"
Long: -32.753333

Hopefully now you should understand Latitude and Longitude. Now, what if you had two points and you wanted to figure out the distance between the two. Well, that’s where things get complicated.

Ok now... we're graduating to some higher level math...

Spherical Low of Cosine Theorem

Here is a little info on Spherical Low of Cosine: 

https://en.wikipedia.org/wiki/Spherical_law_of_cosines

As far as using it for Lat and Long, I have to give a little credit here on my source:

https://maftahur.wordpress.com/2010/03/30/finding-distance-between-two-points-given-in-latitude-and-longitude-points/

So, below is your calculation for Spherical Low of Cosine. D will be your distance and R will be your unit of measurement. For example, 

D = acos(sin(lat1)*sin(lat2)+cos(lat1)*cos(lat2)*cos(Δlong))*R

Below are a couple of good images from Lesson 5-10 of UCSMP Functions, Statistics, and Trigonometry, 3rd ed., © 2010 Wright Group/McGraw Hill (Found this on a comments section on a site and agreed that it is helpful

Image of Spherical Law of Cosines Theorem showing a triangle of points on a sphere. If ABC is a spherical triangle with arcs a, b, and c, then cos c = cos a cos b + sin a sin b cos C.

Now, here are your spherical triangle points on a globe.

Globe showing two different ways of placing a triangular set of points. One with the point being from inside the globe center with triangle drawn to surface and one with the point being north and the triangle drawn on globe surface.

It looks as if there are two different types of spherical triangles presented on the globe above. Triangle NAB is one laying on the globe where N starts at the Northern point. Then triangle OCD is starting from the center. For Lat and Long, you're gathering your coordinates from the center, but really this is plotting distance and points on the surface of your globe based on base points. For our planet, they've established the North and South pole as the top and the bottom. Then, the Equator, which is a center belt around planet between N and S and the Prime Meridian running from N to S.

Back to this here... D = acos(sin(lat1)*sin(lat2)+cos(lat1)*cos(lat2)*cos(Δlong))*R

Let's break it down and see what happens trying to find the distance between two points. How about from Dallas to Seattle?

Dallas: 32.77815,-96.7954

Lat: 32.77815
Long: -96.7954

Seattle: 47.60357,-122.3295

Lat: 47.60357
Long: -122.3295

Δlong = (long2 - long1)

(Radius of Earth in Miles)
R = 3,959

D = acos(sin(32.77815)*sin(47.60357)+cos(32.77815)*cos(47.60357)*cos(-25.5341))*3959

D = acos(0.541387617*0.738497354+0.840773125*0.674256374*0.902328903)*3959

D = acos(0.911340545)*3959

D = 0.424267423*3959

D = 1679.67473 Miles

So... is it correct?

Let's put this in a jsfiddle. I found some similar code online and went from there.

https://jsfiddle.net/mferanda/nshx8qov/

The answer to the question is, it seems to be difficult to really verify. Perhaps someone knows the best site for the highest accuracy for this effort.

Great! So what does this have to do with Arduino? Well, I’ve been messing with a GPS device hooked up to a NodeMCU. Cool/fun stuff. I also utilized the compass in order to make lights change color based on getting closer to North. As for some type of official project with it, I’m not 100% sure just yet. However, there is a little thought about making some kind of crazy tech-y walking staff that’ll provide GPS / Direction but do so being fueled by an alternative energy source.