简体   繁体   中英

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.

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:

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

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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