10

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

我正在使用在此站点上找到的以下代码:

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'......”我做错了什么?

4

6 回答 6

26

您可能想要使用以下 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 - 计算 C# 和 JavaScript 中地理编码之间的距离

于 2010-01-06T01:53:16.917 回答
5

你可以写一个toRadians这样的函数:

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

你可以写一个toDegrees这样的函数:

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

您应该将sinandcos替换为Math.Sinand Math.Cos

于 2010-01-06T01:47:51.330 回答
2

这看起来像 C#。

首先,您需要定义toRadiansand toDegrees

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.Sin,Math.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);

这是什么?latitudelongitudedestlat定义在哪里destlon?此外,您似乎拥有lat1,lon1 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;
于 2010-01-06T01:49:51.543 回答
2

我知道这个问题真的很老,但如果其他人偶然发现这个问题,请使用GeoCoordinatefrom System.Device

var distanceInMeters = new GeoCoordinate(lat1, lon1)
    .GetDistanceTo(new GeoCoordinate(lat2, lon2));
于 2014-12-18T11:51:58.400 回答
1

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

双 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;
于 2016-03-17T08:00:45.113 回答
0

您将需要稍微调整此代码。

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

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

于 2010-01-06T01:51:37.403 回答