0

I want to find nearby registered locations from a database and i can get the current location of the user from GPS android. It would be nice if the nearby registered locations are derived from the database are limited by a radius of 3km.

I can store the latitude and longitude values in the database but, what is the best way to find neaby location ? Retrive and check in the android or directly checking it in the php and then retriving it to the android device.?

For example:A user registers his location, it will be stored in the database and later when another user tries to find nearby registered users the most nearby user should be retrived.

Isaac Bennetch
  • 11,830
  • 2
  • 32
  • 43
Guruprasad N
  • 1
  • 1
  • 4
  • Possible duplicate of [Getting lat long values from database in android](http://stackoverflow.com/questions/10307904/getting-lat-long-values-from-database-in-android) – karan Jul 04 '16 at 06:13

1 Answers1

1

well, you can do this using sql query.

SELECT id, ( 3959 * acos ( cos ( radians(78.3232) ) * cos( radians( lat ) ) * cos( radians( lng ) - radians(65.3234) ) + sin ( radians(78.3232) ) * sin( radians( lat ) ) ) ) AS distance FROM markers HAVING distance < 30 ORDER BY distance LIMIT 0 , 20;

It will find the closest 20 locations that are within a radius of 30 miles to the (78.3232, 65.3234)(latitude, longitude) coordinate (your current location in degree radian). lat, lng are latitude and logitude, database table column names where you store your custom locations (also in degree radian)

To search by kilometers instead of miles, replace 3959 with 6371. refer this question

Community
  • 1
  • 1
SJB
  • 193
  • 1
  • 1
  • 9
  • Wether those 20 locations should be filtered on the server or on the client - depends. Doing it on the server takes more processing power, but doing it on the client takes more bandwidth. – Ivan Rubinson Jul 04 '16 at 06:37
  • Saving bandwidth would be better :D Thanks anyways :) – Guruprasad N Jul 04 '16 at 06:40