Radius Results by Postal Code

6,195 views
Skip to first unread message

calibanter

unread,
May 26, 2010, 3:30:38 PM5/26/10
to Google Maps JavaScript API v3
We have built a multi-search tool with php working with a mySQL
database of LatLng Coordinates. The page is here:
http://www.orcaretirement.com/search_master.php

Currently, for one of the three searches, the user searches by
(Canadian) Postal Code which checks the database for that postal
code. But we have decided we want something smarter. So I want to
build a function that:

1. Takes the input Postal Code as a variable
2. Uses a standard radius of say 30 kms
3. And collects and returns all the records from the database (and
map markers) within a 30 km radius of that postal code.
4. And to be perfectly clever about the whole thing, if there are NO
results in the radius of 30km, then a result of ONE of the next
nearest result.

Now I know I can assign an approximate set of coordinates to a postal
code using the Geocoder Object -
http://code.google.com/apis/maps/documentation/javascript/services.html#Geocoding

But the most I can so far get from that is a JavaScript variable that
I can't re-use with my php code. At least not without a whole lot of
trouble as far as I understand, so:

1. How do I dynamically find and use approximate coordinates derived
from a Postal Code, especially as a php variable?
2. Once I have that variable, how do I do a radius search of
Coordinates from a Database?

I have spent a few days trying to sort this out; and I have to imagine
someone else has managed to do something similar and any help and tips
would be seriously appreciated. Thank you.

Esa

unread,
May 26, 2010, 3:49:08 PM5/26/10
to Google Maps JavaScript API v3
Geonames have a service called Find Nearby Postal Codes
http://www.geonames.org/export/web-services.html#findNearbyPostalCodes

Marc Guay

unread,
May 26, 2010, 3:51:01 PM5/26/10
to google-map...@googlegroups.com
I've been looking into this myself and found this handy document:

http://code.google.com/apis/maps/articles/phpsqlgeocode.html#geocodephp-Paul

Marc Guay

unread,
May 26, 2010, 4:09:02 PM5/26/10
to google-map...@googlegroups.com
> http://code.google.com/apis/maps/articles/phpsqlgeocode.html#geocodephp-Paul

I forgot to mention that once you have the LatLong's there is a simple
formula you can use to calculate the distance between two points.

Marc
MobilizeMe

Kevin Slack

unread,
May 27, 2010, 10:19:11 AM5/27/10
to google-map...@googlegroups.com
Thanks Marc and Esa for your help.  I've already got a perfectly functional database with perfectly functional php lookup queries.  The one in particular that is giving me trouble is Postal Code.  I don't want to get results from my database of  addresses that match the Postal Code and plot the map.  I want to:

1.   Assign approximate coordinates to that postal code; and
2.   Then do a radius result (querying LatLng) 

I don't know how to do either yet but I assume I can sort out the second part of this.  The first part is giving me the trouble.  I can see that I can use the Geocoder object but I get the result (the approximate LatLng of the Postal Code) as a JavaScript variable. But I think I need to play with that variable as a PHP variable. 

It occurs to me that it might just be faster to create a lookup table in my database of Postal Codes and Approx LatLng - that way I don't have to worry about Geocoder data and timing my requests and JavaScript Variables.  Thanks again.

Kevin




--
You received this message because you are subscribed to the Google Groups "Google Maps JavaScript API v3" group.
To post to this group, send email to google-map...@googlegroups.com.
To unsubscribe from this group, send email to google-maps-js-a...@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/google-maps-js-api-v3?hl=en.


Kevin Slack

unread,
May 27, 2010, 12:40:03 PM5/27/10
to google-map...@googlegroups.com
Yes, I think that was the path of least resistance: a database of postal codes and approximate LatLng codes.  Now I've got website code that can easily fetch a LatLong code from a Postal Code.  So now what?

1.   I have a LatLng Coordinate
2.   I want to set a maximum distance, say 30 kms
3.   I have a database of Addresses with LatLng Coordinates
4.   I need to take my Lookup Coordinate and query my database using my maximum distance value
5.   And collect and map all the results.

1 is done; and I know how to do #5.  Meanwhile I will continue to poke around. Thanks. 

Marc Guay

unread,
May 27, 2010, 12:57:55 PM5/27/10
to google-map...@googlegroups.com
1. User enters distance value (say 5km) and Postal Code (say A1A1A1)
2. Convert the PC into a LatLong
3. Loop through your database and ask "Is the distance between these
two points less then or equal to 5km?", if yes, gimme that record!
4. Loop through the matching records and plot them on your map by
passing the PHP vars to JS.

Kevin Slack

unread,
May 27, 2010, 1:04:29 PM5/27/10
to google-map...@googlegroups.com
I was being lazy.  The trick is the Haversine formula, explained here:  http://code.google.com/apis/maps/articles/phpsqlsearch_v3.html.  All better now.  Thank you.

Kevin
Reply all
Reply to author
Forward
0 new messages