0

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

Community
  • 1
  • 1
user3124690
  • 413
  • 1
  • 12
  • 25
  • https://stackoverflow.com/questions/19478874/find-all-nearby-customers-within-a-given-distance-using-longitude-and-latitude – Phonix Sep 18 '17 at 10:50

1 Answers1

2

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;
}
Mohamad Mousheimish
  • 1,641
  • 3
  • 16
  • 48
Mukesh Rawat
  • 2,047
  • 16
  • 30
  • Your code is not upto my requirement. In the second piece of code you are finding the User's Location, but I already have the User's current location(LatLong) saved in my Table. My requirement is to just plus the radius say 5km in the User's LatLong and find the rows that falls between these LatLong range. I hope I am able to explain it clearly – user3124690 Feb 06 '14 at 10:00
  • I have applied your code my not success. Is this not possible to plus the user's input in the User's LatLong saved in the db. I this we will get the range of MinLatLong and MaxLatLong. With this we can easily fire the query in LINQ to get the no of records that falls within these two ranges – user3124690 Feb 06 '14 at 12:49