# Question : Google Maps API: how to calculate distance from a location to a ZIP code?

## Answer : Google Maps API: how to calculate distance from a location to a ZIP code?

##### A ZIP code is not a location, it is a postal carrier's route.  In the cities of the eastern USA there may be ZIP codes that are very small, a few buildings in Manhattan.  In the large states of the west, ZIP codes may be many, many miles wide.Map coordinates are given by the "geocode" which is a latitude/longitude pair.  The data is always expressed in a character string with two signed or unsigned decimal numbers separated by a comma.  The distance between two "geocodes" can be computed, as the crow flies, by the use of the Haversine formula.http://en.wikipedia.org/wiki/Haversine_formulaWhile a Great Circle route is the most accurate A-B distance, its "accuracy" may not be exactly what you want.  For short distances across our earth, plane geometry is sufficiently accurate.http://en.wikipedia.org/wiki/Cartesian_coordinate_systemThe other component of Great Circle accuracy goes to the mode of transportation.  It is highly useful for aircraft and ocean-going vessels.  But if your A-B travels are by automobile and you need to cross a river, for example, the Haversine formula expects you to go straight across the river whether there is a bridge or not.In my experience with Haversine calculations and Google Maps, I have found that you can get a reasonable approximation of the correct driving distance by multiplying the Haversine distance by 1.15 to 1.25.  By reasonable, I mean, "nobody complained" about the margin of error.  For short trips, the computations are less accurate and the fudge factor needs to tend to the high end.  For longer trips the computations are more accurate and the fudge factor can be reduced.The "true" driving distance is determined by evaluating a series of "minitrips" one for each turn-point along the route.  Google Maps can do this.  IIRC there is an API call that will return this data.The newly released Google Maps API has some amazingly robust features.   For example, it was necessary to "geocode" an address before adding a pushpin to the map - now you can simply give the address or the name of the location ("The White House") and Google Maps will often be able to translate the name and/or address into the geolocation for you.  I have an article here at EE that covers some of the new features.http://www.experts-exchange.com/Web_Development/Web_Languages-Standards/PHP/A_3350-Using-the-Google-Maps-API-in-PHP.htmlThe code snippet teaches the way to compute Great Circle distances.Google and Yahoo Geocoders will return a geocode if you give them nothing but a zip code, however the accuracy is not all that great.  The Yahoo Geocoder is the easiest to use.http://developer.yahoo.com/maps/rest/V1/geocode.htmlHope this info is helpful to you.  Please post back if you have any questions.  Best regards, ~Ray ```1: 2: 3: 4: 5: 6: 7: 8: 9: 10: 11: 12: 13: 14: 15: 16: 17: 18: 19: 20: 21: 22: 23: 24: 25: 26: 27: 28: 29: 30: 31: 32: 33: 34: 35: 36: 37: 38: 39: 40: 41: 42: 43: 44: 45: 46: 47: 48: 49: 50: 51: 52: 53: 54: 55: 56: 57: 58: 59: 60: 61: 62: 63: 64: ``` ```<?php // RAY_compute_distance.php error_reporting(E_ALL); echo "<pre>\n"; // COMPUTE THE DISTANCE BETWEEN TWO LAT/LON PAIRS // MAN PAGE: http://en.wikipedia.org/wiki/Haversine_formula function compute_distance(\$from_lat, \$from_lon, \$to_lat, \$to_lon, \$units='KM') { \$units = strtoupper(substr(trim(\$units),0,1)); // ENSURE THAT ALL ARE FLOATING POINT VALUES \$from_lat = floatval(\$from_lat); \$from_lon = floatval(\$from_lon); \$to_lat = floatval(\$to_lat); \$to_lon = floatval(\$to_lon); // IF THE SAME POINT if ( (\$from_lat == \$to_lat) && (\$from_lon == \$to_lon) ) { return 0.0; } // COMPUTE THE DISTANCE WITH THE HAVERSINE FORMULA \$distance = acos ( sin(deg2rad(\$from_lat)) * sin(deg2rad(\$to_lat)) + cos(deg2rad(\$from_lat)) * cos(deg2rad(\$to_lat)) * cos(deg2rad(\$from_lon - \$to_lon)) ) ; \$distance = rad2deg(\$distance); // DISTANCE IN MILES AND KM - ADD OTHERS IF NEEDED \$miles = (float) \$distance * 69.0; \$km = (float) \$miles * 1.61; // RETURN MILES if (\$units == 'M') return round(\$miles,1); // RETURN KILOMETERS = MILES * 1.61 if (\$units == 'K') return round(\$km,2); } if (!empty(\$_GET)) { \$distance = compute_distance(\$_GET["a_lat"], \$_GET["a_lon"], \$_GET["b_lat"], \$_GET["b_lon"], \$_GET["units"]); echo \$distance . ' ' . \$_GET["units"]; } ?> <a target="_blank" href="http://maps.google.com/maps?f=q&source=s_q&hl=en&geocode=&q=camp+david+geolocation&sll=39.648333,-77.465&sspn=0.025972,0.057807&ie=UTF8&z=15&iwloc=lyrftr:m,10141899780758213082,39.648361,-77.465029">Camp David</a> <a target="_blanl" href="http://maps.google.com/maps?f=q&source=s_q&hl=en&geocode=&q=White+House+Washington,+DC&sll=39.806296,-77.097044&sspn=0.414606,0.924911&ie=UTF8&hq=White+House&hnear=White+House,+Washington,+DC&ll=38.898047,-77.036562&spn=0.025417,0.057807&z=15&iwloc=A">White House</a> TEST IT HERE: <form> POINT A LAT <input name="a_lat" value="38.898047"> LON <input name="a_lon" value="-77.036562" /> POINT B LAT <input name="b_lat" value="39.737554"> LON <input name="b_lon" value="-77.464943" /> <input type="radio" name="units" value="miles" checked="checked" />Miles <input type="radio" name="units" value="km" />Kilometers <input type="submit" /> </form> ```
 Random Solutions