在C#中获取具有指定半径的纬度经度范围 [英] Get the Range of Lat Long with in specified Radius in c#

查看:201
本文介绍了在C#中获取具有指定半径的纬度经度范围的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在一个网站上,我的 Location 表中保存了用户的经纬度和经度.现在,我想使用一个过滤器 SearchByDistance ,该过滤器的值包括: 5km 10km 15km 根据指定的范围获得结果.

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.

现在我真正想做的是从用户说 5km 处获取输入,并从我的表中获取结果,该结果落在用户保存的 LatLong Location 表中,并且距该 LatLong 5公里.我在Google上进行了搜索,发现了一些链接,例如:

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. I google on this and found some links like:

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

But I am unable to get my requirement from the above. Please help. Thanks

推荐答案

我认为您的方法可以,首先创建一个半径为5KM或10Km的圆(您的中心将为用户经度和纬度),然后找到多边形环的行.我是为esri地图写的.希望它能解决您的问题

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. I wrote it for esri maps. 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;
}

这篇关于在C#中获取具有指定半径的纬度经度范围的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆