Finding Distance between two points using longitude and latitude

This is a method that calulates a distance between two points. Let me know if you have a more acurate formula.

public static bool ZipDistance(double Lat1, double Lon1, double Lat2, double Lon2, ref double result)

    {

        double LatRad1;

        double LonRad1;

        double LatRad2;

        double LonRad2;

        double LonRadDif;

        double RadDist;

        double X;

        double PI;

        double DistMI;

        PI = 3.141592654;

        if (Lat1 == 0 | Lon1 == 0 | Lat2 == 0 | Lon2 == 0)

        {

            return false;

        }

        else if (Lat1 == Lat2 & Lon1 == Lon2)

        {

            return false;

        }

        LatRad1 = Lat1 * PI / 180;

        LonRad1 = Lon1 * PI / 180;

        LatRad2 = Lat2 * PI / 180;

        LonRad2 = Lon2 * PI / 180;

        try

        {

            LonRadDif = Math.Abs(LonRad1 – LonRad2);

            X = Math.Sin(LatRad1)*Math.Sin(LatRad2) + Math.Cos(LatRad1)*Math.Cos(LatRad2)*Math.Cos(LonRadDif);

            RadDist = Math.Atan(-X/Math.Sqrt(-X*X + 1)) + 2*Math.Atan(1);

            DistMI = RadDist*3958.754;

            result = DistMI;

        }catch(Exception)

        {

            return false;

        }

        return true;

    }

Advertisement

One Response

  1. well done, dude

Leave a Reply

Fill in your details below or click an icon to log in:

Gravatar
WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Connecting to %s

Follow

Get every new post delivered to your Inbox.