在c#中以指定的半径获取Lat long的范围

时间:2014-02-06 08:40:53

标签: c# geolocation geometry

我正在一个网站上工作,我将用户的位置Lat和Long保存在我的位置表中。现在我想使用过滤器 SearchByDistance ,其值为: 5km 10km 15km ,用户可通过根据指定的范围获取结果。

现在我真正想做的是让用户输入 5km 并从我的表中获得结果,该结果属于用户保存的范围 LatLong 位置表格中,距离 LatLong 5公里。我在谷歌上发现了一些链接,如:

How do I find the lat/long that is x km north of a given lat/long

http://msdn.microsoft.com/en-us/library/system.device.location.geocoordinate.getdistanceto%28v=vs.110%29.aspx

但是我无法从上面得到我的要求。请帮忙。感谢

1 个答案:

答案 0 :(得分:2)

我认为您的方法可以是,首先创建一个圆(您的中心将是用户lat和long),给定半径为5KM或10Km,然后从Polygon Rings中找到行。我为esri地图写了它。希望它能解决你的问题

Polygon areaOfInterset;

void CreateCircle(double radius)
        {
            var point = new MapPoint(Your Lat and Long);// This will be user lat and long on which you want to draw a circle

            var graphic = new Graphic();
            var circle = new ESRI.ArcGIS.Client.Geometry.PointCollection();
            int i = 0;

            while (i <= 360)
            {
                double degree = (i * (Math.PI / 180));

                double x = point.X + Math.Cos(degree) * radius;
                double y = point.Y + Math.Sin(degree) * radius;
                circle.Add(new MapPoint(x, y));
                i++;
            }
            var rings = new ObservableCollection<ESRI.ArcGIS.Client.Geometry.PointCollection> { circle };
            areaOfInterset = new Polygon { Rings = rings};


        }

现在接下来的任务是找到行。为此,您可以在循环内使用以下代码。

foreach(MapPoint selectedRow in DatabaseRowsToBeSearched)
{
   var isExist = IsInsideCircle(selectedRow)
}

bool IsInsideCircle(MapPoint location)
        {
            var isInside = false;
            foreach (var shape in areaOfInterset.Rings)
                {
                    for (int i = 0, j = shape.Count - 1; i < shape.Count; j = i++)
                    {
                        if ((((shape[i].Y <= location.Y) && (location.Y < shape[j].Y)) ||
                            ((shape[j].Y <= location.Y) && (location.Y < shape[i].Y))) &&
                            (location.X < (shape[j].X - shape[i].X) * (location.Y - shape[i].Y) / (shape[j].Y - shape[i].Y) + shape[i].X))
                        {
                            isInside = !isInside;
                        }
                    }
                }

            return isInside;
        }