[nycphp-talk] distances between two zip codes
Rob Marscher
rmarscher at beaffinitive.com
Thu Dec 6 23:57:20 EST 2007
On Dec 6, 2007, at 11:13 PM, chad qian wrote:
> I want to program php to calculate the distance between two zip
> codes.I only have zip codes,nothing else.How to do it?I'm completely
> lost.
You need to find out the latitude and longitude of the zip codes.
That's the only way.
You can purchase zipcode databases that will give you the latitude and
longitude. Usually it's something you get a subscription for so you
can be updated when zipcodes change. My company uses this service:
http://www.zipcodeworld.com
An alternative to purchasing your own zipcode database, is to use a
web service to get the latitude and longitude:
http://developer.yahoo.com/maps/rest/V1/geocode.html
Rasmus Lerdorf (creator of php) even posted a great article on how to
use that web service:
http://toys.lerdorf.com/archives/35-GeoCool!.html
Once you have the latitude and longitude, you want to search google
for "distance latitude longitude php." The first hit is
coincidentally the site that sells the zipcode databases:
http://www.zipcodeworld.com/samples/distance.php.html
If you go the route of buying a subscription to the zipcode database
(probably only if you have too high a level of web traffic to use the
Yahoo! ), you should import the CSV file into a mysql database.
Donald J Organ IV just posted a mysql query to this list last week
that can find all the zipcodes in a range:
On Dec 4, 2007, at 1:26 PM, Donald J Organ IV wrote:
> replace the follwing fields with value:
>
> [origin-lat]
> [origin-long]
> [dest-lat]
> [dest-long]
> [radius]
> SELECT distinct zipcode,
> ROUND((ACOS((SIN([origin-lat]/57.2958) * SIN([dest-latitude]/
> 57.2958)) +
> (COS([origin-lat]/57.2958) * COS([dest-lat]/57.2958) *
> COS([dest-long]/57.2958 - [origin-long]/57.2958)))) * 3963, 3) AS
> distance
> FROM zipcodes
> WHERE (latitude >= [origin-lat] - ([radius]/111))
> AND (latitude <= [origin-lat] + ([radius]/111))
> AND (longitude >= [origin-long] - ([radius]/111))
> AND (longitude <= [origin-long] + ([radius]/111))
> ORDER BY distance
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.nyphp.org/pipermail/talk/attachments/20071206/2939a941/attachment.html>
More information about the talk
mailing list