3

I have stored latitude and longitude for different interesting places in my database. Whenever a user clicks into any of the interesting places, the show page displays details of the location, along with a google map with a marker pointing at it.

Now I have been trying to implement a button to "Show nearby places", along with a drop-down for ranges (eg. 50km, 100km, 150km), but could not figure out how to do it.

I was wondering how to find the neighboring places within a given range from a selected location, using the latitudes and longitudes I have in my database.

Taryn
  • 242,637
  • 56
  • 362
  • 405
jayandra
  • 296
  • 2
  • 8

3 Answers3

3

You should use geocoder, it provides builtin methods like:

Venue.near([40.71, 100.23], 20)    # venues within 20 miles of a point
apneadiving
  • 114,565
  • 26
  • 219
  • 213
1

If you need to be precise - you need to keep in mind Earth's shape - it is not a plane. So, choose your accuracy level (simple sphere, WGS84) and write some math for it. And what database do you use? Some of them can operate with spartial inedxes which may be very useful.

madhead
  • 31,729
  • 16
  • 153
  • 201
0

for an easy - not accurate, but fast approach look here on SO small distance

it is using a simple technical logic the fits many problems in real world, but it is fast but not intended do be accurate

halfbit
  • 3,773
  • 2
  • 34
  • 47