php-b-grader
php-b-grader

Reputation: 3315

Matching long & lat within radius

I'm thinking of using my map coordinates to allow for location based searching and I don't know where to start.

I have a database of coordidinates - When I enter a suburb, I want to display all matches within a given radius. (i.e. search database of long/lat coords and return all coords that are in an x radius of long/lat of suburb searched)

I'm not sure how to achieve this or where to start?

Upvotes: 1

Views: 888

Answers (2)

Argiropoulos Stavros
Argiropoulos Stavros

Reputation: 9533

Are you aware of mysql spatial extensions?Try to create a spatial column in your table i.e. Point and then use the functions that spatial extensions offer so you don't reinvent the wheel

Upvotes: 0

herostwist
herostwist

Reputation: 3958

Well it really depends on what database you are using. the general technique is to use a scaler value function that uses a formula such as:

distance = ACOS(SIN(lat1)*SIN(lat2)+COS(lat1)*COS(lat2)*COS(lon2-lon1))*6371

then you can write a query like:

select * 
from locations 
where mydistancefunction(lat1,lng1,locations.lat,locations.lng) < @radius

Upvotes: 1

Related Questions