NYCPHP Meetup

NYPHP.org

[nycphp-talk] Zipcodes calculation

Mark Armendariz nyphp at enobrev.com
Mon Jul 19 01:13:00 EDT 2004


Posted this on this list some time ago:

<snip>
here's a function I've been using for a couple years now.  I can honestly
say I have no idea whether or not I wrote it as I'd done a LOT of variations
of it at the time.  From what I know it's worked well for around 2 years now
(for my client).  I've only tested with US values, but I imagine it should
work for other values as well.
 
Good luck!!

function getDistance($fromLatitude, $fromLongitude, $toLatitude,
$toLongitude) {
 
 /* assume your points, in decimal, are in $fromLongitude,$fromLatitude and
$toLongitude,$toLatitude 
 * this can also be useful with the US ZIPcodes at 
 * http://ftp.census.gov/geo/www/gazetteer/places.html 
 */ 
 
 $Pi   = 3.1415926535897932384626;
 $Radius  = doubleval($Pi/180.0); 
 
 $fromLongitude  = doubleval($fromLongitude) * $Radius; $fromLatitude =
doubleval($fromLatitude) * $Radius; 
 $toLongitude   = doubleval($toLongitude) * $Radius; $toLatitude =
doubleval($toLatitude) * $Radius; 
 
 $theta = $toLongitude - $fromLongitude; 
 $Distance = acos(sin($fromLatitude) * sin($toLatitude) + cos($fromLatitude)
* cos($toLatitude) * cos($theta)); 
 if ($Distance < 0) {
  $Distance += $Pi;
 } 
 
 $Distance  = $Distance * 6371.2; 
 $Miles   = doubleval($Distance * 0.621);
 $Distance  = sprintf("%.2f", $Distance); 
 $Miles   = sprintf("%.4f", $Miles);
 
 return $Miles;
}
</snip>

As far as doing it through sql... Haven't tried it.

> -----Original Message-----
> From: talk-bounces at lists.nyphp.org 
> [mailto:talk-bounces at lists.nyphp.org] On Behalf Of inforequest
> Sent: Sunday, July 18, 2004 8:46 PM
> To: talk at lists.nyphp.org
> Subject: Re: [nycphp-talk] Zipcodes calculation
> 
> Kshitij Bedi Kbedi-at-inta.org |nyphp 04/2004| wrote:
> 
> >I have a database with zipcodes and corresponding latitudes 
> and longitudes.
> >Can anyone suggest a mysql statement that can pull me all zipcodes 
> >within 20 or 50 or 100 miles of a given zipcode
> >
> >Kshitij Bedi
> >Web Administrator
> >International Trademark Association
> > 
> >_______________________________________________
> >talk mailing list
> >talk at lists.nyphp.org
> >http://lists.nyphp.org/mailman/listinfo/talk
> >
> >  
> >
> is that a free database or one that has to be commercially licensed?
> _______________________________________________
> talk mailing list
> talk at lists.nyphp.org
> http://lists.nyphp.org/mailman/listinfo/talk
> 
> 




More information about the talk mailing list