地图坐标C#

时间:2016-07-12 11:08:51

标签: c# geolocation geometry

我有一组纬度经度点。如果我想测试一个新点是否在任何现有点的x米范围内,那么这是可能的吗? 如果我这样使用它会是最好的吗?

ForEach(Coordinate coord in Coordinates)
{
     var distance =  GeoCoordinate.GetDistance(lat,lon);
     if(distance <= x)
     {
         addToQualifyingList(coord);
     }
}

并将新坐标与集合中的每个点进行比较并检查它是否在x米范围内?

2 个答案:

答案 0 :(得分:2)

这是一种计算2点之间距离的方法(lat1,lon1和lat2,lon2)

public enum DistanceUnit { miles, kilometers, nauticalmiles }
public double GetDistance( double lat1, double lon1 , double lat2 , double lon2, DistanceUnit unit)
{
    Func<double, double> deg2rad = deg => (deg * Math.PI / 180.0);
    Func<double, double> rad2deg = rad => (rad / Math.PI * 180.0);
    double theta = lon1 - lon2;
    double dist = Math.Sin(deg2rad(lat1)) * Math.Sin(deg2rad(lat2)) + Math.Cos(deg2rad(lat1)) * Math.Cos(deg2rad(lat2)) * Math.Cos(deg2rad(theta));
    dist = Math.Acos(dist);
    dist = rad2deg(dist);
    dist = dist * 60 * 1.1515;

    if (unit == DistanceUnit.kilometers)
    {
        dist = dist * 1.609344;
    }
    else if (unit == DistanceUnit.nauticalmiles)
    {
        dist = dist * 0.8684;
    }
    return (dist);
}

确定距离低于1公里的所有坐标:

List<Coordinate> result = Coordinates.Where(x => GeoCoordinate.GetDistance(lat,lon, x.lan, x.lon, DistanceUnit.kilometers) < 1).ToList();

答案 1 :(得分:0)

纬度和经度都需要知道地球上的位置或任何其他球形物体。它们以各自的零线/点的度数给出。

http://www.movable-type.co.uk/scripts/latlong.html

有Java解决方案

>    var R = 6371e3; // metres
>     var φ1 = lat1.toRadians();
>     var φ2 = lat2.toRadians();
>     var Δφ = (lat2-lat1).toRadians();
>     var Δλ = (lon2-lon1).toRadians();
> 
>     var a = Math.sin(Δφ/2) * Math.sin(Δφ/2) +
>             Math.cos(φ1) * Math.cos(φ2) *
>             Math.sin(Δλ/2) * Math.sin(Δλ/2);
>     var c = 2 * Math.atan2(Math.sqrt(a), Math.sqrt(1-a));
> 
>     var d = R * c;

显然,这是小距离的最佳解决方案之一。

您应该可以将其复制并粘贴到C#,您必须更改变量名称。