0

A client wants a page on their website where the user can search for a stockist of their product range. What they want is the ability to enter a UK PostCode and for a list of their stockists to appear in closest order.

Is there a way to utilise Google Maps to determine the closest supplier?

I have a PHP MySQL database with ALL the suppliers Postcodes and details and I'll have the postcode of the user to use as well.

Dan Hanly
  • 7,829
  • 13
  • 73
  • 134

5 Answers5

3

I think you'll need the Ordnance Survey CodePoint data to map your postcodes to a longitude and latitude. Codepoint is available as a commercial product, or there's also Codepoint Open

Once you have the latitude and longitude, then you can use standard algorithms such as Haversine or Vincenty to calculate distances between the points

EDIT

Structure of the CodePoint Open CSV files can be found here. Note that location is held as Northings and Esatings rather than Longitude and Latitude, so it will need converting. There's a number of articles about this on the web, e.g.

http://www.deepbluesky.com/blog/-/converting-os-coodinates-into-longitude-latitude_7/
http://mediakey.dk/~cc/convert-northing-and-easting-utm-to-longitude-and-latitude/

but you need to be aware that OS Northings/Eastings are based on the Airy 1830 ellipsoid rather than the WGS84 model used by Google maps (and most GSM systems). Failing to allow for this difference can put you out by anything between 70-120m between Cornwall and East Anglia.

You can also find PHP functions to do this conversion at the Movable Type site (essential reading for any PHP developer, working with GeoData. I'd recommend adding a couple off columns to your stockist database for longitude and latitude, and a one-off script to update all existing data using the Codepoint data, then modify your insert/update routines for stockists to keep this information up-to-date. Using PHP, another solution for this conversion is PHPCoord by Jonathan Stott

For higher performance in your database query, do a lookup of lat/long against a "bounding box" before calculating distances. I've explained how to do this in response to a similar query.

Community
  • 1
  • 1
Mark Baker
  • 209,507
  • 32
  • 346
  • 385
0

You may find this website helpful: SQL/CSV Postcode Database

I've previously used this to lookup long/lat from a postcode "outcode".

There's also a php script (which I've not tested) which calculates distance between two postcodes.

James
  • 311
  • 5
  • 17
0

I think you'll need a little more data than that. I'd suggest that you need to store the latitude and longitude of every UK postcode alongside the postcode itself. I think this data is available from the Royal Mail for a cost and I remember reading somewhere that it was going to be available in the public domain too. Have a user enter their postcode, look up their latitude and longitude in the db and then use that to perform another query that calculates the closest supplier to them, perhaps within a certain number of miles. You could perhaps create a stores procedure to do all of this on the db. This post seems to have some details on how to do this.

Jeremy
  • 2,651
  • 1
  • 21
  • 28
0

If you want it to be fast then pre-compute the distances between postcodes within a certain radius of each other - see the following:

Calculate distance between zip codes and users

This should work fine in your case unless you think a user would be willing to travel more than 100 miles to purchase a product ??

Community
  • 1
  • 1
Jon Black
  • 16,223
  • 5
  • 43
  • 42
0

Instead of Google Maps, this open postcode geocoding API may be of use to you. As others have mentioned, once you have lat long for both points you can use standard algos to find distances. A previous question contains some info on how to do this directly in SQL.

Community
  • 1
  • 1
Nev Stokes
  • 9,051
  • 5
  • 42
  • 44