0

I have an SQLITE database that stores the coordinates of places as float numbers.

PLACE TABLE
--------------------------------
placeID  | latitude | longitude
--------------------------------
1        | XX.XXXXXX| YY.YYYYYY
2        | X1.XXXXXX| Y1.YYYYYY

Let's say that I'm standing in a place with specific coordinates (lat and lon).

I would like to find all the places 1km around me (in a circle).

1) How can I convert the meters to coordinates?

2) How can I get from the database all the places 1km away from me? Cause I would like to add the meters to the coordinates.

Is that possible?

Thanks, in advance

programmer
  • 4,571
  • 13
  • 49
  • 59

1 Answers1

2

Simply use the distanceTo() or distanceBetween(lat1, lon1, lat2, lon2) function of your API.
If you dont have such a method, then search for haversine formula.

Then the location are within a cirlcle of x meters around you if the distance from you to the object is <= x.

In code:

double radius = 1000; // 1 km
double distanceMeters = haversineDistance(mylat, mylon, otherLat, otherLon);
if (distanceeMeters <= radius) {
  // other location (otherLat, otherfLOn) is inside of radius of 1km
}
AlexWien
  • 28,470
  • 6
  • 53
  • 83
  • thanks for replying, my problem is in line `double distanceMeters = haversineDistance(mylat, mylon, otherLat, otherLon);`. I have to get all the coordinates in my database in order to check if the distance is <= radius. My database consists of many points and the iteration through all the points in the database is going to be time-consuming because I have to check all the points in the database. If I could get only the LAT and LON of the points restricted in 1000m is going to be the best solution, Can that happen? – programmer Mar 27 '14 at 18:22
  • The otherLat and otherLon come from the database. So, I have to check for all the points in the database and this is not the best solution for me...I would like to get the LAT and LON of the points less than 1000m – programmer Mar 27 '14 at 18:25
  • If you have more than 10.000 locatoins to check, you need a geospatial index, e.g like a quad tree, kd tree or R-tree. Postgres DB has such a spatial index – AlexWien Mar 27 '14 at 19:33
  • Well I use Sqlite, I guess that I shall use a tree there. Is there any other way that we can reverse the Haversine formula? I would like to convert meters to X and Y coordinates. Is that possible? – programmer Mar 27 '14 at 20:08
  • That will not help you, if you have a huge data set. But yes, its called an UTM transformation. But beware when comparing two positions form different UTM Zones. If the zone of interest is less than some kilomters there are other such WGS84 to cartesian conversions. But this needs some geo know how. I would just use the haversine. – AlexWien Mar 27 '14 at 20:33