0

Hello i developed an android app, using the position of my user, to convert it in lat and lng, my user will set a range too (to this example 10km ), my user will do a search in my app, and i will search in a web server what places are near my user, the web server will return to me a list of places, the question is. How can i know if the lat and lng is in the specified range?

Ex:

user position -14.84598758/-17.54817477
position 1 -14.4451524/-16.54654254 // this is in the 10km range?
poistion 2 -14.8444477/-17.54141587 // too?

google maps api v3 - android here

ronniery
  • 43
  • 1
  • 7
  • Check out http://googlemaps.github.io/android-maps-utils/javadoc/com/google/maps/android/SphericalUtil.html. You can either include it in your project or just find the formulas for typical LatLng calculations there. – user2808624 Jul 25 '14 at 04:27

1 Answers1

0

The Pythagorean Theorem - Find the differences between both X values and both Y values of 2 points, and use that to calculate the length of the hypotenuse.

This will give you a radius for a circle, which is the most common type of geofence. A more efficient "square" region can also be calculated simply in a database query by comparing ranges of X and Y, and potentially is indexable for faster results. For the purpose of reducing the intermediate result set of searchable points in a database query, you can pre-filter with a square (ABS(DIFF(x1,x2)) < hypotenuse AND ABS(DIFF(y1,y2)) < hypotenuse) since it can be translated to a range query in SQL to narrow down the results.

codenheim
  • 20,467
  • 1
  • 59
  • 80