Michael
Michael

Reputation: 4400

Calculating distance between two post codes

I would like some help with how to go about calculating the distance between two post codes. Here is the setup that I wish to have, let me give you a scenario.

A user logs into my website (they have provided their details, their postcode being one of them). That data is stored in the users table.

I have another table with details about locations. I would like to display locations (and the distance) that are closest to the user.

I know that there are certain methods that can be used, for example:

Converting a postcode to a long-lat and using built in methods to calculate the distance, I think Google offer a free service. But I have no idea how I would implement such a thing. All the tutorials I have taken a look at seem too complicated and for the time being I want a very crude and simple solution upon which I can build and more importantly learn!

I am fairly familiar with PHP and MySQL when it comes to basic input/output of data. But I am not sure of the best way to handle the information with regards to user data, and the locations of the 'businesses'.

Any suggestions, comments will be more than appreciated.

Upvotes: 0

Views: 2283

Answers (2)

user6172892
user6172892

Reputation: 11

If you're US based, this API should meet your needs: https://www.zipcodeapi.com

It's made specifically to find distance between two zip codes, tells you the long-lat for zip codes, find all zips relative to a location, etc.

Upvotes: 1

guy lancaster
guy lancaster

Reputation: 1

  1. Get postcode northings eastings data free from ordinance survey (credit them on your webpage to get free)

2 calculate distance from the appropriate csv flat file (M.csv contains all manchester postcodes and north and eastness).

  1. Distance is sqrt((n1-n2)**2 +(e1-e2)**2) - excuse my fortran

  2. If its slow,and you want an approximate distance create a small database of first half postcodes averages so you are looking up M20 not M20 3nb.

5 NB google don't like you using their postcode apis outside google Maps.

  1. I am wondering about an algorithm so that for short distances, if you are looking at places close to a postcode, you just search M postcodes for a M postcode (and maybe nearby post areas like SK, OL, if you have a table of adjacent postcode areas).

7 Any body taken this further?

Upvotes: 0

Related Questions