Calculating the distance between 2 points in c#

此生再无相见时 提交于 2019-11-30 05:17:23

问题


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


回答1:


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



回答2:


double lat1 = {};
double lat2 = {};
double lon1 = {};
double lon2 = {};

var R = 6376.5000; //Km
lat1 = lat1.ToRad();
lat2 = lat2.ToRad();
lon1 = lon1.ToRad();
lon2 = lon2.ToRad();
var dLat = lat2 - lat1;
var dLon = lon2 - lon1;
var a = Math.Pow(Math.Sin(dLat / 2), 2) + (Math.Pow(Math.Sin(dLon / 2), 2) * Math.Cos(lat1) * Math.Cos(lat2));
var c = 2 * Math.Atan2(Math.Sqrt(a), Math.Sqrt(1 - a));
var distance = R * c;

public double ToRad(this double degs) {
    return degs * (Math.PI/180.0);
}

Input expects doubles.

This is the haversine formula, it's used to calculate the distances on our globe between two points. This is the distance in a straight line, if you need the distance on a path you will have to find all points on that path and then calculate the distances between each two points and then take the sum of that.




回答3:


You can use DbGeography for spatial calculation. It has DbGeography.Distance method which is used to calculate the distance between two gps points.

Otherwise, try Ref: Harversine Formula to calculate the distance between two points.




回答4:


Distance Formula: Given the two points (x1, y1) and (x2, y2), the distance between these points is given by the formula:

use it accordingly



来源:https://stackoverflow.com/questions/22686161/calculating-the-distance-between-2-points-in-c-sharp

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!