如何确定两组纬度/经度坐标之间的距离?

时间:2010-01-06 01:44:45

标签: c# distance latitude-longitude

我正在尝试写一些能确定纬度/经度坐标之间距离的东西。

我使用的是this site上的以下代码:

public static double distance (double lat1, double lon1, double lat2, double lon2) { 
    double lat1 = Convert.ToDouble(latitude);
    double lon1 = Convert.ToDouble(longitude);
    double lat2 = Convert.ToDouble(destlat);
    double lon2 = Convert.ToDouble(destlon);

    double theta = toRadians(lon1-lon2); 
    lat1 = toRadians(lat1); 
    lon1 = toRadians(lon1); 
    lat2 = toRadians(lat2); 
    lon2 = toRadians(lon2); 

    double dist = sin(lat1)*sin(lat2) + cos(lat1)*cos(lat2)*cos(theta); 
    dist = toDegrees(acos(dist)) * 60 * 1.1515 * 1.609344 * 1000; 

    return dist; 
} 

我的问题是我遇到编译错误“当前上下文中不存在名称'toRadians'/'cos'/'sin /'toDegrees'”我做错了吗?

6 个答案:

答案 0 :(得分:25)

您可能希望使用以下C#类:

public static class GeoCodeCalc
{
    public const double EarthRadiusInMiles = 3956.0;
    public const double EarthRadiusInKilometers = 6367.0;

    public static double ToRadian(double val) { return val * (Math.PI / 180); }
    public static double DiffRadian(double val1, double val2) { return ToRadian(val2) - ToRadian(val1); }

    public static double CalcDistance(double lat1, double lng1, double lat2, double lng2) 
    {
        return CalcDistance(lat1, lng1, lat2, lng2, GeoCodeCalcMeasurement.Miles);
    }

    public static double CalcDistance(double lat1, double lng1, double lat2, double lng2, GeoCodeCalcMeasurement m) 
    {
        double radius = GeoCodeCalc.EarthRadiusInMiles;

        if (m == GeoCodeCalcMeasurement.Kilometers) { radius = GeoCodeCalc.EarthRadiusInKilometers; }
        return radius * 2 * Math.Asin( Math.Min(1, Math.Sqrt( ( Math.Pow(Math.Sin((DiffRadian(lat1, lat2)) / 2.0), 2.0) + Math.Cos(ToRadian(lat1)) * Math.Cos(ToRadian(lat2)) * Math.Pow(Math.Sin((DiffRadian(lng1, lng2)) / 2.0), 2.0) ) ) ) );
    }
}

public enum GeoCodeCalcMeasurement : int
{
    Miles = 0,
    Kilometers = 1
}

用法:

// Calculate Distance in Miles
GeoCodeCalc.CalcDistance(47.8131545175277, -122.783203125, 42.0982224111897, -87.890625);

// Calculate Distance in Kilometers
GeoCodeCalc.CalcDistance(47.8131545175277, -122.783203125, 42.0982224111897, -87.890625, GeoCodeCalcMeasurement.Kilometers);

来源:Chris Pietschmann - Calculate Distance Between Geocodes in C# and JavaScript

答案 1 :(得分:5)

您可以像这样编写toRadians函数:

double ToRadians(double degrees) { return degrees * Math.PI / 180; }

您可以像这样编写toDegrees函数:

double ToDegrees(double radians) { return radians * 180 / Math.PI; }

您应该将sincos替换为Math.SinMath.Cos

答案 2 :(得分:2)

这看起来像C#。

首先,您需要定义toRadianstoDegrees

double toRadians(double degrees) {
    double sign = Math.Sign(degrees);
    while(Math.Abs(degrees) > 360) {
        degrees -= sign * 360;
    }
    return Math.PI * degrees / 180;
}

double toDegrees(double radians) {
    double sign = Math.Sign(radians);
    while(Math.Abs(radians) > 2 * Math.PI) {
        radians -= sign * 2 * Math.PI;
    }
    return 180 * radians / Math.PI;
}

然后,要使用三角函数,您需要使用Math.SinMath.Cos等。

double dist = Math.Sin(lat1) * Math.Sin(lat2)
                + Math.Cos(lat1) * Math.Cos(lat2) * Math.Cos(theta);

dist = toDegrees(Math.Acos(dist)) * 60 * 1.1515 * 1.609344 * 1000; 

评论:

public static double distance (double lat1, double lon1, double lat2, double lon2) { 
double lat1 = Convert.ToDouble(latitude);
double lon1 = Convert.ToDouble(longitude);
double lat2 = Convert.ToDouble(destlat);
double lon2 = Convert.ToDouble(destlon);
这是什么? latitudelongitudedestlatdestlon在哪里定义?此外,您似乎已将lat1lon1 lat2lon2作为此方法的参数,以便您无法在此处使用相同的名称定义本地化。

double theta = toRadians(lon1-lon2); 
lat1 = toRadians(lat1); 
lon1 = toRadians(lon1); 
lat2 = toRadians(lat2); 
lon2 = toRadians(lon2); 

这是糟糕的风格。如果lat1表示以度为单位的纬度,那么计算等于lat1的弧度等效值会好得多:

double lat1Radians = toRadians(lat1);

因此将以上内容替换为:

double theta = toRadians(lon1-lon2); 
double lat1Radians = toRadians(lat1); 
double lon1Radians = toRadians(lon1); 
double lat2Radians = toRadians(lat2); 
double lon2Radians = toRadians(lon2);

最后:

double dist = sin(lat1) * sin(lat2)
                + cos(lat1) * cos(lat2) * cos(theta); 
dist = toDegrees(acos(dist)) * 60 * 1.1515 * 1.609344 * 1000; 

这也是不好的风格。第一个公式和第二个公式不能同时代表您尝试计算的距离。您应该将第一个公式的结果分配给名称更有意义的变量。在最坏的情况下,至少要做以下事情:

double temp = Math.Sin(lat1) * Math.Sin(lat2)
                + Math.Cos(lat1) * Math.Cos(lat2) * Math.Cos(theta);
double dist = toDegrees(Math.Acos(dist)) * 60 * 1.1515 * 1.609344 * 1000; 

return dist;

答案 3 :(得分:2)

我知道这个问题真的很陈旧,但万一其他人偶然发现这个问题,请使用System.Device中的GeoCoordinate

var distanceInMeters = new GeoCoordinate(lat1, lon1)
    .GetDistanceTo(new GeoCoordinate(lat2, lon2));

答案 4 :(得分:1)

计算纬度和经度点之间的距离......

double Lat1 = Convert.ToDouble(纬度);

    double Long1 = Convert.ToDouble(longitude);

    double Lat2 = 30.678;
    double Long2 = 45.786;
    double circumference = 40000.0; // Earth's circumference at the equator in km
    double distance = 0.0;
    double latitude1Rad = DegreesToRadians(Lat1);
    double latititude2Rad = DegreesToRadians(Lat2);
    double longitude1Rad = DegreesToRadians(Long1);
    double longitude2Rad = DegreesToRadians(Long2);
    double logitudeDiff = Math.Abs(longitude1Rad - longitude2Rad);
    if (logitudeDiff > Math.PI)
    {
        logitudeDiff = 2.0 * Math.PI - logitudeDiff;
    }
    double angleCalculation =
        Math.Acos(
          Math.Sin(latititude2Rad) * Math.Sin(latitude1Rad) +
          Math.Cos(latititude2Rad) * Math.Cos(latitude1Rad) * Math.Cos(logitudeDiff));
    distance = circumference * angleCalculation / (2.0 * Math.PI);
    return distance;

答案 5 :(得分:0)

您需要稍微调整一下这段代码。

正如SLaks所说,您需要定义自己的toRadians()方法,因为.NET没有本机版本。

您还需要将对cos()和sin()的调用更改为:Math.Cos()和Math.Sin()