【问题标题】:How to determine nearest co-ordinate如何确定最近的坐标
【发布时间】:2014-01-25 12:15:29
【问题描述】:

我正在开发 Windows Phone 8 应用程序,并且正在执行基于位置的搜索。我正在使用这个example 来查找我当前工作正常的位置。我还有一个纬度和经度坐标列表。我想要做的是从这个经纬度坐标列表中找出最接近我当前位置的位置。我也可能会修改它以找出最接近的 5 或 10 或类似的东西。

听起来应该很容易找到,但我不知道该怎么做。

如何找出最接近另一个坐标的坐标?

任何帮助将不胜感激。

谢谢!

【问题讨论】:

  • 您可以使用Haversine 公式计算两点之间的距离(具有经度和纬度)。在计算之后,很容易按到所需位置的距离对您的点进行排序。

标签: c# windows-phone-8 geolocation location


【解决方案1】:

实际距离需要测地线函数:

http://en.wikipedia.org/wiki/Great-circle_distance

这是相当昂贵的,因此您希望先使用另一个功能进行过滤,该功能可帮助您减少候选者并稍后在代码中对其进行排序。

在此之前的传递中,您可以使用欧几里得距离:

http://en.wikipedia.org/wiki/Euclidean_distance

这种两遍方法大大降低了计算成本(如果需要,可以降低 10,000 倍),并在 Programming Pearls(第 8 章)中进行了描述:

http://www.cs.bell-labs.com/cm/cs/pearls/

【讨论】:

  • 我不能只使用msdn.microsoft.com/en-us/library/… 并按升序对结果进行排序吗?
  • 绝对是的!您甚至应该这样做,因为这种实现肯定比您可以实现的任何东西都要好。但“内部”仍将基于复杂的三角学。上述 2-pass 方法是一种性能改进,可能适用于您的情况,也可能不适用于您的情况。
  • 谢谢我使用了GetDistanceTo这个方法,它似乎工作正常。
【解决方案2】:

由于您的距离可能很短(例如,

// Convert Degress to Radians
//
private static double Deg2Rad( double deg ) 
{
   return deg * Math.PI / 180;
}

// Get Distance between two lat/lng points using the PythagorsTheorm (a*a = (b*b + c*c))
// on an equirectangular projection
//
private double PythagorasEquirectangular( Geoposition coord1, Geoposition coord2 )
{
    double lat1 = Deg2Rad( coord1.Coordinate.Latitude );
    double lat2 = Deg2Rad( coord2.Coordinate.Latitude );
    double lon1 = Deg2Rad( coord1.Coordinate.Longitude );
    double lon2 = Deg2Rad( coord2.Coordinate.Longitude );

    double R = 6371; // km
    double x = (lon2-lon1) * Math.Cos((lat1+lat2)/2);
    double y = (lat2-lat1);
    double d= Math.Sqrt(x*x + y*y) * R;
    return d;
}

// Find the closest point to your position
//
private Geoposition NearestPoint( List<Geoposition> points, Geoposition position  )
{
    double min_dist = 999999;
    Geoposition closest = null;

    // Calculate distance of each point in the list from your position
    foreach ( Geoposition point in points )
    {
        double dist = PythagorasEquirectangular( position, point );

        // keep track of which point is the current closest.
        if ( dist < min_dist )
        {
            min_dist = dist;
            closest = point;
        }
    }

    // return the closest point
    return closest;
}

【讨论】:

  • 不知道为什么有人会在没有解释的情况下投反对票,因为这通常是短距离的例外方法,我使用内置的地理编码库将其调整为 C#。
【解决方案3】:

赤道处的地球半径 = 6,371 公里。赤道被划分为 360 度经度,因此赤道处的每一度代表大约 111.32 公里。从赤道向极点移动,此距离在极点处减小到零。要计算不同纬度的距离,请将其乘以纬度的余弦

3 位小数,0.001 度近似为 赤道111.32米
北纬 30 度时 96.41 米
北纬 45 度时 78.71 米
北纬 60 度时 55.66 米
28.82 米,北纬 75 度

对于小距离(100 米),毕达哥拉斯定理可用于equirectangular projection 来计算距离。这比Haversine 或Spherical Law of Cosines 简单,但仍然允许向极点收敛。

变量 R = 6371; // 公里
以弧度表示的纬度/经度

在伪代码中,因为我不懂 C#

var x = (lng2-lng1) * cos((lat1+lat2)/2);
var y = (lat2-lat1);
var d = sqrt(x*x + y*y) * R;

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2022-11-17
    • 1970-01-01
    • 1970-01-01
    • 2013-06-28
    • 1970-01-01
    • 2014-09-07
    • 1970-01-01
    • 2012-06-02
    相关资源
    最近更新 更多