0

I am building a map-based xamarin app. I have a list of coordinates I save in a database. I want to query all of the coordinates that fall within x meters from a given starting coordinate. I just wanted to know how I can get the offset I need for the latitude and the longitude to formulate my query. I know how to get the distance between to coordinates but most of the other searches I got returned the same formula. Any help would be appreciated. Thank you very much!

I've checked out the following posts which were suggested as I made this post:

how to check radius of 10 meter from x,y position - from GPS

Calculating gps coordinate radius

EDIT

To specify the requirements further

I want to be able to end up with 2 ranges of numbers that I can use for a query like this

"SELECT * FROM tbl_Coordinates WHERE Lat >= {MIN_LAT_OFFSET} AND Lat <= {MAX_LAT_OFFSET} AND Lng >= {MIN_LNG_OFFSET} AND Lng<={MIN_LNG_OFFSET}"

I want to avoid querying all the coordinates in the db and checking the distance of all of them to see if they are within the radius.

EDIT 2

A new suggestion popped up in stackoverflow that seems promising here

If you guys have a better/more accurate answer I'm open to them. Thanks!

Kaizer69
  • 373
  • 4
  • 18
  • What DB? There are ones with geospatial capabilities: e.g. [MSSQL](https://learn.microsoft.com/en-us/sql/relational-databases/spatial/create-construct-and-query-geography-instances), [MongoDB](https://docs.mongodb.com/manual/geospatial-queries/) – Fildor Mar 19 '18 at 14:25
  • Hi Fildor, I'm using SQL Server 2012 but I'd rather do this in code if possible. Thanks. – Kaizer69 Mar 19 '18 at 16:20
  • Sorry I don't understand how your requirement is different to the links, or as you mention " I know how to get the distance between to coordinates but most of the other searches I got returned the same formula." - what do you want? – Jim W Mar 19 '18 at 16:54
  • Do you mean that you want an algorithm that gives you a list of coordinates within a range, and then you intend to use those coords to see if you have a match? I think that's the wrong way around. – Jim W Mar 19 '18 at 16:55
  • The requirement is I need the offset i.e. how much do I need to add or subtract from the lat and lng to get a proper radius search on a db since I'm searching against a db and I want to avoid querying all the coordinates in the db and checking the distance of all of them to see if they are within the radius. I want to be able to end up with 2 ranges of numbers that I can use for a query like this "SELECT * FROM tbl_Coordinates WHERE Lat >= {MIN_LAT_OFFSET} AND Lat <= {MAX_LAT_OFFSET} AND Lng >= {MIN_LNG_OFFSET} AND Lng<={MIN_LNG_OFFSET}" – Kaizer69 Mar 19 '18 at 20:26

1 Answers1

0

To anyone who might come across this, the answer I was looking for came from here

The answer gave this formula for computing how many KM/degree in latitude and longitude

Latitude = 10000/90 kilometers Longitude = 10000/90 km * cos(latitude)

and I ended up with this code where iLat and iLng are the center coordinates

//This is in meters
        double iDistanceDegreeConverter = 10000.0 / 90.0 * 1000;
        double iLatOffset = iRadius / iDistanceDegreeConverter;
        double iLngOffset = iRadius / (Math.Cos(iLat) * iDistanceDegreeConverter);
        double iMaxLat = Math.Abs(iLat) + iLatOffset;
        double iMinLat = Math.Abs(iLat) - iLatOffset;
        double iMaxLng = Math.Abs(iLng) + iLngOffset;
        double iMinLng = Math.Abs(iLng) - iLngOffset;
        if (iLat < 0)
        {
            iMaxLat *= -1;
            iMinLat *= -1;
        }
        if (iLng < 0)
        {
            iMaxLng *= -1;
            iMinLng *= -1;
        }
        if (iMaxLat > 90)
        {
            double iOverflow = iMaxLat - 90;
            iMaxLat = 90 - iOverflow;
        }
        if (iMinLat < -90)
        {
            double iOverflow = -90 + iMinLat;
            iMinLat = -90 + iOverflow;
        }
        if (iMaxLng > 180)
        {
            double iOverflow = iMaxLng - 180;
            iMaxLng = 180 - iOverflow;
        }
        if (iMinLng < -180)
        {
            double iOverflow = -180 + iMinLng;
            iMinLng = -180 + iOverflow;
        }
Kaizer69
  • 373
  • 4
  • 18