kimbo_king — 2014-07-08T15:31:03-04:00 — #1
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
c_ankerstjerne — 2014-07-08T15:46:00-04:00 — #2
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.
kimbo_king — 2014-07-08T16:47:19-04:00 — #3
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)
pdxsherpa — 2014-07-08T17:31:10-04:00 — #4
this should get you started
mawburn — 2014-07-08T20:10:33-04:00 — #5
You could download a list of Lat/Long for Zip Codes/Postal codes and do your own calculation.
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: http://www.freemaptools.com/download-uk-postcode-lat-lng.htm (midway down under Outcode Area)
These are just a couple random websites I found from Googling "Zip Code Lat Long CSV"
pdxsherpa — 2014-07-09T10:25:05-04:00 — #6
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?
kimbo_king — 2014-07-09T14:26:52-04:00 — #7
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
c_ankerstjerne — 2014-07-09T14:57:01-04:00 — #8
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.