0

I have a database with thousands of users having latitude and longitude values, I want to get all users from database who are within 1 km radius around my current location.

say I am at location A with my lat 10 and long 20. I have to search users nearby with the range of 1 km.... and these user locations are stored in database.

one way I figured out to get each user lat and lon and compare with mine with iOS method distanceFromLocation: but that requires each value to be fetched first and then calculated with method..Is there any other way ? Maybe to do this on database side?

LoVo
  • 1,856
  • 19
  • 21
user3226440
  • 559
  • 1
  • 8
  • 23

1 Answers1

0

You can calculate it from mysql.

For total description how to do it http://www.scribd.com/doc/2569355/Geo-Distance-Search-with-MySQL

For quick answer https://stackoverflow.com/a/570048/2246798

Or this https://stackoverflow.com/a/574736/2246798

EDIT

Please find the code here

ACTUAL TABLE

Demo table

QUERY WITH RESPONSE

Query with response

Community
  • 1
  • 1
souvickcse
  • 7,742
  • 5
  • 37
  • 64
  • Hi Thanks! the last stackoverflow answer seems fine.. but into this is there any column is distance bcz i m using sql server and its giving me error – user3226440 Mar 07 '15 at 09:29
  • Please check the full query. Specially `( 3959 * acos( cos( radians(37) ) * cos( radians( lat ) ) * cos( radians( lng ) - radians(-122) ) + sin( radians(37) ) * sin(radians(lat)) ) ) AS distance ` – souvickcse Mar 07 '15 at 15:23