PHP Code To Calculate If UK Postcode Is Within Distance

Hi.

I am looking to see if anyone can point me in the right direction / code / api to basically find all users stored in a database are within e.g a 50 mile radius of a specified postcode. (UK)

Would I always have to check every postcode associated with a user every time a search is performed?

All I have at the moment is postcode, so need something that I pass parameters into and it returns yay or nay or I can calculate that. I’ve had a quick look at Google API is this the best thing to use, is there something better already developed OSource with examples?

Is there data for latitude and longitude of all UK postcodes anywhere?

Thanks very much.

Postcode Anywhere, and its API, would be worth a look.

Thanks for that shall have a look.

I am not sure if I have it but.

I’ve found postcodes.sql which has all the lng/lat of postcodes in the country and may just need to find/precal the lng/lat of the postcode specified by the user and run this SQL function

$orig_lat=23.04988655049843;
$orig_lon= 72.51734018325806;
$dist=10; // Radius;

//Latitude = database field name;
//Longitude = database field name;

$query = SELECT *, 3956 * 2 * ASIN(SQRT( POWER(SIN(($orig_lat -abs(dest.Latitude)) * pi()/180 / 2),2) + COS($orig_lat * pi()/180 ) * COS( abs(dest.Latitude) * pi()/180) * POWER(SIN(($orig_lon - dest.Longitude) * pi()/180 / 2), 2) )) as distance FROM tbl_name dest having distance < $dist ORDER BY distance limit 10

I’ve not tried it but it looks like something that would work

Found it here : http://stackoverflow.com/questions/2296824/php-mysql-compare-long-and-lat-return-ones-under-10-miles

Even more here if anybody else needs more info

http://www.movable-type.co.uk/scripts/latlong-db.html

&

I have just written an article that explains exactly how to do this. You can read it here:
Programmatically calculate distances between UK postcodes

As far as data files are concerned, if you want to work at the level of the individual postcode, any solution would require access to the official Postcode Address File (PAF), which is expensive and controlled by strict licensing. However, if you are happy to work at the area level, you can obtain free copies of the relevant lookup files (that resolve postcodes to grid references or lang/long references). The above article suggests some places where you can download those files.

I hope this helps. I’ve done a lot of work in this area, so if you have any more detailed questions, feel free to ask.

Mike

Thank Miki.

I’ll have a look.

I think I may have found what I need. Before I go down the wrong rd.

I have all the Area Codes and long/lat e.g BN3, BN1 and some code above that checks the distance within a certain radius from a postcode specified/point via SQL statement you see above. Does it look good?

Not got the time just yet but def will have a look at what you’ve linked.

Thanks very much…

Glad to help, Darius.

By the way, in mainland Britain (but not NI), it’s very much easier to work with grid references rather than long/lats. But the choice is yours.

Mike

Cheers for your help, I’ve bookmarked it in case I need to come back but so far what I have seems to work for my needs

I just want to reiterate these two links for anyone in the same situation it solved my problem

Finding all postcodes within a certain distance

http://www.movable-type.co.uk/scripts/latlong-db.html
&
I can’t find the link right now but google postcodes.sql

MySQL Script for UK List of Postcodes and Long/Lat Coords…