Love2c0de Posted April 16, 2013 Share Posted April 16, 2013 Good evening, I want to have a page where users can enter their postcode to find their nearest Citizens Advice Bureau. This is throughout England (excluding Ireland, Scotland and Wales). My questions are: How can I compare the post code entered by the user to the post code of the exisiting Citizens Advice Bureau's? I know in PHP we can use a string function which compares the similarity between 2 strings but this clearly isn't going to suffice and work properly as you could have a CAB in London with post code SW1X 3RT (for example) and the users post code could be CM2 7ZN. My initial thoughts are to store the addresses and post codes of all the CAB's in a table but how do I compare those values and be accurate? The website company I work for have a similar functionality where we display certain things depending on user's post code so it is a fully post code driven site and I know our developer was coding a longitude and latitude script to determine this. Is that the only way to be accurate or would there be an easier solution or even a better one? Kind regards, L2c. Quote Link to comment Share on other sites More sharing options...
Jessica Posted April 16, 2013 Share Posted April 16, 2013 What do you mean by compare them? Because as you pointed out, your two examples don't have much in common. What are you looking for? Quote Link to comment Share on other sites More sharing options...
Love2c0de Posted April 16, 2013 Author Share Posted April 16, 2013 Sorry I'm finding this difficult to explain. What I want is the user to enter their post code into an input, which will then display to them the nearest 5 citizens advice bureau addresses but ordered by distance from the users location. Let's say I'm in London and for example lets say there are 10 citizens advice bureaus in that city. I want my page to return the 5 nearest CAB's relevant to the users post code. Even if one of those 5 CAB's is not WITHIN London, it should display the 5 nearest ones. I need to determine what the distance is of each CAB depending on the users post code, then return to them 5 of the nearest CAB's Kind regards, L2c. Quote Link to comment Share on other sites More sharing options...
Jessica Posted April 16, 2013 Share Posted April 16, 2013 Nearest - your best bet is to find an existing database that relates post codes to lattitude and longitude. I've done this with US zip codes, never tried UK. You can import that data into your program. Another option is to use something like Google Maps API to calculate the routes. I've also done that and it works well. Quote Link to comment Share on other sites More sharing options...
Barand Posted April 16, 2013 Share Posted April 16, 2013 If you are just searching for a matching postcode then the problem is simple. I would strip out any spaces from the code before storing as you are never sure if the user will always put a space in the middle. The first part of the postcode can be 2,3 or 4 chars but the second part is always 3. If you want to use distance to find the nearest then, as Jessica said, you will need the geographic locations of CABs and you will have to get a file of postcodes with their locations. You can use lat and long positions but, if you have them, Ordinance Survey eastings and northings are easier (you can just apply Pythagoras) as the coordinates are in metres. Quote Link to comment Share on other sites More sharing options...
Love2c0de Posted April 16, 2013 Author Share Posted April 16, 2013 Thank you very much, I've decided to go for the Google Maps API route and I'm currently just on the developers part about to read up on it. Kind regards, L2c. Quote Link to comment Share on other sites More sharing options...
Love2c0de Posted April 16, 2013 Author Share Posted April 16, 2013 If you are just searching for a matching postcode then the problem is simple. I would strip out any spaces from the code before storing as you are never sure if the user will always put a space in the middle. The first part of the postcode can be 2,3 or 4 chars but the second part is always 3. Good evening Barand, I'm not searching for a matching postcode as such, I need to return some results based on the distance, ordered nearest to farthest. If you want to use distance to find the nearest then, as Jessica said, you will need the geographic locations of CABs and you will have to get a file of postcodes with their locations. You can use lat and long positions but, if you have them, Ordinance Survey eastings and northings are easier (you can just apply Pythagoras) as the coordinates are in metres. So I need to have some sort of file/database which is holding the addresses and post codes of all the CAB's nationally. I'm not quote sure about the last sentence and will be researching all of this shortly but is there a way to integrate a database of addresses and postcodes of CAB's into something like the Google Maps API? Thanks for your input, Kind regards, L2c. Quote Link to comment Share on other sites More sharing options...
Barand Posted April 16, 2013 Share Posted April 16, 2013 I have never used Google maps API but I am fairly sure it knows postcode locations Quote Link to comment Share on other sites More sharing options...
Love2c0de Posted April 17, 2013 Author Share Posted April 17, 2013 What is the best format when storing addresses into a table? Should I split the address into separate columns like: house_name_or_number street_name city_or_towncounty post_code Or should I just store it into one field as a full string? Kind regards, L2c. Quote Link to comment Share on other sites More sharing options...
Barand Posted April 17, 2013 Share Posted April 17, 2013 (edited) I'd certainly keep postcode separate. If you are likely to require a search/sort on county or town then separate those also. Basically, separate fields give more future flexibility Edited April 17, 2013 by Barand Quote Link to comment Share on other sites More sharing options...
Love2c0de Posted April 18, 2013 Author Share Posted April 18, 2013 Thank you for your reply. Kind regards, L2c. Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.