Sam Salman
Sam Salman

Reputation: 33

find Distance by latitude and longitude

I am just stuck in the strange calculation.

I am trying to find distance by latitude and longitude by using this formula mentioned in this site.

http://www.movable-type.co.uk/scripts/latlong.html

every thing is Ok there when I do it by calculator but when I started doing it by programmatically there is a difference in result.

here is my part of code

lat = Convert.ToDouble( (s3[a]));
                longt =Convert.ToDouble( (s4[a]));
                Avg_lat = pc._Latitude - lat;
                Avg_longt = pc._Longitude - longt;


            a_hold =( Math.Sin(Avg_lat / 2) * Math.Sin(Avg_lat / 2) )+ (Math.Cos(pc._Latitude) * Math.Cos(lat)) * (Math.Sin(Avg_longt / 2) * Math.Sin(Avg_longt / 2));
            c_hold = 2 * Math.Atan2(Math.Sqrt(a_hold), Math.Sqrt(1 - a_hold));
           // c_hold = (c_hold * 180) / 3.12;
            distance = 6371 * c_hold;

the problem is Math funcation here calculate the things in radian I want it in degree. so I just found a formula to convert radian into degree but that formula doesn't convert the calculation correctly.

I am just confused what to do bcoz there isn't any other option to convert radian into degree..

Upvotes: 3

Views: 485

Answers (3)

reprogrammer
reprogrammer

Reputation: 14728

You can find a Java implementation of a function that computes the distance between two latitude-longitude pairs in LinkedIn's datafu project.

Upvotes: 1

PleaseStand
PleaseStand

Reputation: 32122

Are you converting correctly and in all places necessary? The correct radians-to-degrees formula is to multiply by (180.0 / Math.PI) and the degrees-to-radians formula is to multiply by (Math.PI / 180.0).

You need to make a degrees-to-radians conversion on the arguments to Math.Sin and Math.Cos. You would need to make a radians-to-degrees conversion on the return value of Math.Atan2 if you are trying to simulate the output of a scientific calculator in degrees mode, but the formula from the web page you linked to says to not do so (only convert the inputs).

Upvotes: 1

winwaed
winwaed

Reputation: 7829

Your calculator will have a radians option, so you verify your conversion.

Radians = Degrees * PI / 180

Upvotes: 0

Related Questions