【问题标题】:Measure distance by longitude and lattitude [duplicate]按经度和纬度测量距离[重复]
【发布时间】:2015-10-01 19:13:31
【问题描述】:

首先,我什至不确定这是否是最好的方法,但是...

我收集了一张美国所有邮政编码的经度和纬度点表。我想要做的是允许用户选择一个邮政编码,选择一个以英里为单位的半径(5、10、20、40 等......),应用程序将列出该半径内的所有用户。

它显然不需要非常准确,但它必须接近。我一直在四处寻找其他方法来做到这一点,但我很困惑,我找不到使用 long/lat 来做这件事的好例子。

如果我能在 C# 中得到一些效果最好的东西。我不精通Java,但如果绝对必要,我可能会应付。

编辑:

我的坐标是这样的:

CountryCode Zipcode Place   StateCode   Latitude    Longitude
US          95219   Stockton     CA        38.01    -121.3698
US          95220   Acampo       CA      38.2004    -121.2186
US          95227   Clements     CA      38.1929    -121.0811
US          95230   Farmington   CA      37.9945    -120.7926
US          95231   French Camp  CA       37.878    -121.2827
US          95234   Holt         CA      37.9344    -121.4261
US          95236   Linden       CA       38.032    -121.0493

这个问题不是重复的,链接的问题是针对电话的。

【问题讨论】:

  • 请注意,如果您可以将纬度/经度转换为东/北,这几乎是微不足道的。
  • @oppassum - 这是一部手机,我假设它内置了 GeoCoordinates 功能?
  • 也许我理解的不正确,但是你的问题是找到,比如说,所有坐标,相对于所选位置,某个半径?
  • 不知何故,建议的线程正在计算两个 lang/long 之间的距离,但没有在选定的半径内找到所有它们。也许这个链接可以帮助你:janmatuschek.de/LatitudeLongitudeBoundingCoordinates
  • 嗯,我想要的结果是找到所有居住在您半径范围内的用户,给定您的邮政编码。仍然不确定查找距离的效率如何,我将不得不转换每个用户以查找给定范围内的 c,这听起来非常低效。我想我必须在 SQl Server 中执行此操作。

标签: c# asp.net code-behind


【解决方案1】:

以下代码会生成此 Wgs84Point 实例与另一个实例之间的距离。距离是假设一个完美的球形地球给出的,并且没有考虑地球的不规则形状。

public class Wgs84Point
{
    const double MaxDegreesLongitude = 180;
    const double MinDegreesLongitude = -180;
    const double MaxDegreesLatitude = 90;
    const double MinDegreesLatitude = -90;

    readonly double _longitude;
    readonly double _latitude;

    public double Latitude { get { return _latitude; } }

    public double Longitude { get { return _longitude; } }

    public Wgs84Point(double longitude, double latitude)
    {
        if (longitude > MaxDegreesLongitude || longitude < MinDegreesLongitude)
            throw new ArgumentException("longitude");

        if (latitude > MaxDegreesLatitude || latitude < MinDegreesLatitude)
            throw new ArgumentException("latitude");

        _longitude = longitude;
        _latitude = latitude;
    }

    public Distance DistanceTo(Wgs84Point that)
    {
        if (that == null)
            throw new ArgumentNullException("that");

        if (this == that)
            return Distance.Zero;

        var dLat = DegreesToRadians(Latitude - that.Latitude);
        var dLon = DegreesToRadians(Longitude - that.Longitude);
        var a = Math.Sin(dLat / 2) * Math.Sin(dLat / 2) +
            Math.Cos(DegreesToRadians(Latitude)) * 
            Math.Cos(DegreesToRadians(that.Latitude)) *
            Math.Sin(dLon / 2) * Math.Sin(dLon / 2);
        var c = 2 * Math.Atan2(Math.Sqrt(a), Math.Sqrt(1 - a));
        var d = Distance.RadiusOfEarth.ToDouble() * c;
        return new Distance(d);
    }

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

【讨论】:

  • 使用这个的语法是什么?假设,在我添加到上面编辑的表格中,我想找到从斯托克顿到霍尔特的距离。我会使用什么语法?
  • 表?您的问题的 [原始版本] 没有提到持久性机制。您必须使用 EF、DataReader 或 DataSet 构建 C# 类,并从那时起使用上述代码。或者,查找上述代码的 SQL 实现。
猜你喜欢
  • 2016-10-11
  • 1970-01-01
  • 1970-01-01
  • 2014-01-01
  • 1970-01-01
  • 2011-10-01
  • 1970-01-01
  • 2014-12-16
  • 2018-04-01
相关资源
最近更新 更多