Man_E
Man_E

Reputation: 81

Query to find places near to me (Google Map)

For the purposes of my application, i am using google maps so that: A user will mark at the map the place where he stays (it is stored to the db). Then when he views the map again, it should show other users who stay near to him.

The coordinates are stored at a db field of users named eg coords, at this form: 22.515578,33.265897 (latitude,longitude)

What should be the query so that to find all marks which are +/- 1 (or any other distance) from him?? (Eg if he is at 22.515578,33.265897 then it should search for others which are eg at 21.515578,32.265897 or 23.515578,34.265897)

Thanks in advance!

Upvotes: 0

Views: 3271

Answers (2)

Argiropoulos Stavros
Argiropoulos Stavros

Reputation: 9533

Don't reinvent the wheel.If you are using mysql try the spatial extensions and the spatial functions between geometries(i.e. distance).if you try to do this manually than you must go another way.Cheers

Upvotes: 0

Gaurav
Gaurav

Reputation: 28775

This can help you.

For example here let given latitude and longitude is 32, -122 and distance is less then 25 miles.

SELECT id, ( 3959 * acos( cos( radians(37) ) * cos( radians( lat ) ) * cos( radians( lng ) - radians(-122) ) + sin( radians(37) ) * sin( radians( lat ) ) ) ) AS distance 
FROM geocodeTable HAVING distance < 25 ORDER BY distance LIMIT 0 , 20;

Upvotes: 1

Related Questions