Continue to Site

Welcome to our site!

Electro Tech is an online community (with over 170,000 members) who enjoy talking about and building electronic circuits, projects and gadgets. To participate you need to register. Registration is free. Click here to register now.

  • Welcome to our site! Electro Tech is an online community (with over 170,000 members) who enjoy talking about and building electronic circuits, projects and gadgets. To participate you need to register. Registration is free. Click here to register now.

Distance Calculation by Lattitude/Longitude

Status
Not open for further replies.
We have a in-house script at my work that calculates the ground range based on two lat/long coordinates. Sounds like what you're looking for. I'm not free to distribute our code (it's proprietary), but I can tell you it's based on the equations in the book Astronomical Algorithms by Jean Meeus, 2nd Edition. Pages 84-85.

Good luck.
 
[URL="https://www.movable-type.co.uk/scripts/latlong.html"]https://www.movable-type.co.uk/scripts/latlong.html[/URL]

This formula works for any two points anywhere in the world:-
r * arccos[sin(lat1) * sin(lat2) + cos(lat1) * cos(lat2) * cos(lon2 - lon1)]

It is quite complicated to program for a microcontroller. For short distances, to get accurate distances you need extreme accuracy in the calculation.

If you want to calculate short distances, say up to 1000 miles, and you don't need to calculate distances near the north or south pole, the following formula is simpler and is often more accurate on a microcontroller:-

2 * π * r * √ ( (lat1 - lat2)² + (cos ((lat 1 + lat2)/2) * (lon1 - lon2))²)
 
I think for short distances (<500 miles?) you can simplify that a LOT.

You can apply a single latitude "factor" to scale the coords east-west, then act as if the 2 coords are on a XY grid. If the north-south (Y) distance between the 2 coords is small the east-west scaling factor will be good enough to use for both, ie you get a + and - X error which cancel out. And obviously for short distances you can assume the world is "flat" so north-south degrees directly scales to Y distance.

Since a lot of "distance" calcs using microcontrollers and GPS are very local (like how far did my 'plane just fly) this should work fine.
 
Last edited:
That is really what I did. I only applied one scaling factor for the east-west difference.

The only complication was to take the average latitude as the basis for that. In many cases that is overkill, but average of two values is very easy and quick on a microcontroller, so it is not a big overhead. It is certainly easier than trying to find the arccos accurately for numbers larger than 0.9999 that many calculations need. For some applications, for instance if you are confining yourself to one small country or one US state, a fixed scaling factor would be fine.

To break what I suggested down it is:-

Find the average latitude.
Take the cosine of that, and store as the scaling factor.
Find the difference in longitude.
Multiply by the scaling factor
Square the result and store.
Find the difference in longitude
Square the difference
Add to the previous square.
Take the square root of the sum of the squares.
Multiply by the circumference of the earth.

I don't know if the OP is interested, but I have code to do that on a pic18 or on a pic24
 
i have few questions
1.The lattitude and longitude i get from gps is in deg or rad ??
i.e N 42 59.458 W 71 27.826
2.Diver30 tell me in your formula latt/long is in rad or deg??
 
Last edited:
@Diver what is n ?? in this formula and if you have a code for distance calculation than please share.
2 * π * r * √ ( (lat1 - lat2)² + (cos ((lat 1 + lat2)/2) * (lon1 - lon2))²)
 
hi,
This may help.
**broken link removed**

If its only for one distance measurement have you considered using Google Earth's ruler.. I do.

**broken link removed**
 
Last edited:
how to convert gps coordinates to decimal degrees if some one know the formula than please share i am unable to convert gps coordinates to decimal degrees .
thankss
 
i am unable to calculate distance with this formula
from calculator pi/180=0.01745329252
from uc pi/180=0.017460318
how to remove this error???
 
i am unable to calculate distance with this formula
from calculator pi/180=0.01745329252
from uc pi/180=0.017460318
how to remove this error???

Try using higher precision variables and maths routines (if you have them), presumably you're failing prey to rounding errors?, but it's with in something like 0.001% anyway.
 
here is some part of my code.
Code:
float32 dlat,dlon,lat1,lat2,a,c,d,r,d2r,pii,lon1,lon2,cosl1,sin1,cosl2,sin2,ang,tmp,tmp2;
lat1=42.990967;
lat2=42.990967;
lon1=71.463767;
lon2=71.463100;


pii=3.142857142857143;
d2r=(pii/180);
r = 6371; // km
dLat = (lat2-lat1)*d2r;
dLon = (lon2-lon1)*d2r;
lat1 = lat1*d2r;
lat2 = lat2*d2r;
 
Last edited:
Why don't you define PI/180 as the correct float.... What float are you using 24,32 or 64 bit . I remember that C18 uses 32 bit... under certain conditions it would be hard to maintain 10 decimal places ( it usually averages out 3 bits per decimal place ).
 
Status
Not open for further replies.

Latest threads

New Articles From Microcontroller Tips

Back
Top