View Single Post
Old 10-23-2008, 06:39 PM   #1 (permalink)
jcorradino
The Contributor
 
jcorradino's Avatar
 
Join Date: Sep 2008
Posts: 36
Thanks: 2
jcorradino is on a distinguished road
Default Google Maps API to calculate distance between zips? (solved)

Anyone know how to accomplish this feat?

I am trying to calculate the distance between one zip code and another (for a store locater). You can see it working here: Ligo Products - Quality Imported Furniture - Find Stores

I got it pretty much working, but I have two problems: the zip database I got from USPS does not seem to be very accurate, it (obviously) does not include Canadian postal codes, and for some reason, some zip codes either do not exist on the database, or the distance is not registered for some reason (making it seem as if that particular zip code is nowhere to be found).

I am using the following to find the distance in miles between one longitude/latitude and another

PHP Code:
$r 3963.1;
    
$pi 3.14159265358979323846;

    
$lat1 $lat1*($pi/180);
    
$lat2 $lat2*($pi/180);
    
$lon1 $lon1*($pi/180);
    
$lon2 $lon2*($pi/180);

    
$distance = ( acos cos $lat1 ) * cos $lon1 ) * cos $lat2 ) * cos $lon2 )  +  cos $lat1 ) * sin $lon1 ) * cos $lat2 ) * sin $lon2 ) + sin $lat1 ) * sin $lat2 ) ) * $r); 
For example, Biltrite should show up with a zip of 53220, but it does not show up until you change the distance selection to 40 miles.

Not only would I like to just implement the GMaps API, instead of digging through my code to find the culprit; I would also like to make the search much more accurate, which is not possible with the zips DB I have currently.

Thanks for the help!
__________________
Jason Corradino
Applications Developer, Interactive Support - Tribune Technology
J2EE Development, Script Tinkering - Develop, Support, and Maintain Tribune websites.

Last edited by jcorradino : 10-30-2008 at 01:38 PM.
jcorradino is offline  
Reply With Quote