Nearest to my address


I’ve tried searching the forums but cannot find anything. If there is already a thread and you could link it to me that would be great.

I have a list of locations (UK postcodes (Zip codes)) and want a user to be able to type in their address (postcode) and it bring back the 5/10 nearest from my list.

Is this possible?

I use HTML and java on my site but I’m not very confident with java. Any help would be appreciated

What do you mean by ‘nearest’?

Do you mean nearest in alphabetical terms, whereby Manchester is closer to London than Gatwick, or nearest in geographic terms?

The former solution is trivial, but not very helpful.

The latter requires that you have GPS coordinates for each postcode, but assuming you have access to this information, would be fairly trivial to code as well. The distance between two points in a Cartesian plane is very easy to calculate, to the extent that you could possibly do a real-time calculation from one postcode to all other postcodes in the UK without any significant lag. More efficient methods exist, of course, examples of which can no doubt be found through Google.

Thanks for the reply

I do mean distance rather than alphabetical

I’d like to do something like this:

Essentially it will show a list of football clubs near to a players home (We are a website promoting amateur football)

I believe for that you would need your google map api and some javascript.
this should get you started

You could download a list of Lat/Long for Zip Codes/Postal codes and do your own calculation.

Haversine Formula

var R = 6371; // kilometers
var phi1 = lat1.toRadians();
var phi2 = lat2.toRadians();
var deltaphi = (lat2-lat1).toRadians();
var deltalam = (lon2-lon1).toRadians();

var a = Math.sin(deltaphi/2) * Math.sin(deltaphi/2) +
        Math.cos(phi1) * Math.cos(phi2) *
        Math.sin(deltalam/2) * Math.sin(deltalam/2);
var c = 2 * Math.atan2(Math.sqrt(a), Math.sqrt(1-a));

var d = R * c;

I’ve never actually done it, but I recently looked into how to do it and this is the best solution I found. If I’m not mistaken, Google API is only available for a certain amount of requests per month until they start charging you.

You can download CSVs of locations for US Zip codes and UK Postal Codes and just add them to your DB.

UK: (midway down under Outcode Area)

These are just a couple random websites I found from Googling “Zip Code Lat Long CSV”

This is what i got from google info:

If your site or application generates 25 000 map loads or more each day, for more than 90 consecutive days, we’ll get in touch with you to talk about payment. Don’t worry, if you go over the limits, we won’t immediately shut off your API access or display error messages on your site.

But i like your solution mawburn.
The only thing i would say is that if kimbo_king doesn’t have the code to implement it he could still crib some from google dev and tweak it?

Thanks all

Appreciate the responses

I have a CSV file full of postcodes ready to go

Let me have a play with some of your suggestions and I’ll go from there

If you do use the formula given by Mawburn, I would suggest pre-calculating the cosine of phi1 and phi2 for your table in advance, to speed up the calculation.