Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
421 views
in Technique[技术] by (71.8m points)

latitude longitude - Calculating the distance between 2 points in c#

I am trying to sort out a method to calculate the distance between 2 points in c#.

This is the code I have been trying though I fear the answer I get is not correct.

static void Main()
    {
        //postcode australia 2600 -> 3000
        float latA = -31.997976f;
        float longA = 115.762877f;
        float latB = -31.99212f;
        float longB = 115.763228f;
        decimal distance = (DistanceBetween(latA, latB, longA, longB));
        Console.WriteLine("Distance is" + distance);
        Console.ReadLine();
    }

    static decimal DistanceBetween(float latA, float longA, float latB, float longB)
    {
        var RadianLatA = Math.PI * latA / 180;
        var RadianLatb = Math.PI * latB / 180;
        var RadianLongA = Math.PI * longA / 180;
        var RadianLongB = Math.PI * longB / 180;

        double theDistance = (Math.Sin(RadianLatA)) *
                Math.Sin(RadianLatb) +
                Math.Cos(RadianLatA) *
                Math.Cos(RadianLatb) *
                Math.Cos(RadianLongA - RadianLongB);

        return Convert.ToDecimal(((Math.Acos(theDistance) * (180.0 / Math.PI)))) * 69.09M * 1.6093M;
    }

this was adapted from a response found on this site here Distance between addresses

Any thoughts on what is going wrong/

Thanks Ryan

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

The class I usually use is GeoCoordinate

double latA = -31.997976f;
double longA = 115.762877f;
double latB = -31.99212f;
double longB = 115.763228f;

var locA = new GeoCoordinate(latA, longA);
var locB = new GeoCoordinate(latB, longB);
double distance = locA.GetDistanceTo(locB ); // metres

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...