Jump to content

selecting results with range + Pythagoras theorem


scarhand

Recommended Posts

i have a table for uk people that includes:

 

- postcode beginning (i.e. BB2)

- latitude (int)

- longitude (int)

- range (int, in miles, 1-20)

 

http://www.easypeasy.com/guides/article.php?article=64 - theres the article for the sql file i based my table on

 

now it says i can use Pythagoras theorem to calculate distances based on longitude and latitude.

 

so lets say i wanted to select all the people who are in range (based on the 1-20 they enter in the range field) of a postcode beginning i search for

 

for example, lets say i search for "BB2", i want a query that will select all of the people whose postcode beginning is "BB2", AND all the people within range of BB2 (going by the 1-20 mile range in their database field).

 

can some math whiz help me out?

Hi

 

Think the calculation you need is:-

 

WHERE ( 3959 * acos( cos( radians(#1#) ) * cos( radians( lat ) ) * cos( radians( lng ) - radians(#2#) ) + sin( radians(#1#) ) * sin( radians( lat ) ) ) ) < 20

 

where #1# is the latitude of your centre point and #2# is its longitude.

 

All the best

 

Keith

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.