简体   繁体   English

在c#中以指定的半径获取经纬度的范围

[英]Get the Range of Lat Long with in specified Radius in c#

I am working on a website in which I have the User's Location Lat and Long saved in my Location table.我正在一个网站上工作,我在我的位置表中保存了用户的位置纬度和经度。 Now I want to use a filter SearchByDistance which have values: 5km , 10km , 15km through which the user can get the results according to the specified Range.现在我想使用具有值的滤波器SearchByDistance:5公里,10公里1公里,且通过它,用户可以根据规定的范围得到的结果。

Now what I actually wants to do is to get the input from the user say 5km and get the results from my table which falls with in the range of user's saved LatLong in the Location table and 5km to that LatLong .现在我真正想要做的是从用户那里获取发言权5公里输入,并从我的表,该表与用户保存的LatLong位置表和5公里到该LatLong网范围内下降的结果。 I google on this and found some links like:我谷歌在此发现了一些链接,如:

How do I find the lat/long that is x km north of a given lat/long 如何找到一个给定的纬度/长是X公里的北经/纬

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

But I am unable to get my requirement from the above.但我无法从上面得到我的要求。 Please help.请帮忙。 Thanks谢谢

I think your approach can be, first create a circle (your center will be user lat and long) with the given radius say 5KM or 10Km and then find the rows from the Polygon Rings.我觉得你的方法可能是,先创建一个圆(你的中心将是用户lat和长)利用指定的半径5公里讲或10公里,然后找到多边形环行。 I wrote it for esri maps.我写了ESRI地图。 Hope it will solve your problem希望这本书能解决你的问题

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};
}

Now next task is to find the rows.现在,下一个任务是找到行。 For that you can use below code inside the loop.对于您可以使用下面的代码在循环中。

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;
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM