Tunç,

> Thank you very much for a quick reply. What I am after is visitor puts
> their postcode and the application calculates and displays the nearest
> stores in order (nearest one first) with some information.
>
> Please have a look at: 
> http://www.asda.co.uk/corp/storelocator_frameset/storelocator_frameset.htm
> try postcode: N3 2NS
>
> Is that how your solutions work?

Actually the code I wrote allows more granular searching if desired.   
The user can search by just their postal code (which can only  
calculate distance to the store by their general location), or they  
can get really granular and search by their actual address and get the  
distance from their location to each store in miles, kilometers or  
nautical miles (and all using the free Google Geocode service).  Also,  
I didn't want to have hundreds of hits to Google's service (that would  
be slow), so instead I only hit Google's service once for a search  
(fast and simple).

My app wasn't written in FarCry (it's for another framework I wrote),  
but the logic is separate from views so it wouldn't make a difference  
anyway.

The way I did it was that when I originally store all of my store  
locations in the database I used the Google Geocode service to lookup  
their latitude and longitude and store that with their information in  
the database (as separate DB fields).  Like you, I had an existing DB  
of store locations, so I just needed to populate them first by looping  
over them (query loop) and get their lat & lon from the Google Geocode  
service (it was just a one-time thing.  Going forward in the future  
when someone added or updated a store location in the database, I had  
the code talk to the Google Geocode service to get the lat & lon).

Then when a user gets to the search form, I allow them to search by  
their zipcode/postalcode.  All I want back from the search is the  
user's latitude and longitude (which means that I send their  
postalcode to the Google Geocode).  But this also means that they can  
choose to search by their actual address (or they can generalize:  
search by state, zip, town, street, full address... whatever Google  
can accept as a search string - each option giving them more accurate  
results (or less accurate results) depending on how general their  
search string was.

Next I run a cached query of all the store locations (making sure to  
get their latitudes and longitudes which were calculated at a  
different time - read above).  I then compare the lat & long of the  
user's search location against each location's lat & lon and calculate  
their distance using a method called getDistance().  The getDistance()  
method/function gets the distance (in miles, kilometers or nautical  
miles) between two location's latitudes and longitudes (it's very fast  
- I can compare the user's search against thousands of results and it  
takes only milliseconds to run - and no need to hit an offline service  
because the database already holds the latitudes and longitudes of all  
the store locaitons).

I add the resulting data to a custom query (the query now consists of  
all the DB fields for the store location and a new field called  
"distance") and sort the query by distance.  The final result is a  
query of store locations with their distance in miles, kilometers, or  
nautical mile from the user's search location.

Then I just output the top 5 stores (or however many you want) that  
shows all the store information as well as distance to the client.  I  
even offer a link to a modal window with a Google Map (showing the  
location as well as the driving course from start to finish.  However,  
this is a completely different topic and code discussion :) ).

The solution is extremely fast to run and cost me nothing but  
development time.  Keep in mind that you're relying on a cloud service  
(in this case Google Geocode), so you should always check to make sure  
the service is running.  My code has been running for 1.5 years now  
without a problem, but you never know.

Although my original custom method getDistance() method worked, the  
code was lengthy and complicated (I made finding the radius and theta  
equations much more difficult than they needed to be).  A few months  
after the project launched I found another script by someone else  
doing the same thing, but much with much cleaner code http://tinyurl.com/dh6rwl 
.

I hope this helps.

--
Jeff Coughlin
Web Application Developer
http://jeffcoughlin.com
--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups 
"farcry-dev" group.
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to 
[email protected]
For more options, visit this group at 
http://groups.google.com/group/farcry-dev?hl=en
-~----------~----~----~----~------~----~------~--~---

Reply via email to