Find the nearest store to a customer
I have a form where the client is able to enter a post/zip code. I would then like this to be matched against a database of stores and to output the nearest 3.
How would I set something like this up? I'm wondering if I could save the store information in an XML file? However the difficult bit is working out the closest matches to the customers post/zip code. Can anyone help point me in the right direction on how to do this?
Thanks :thumb:
Re: Find the nearest store to a customer
Suppose your user's postal code is 12345, then you could query your database to check whether an exact match is found or else list those which are in between 12340 and 12350.
I'm not much sure about the postal codes, I mean how they are numbered. So, what I said was based on assumption.
The query might look something like this:
Code:
SELECT store_address FROM tblStores WHERE store_zip = '12345' OR (store_zip >= '12340' AND store_zip <= '12350')
I haven't tested it.
:wave:
Re: Find the nearest store to a customer
I understand the logic but I'm not sure if that will work as I'm not sure if postal codes are numbered in that sort of way.
I was wondering if there is any function/API/script which works this out - anyone know?
Re: Find the nearest store to a customer
I believe Google Maps has an API to get the langitude/longitude coordinates of an address (or part thereof - like a zip code); you can store the coordinates of your store locations, then do an API call on user-submitted addresses and compare for the nearest locations. I'd try to find some resource links for you, but I'm on a mobile device at the moment.
Re: Find the nearest store to a customer
Maps API: http://code.google.com/apis/maps/index.html
If you only need to search your own store locations, I would save the coordinates of each store in your database and execute a query ordered by distance ((customer latitude - store latitude) + (customer longitude - store longitude)).
See if you can use the Maps API to get the customer's coordinates.
The bonus of using the Google tools is that they will accept any sensible sort of input: suburb names and street addresses as well as postcodes.