Here is a module that I wrote for that purpose. It will have to be modified for your needs, but should give you a good starting point for further development. Someday I will get around to making it an independent module. HTH
package Zips; use strict; use Time::HiRes qw(gettimeofday); sub get_coords { my ($s) = shift; my $dbh = $s->dbh; my ($zip) = @_; my $sth = $dbh->prepare(qq! SELECT lat,lng FROM zipcodes WHERE zip +='$zip' !); $sth->execute(); my ($lat,$lng) = $sth->fetchrow(); $sth->finish(); return ($lat,$lng); } sub radius_select { my ($s) = shift; my $dbh = $s->dbh; my ($zip,$m) = @_; my ($lat, $lng) = $s->get_coords($zip); if (($lat)&&($lng)) { print "<div align=center>Lat: $lat Long: $lng</div>\n"; my $d = $m / 3958.75; my $div = 57.2958; my $sth = $dbh->prepare(qq! SELECT zip,cname,state, ( 3958.75 * + ACOS( COS($lat/$div ) * COS(lat/$div) + SIN($lat/$div) * SIN(lat/$div) * COS($lng/$div - lng/$div) ) ) as +dist FROM zipcodes WHERE (ACOS( COS($lat/$div) * COS(l +at/$div) + SIN($lat/$div) * SIN(lat/$div) * COS($lng/$div - lng/$div) )) < $d ORDER BY dist !) || die "Can't select from zipcodes: + ", $dbh->errstr; #$s->write_log($sth->{Statement}); my $b = gettimeofday; $sth->execute() || die "Can't execute select on zipcodes: ", $ +sth->errstr; $s->e->{'qtime'} = gettimeofday-$b; my $r_all = $sth->fetchall_arrayref(); $sth->finish(); return $r_all; } else { print "Invalid Zip Code.\n"; } } sub get_limit_byzip { my ($s) = shift; my $dbh = $s->dbh; my ($zip,$m) = @_; my $d = 200 / 3958.75; my ($lat, $lng) = $s->get_coords($zip); if (($lat)&&($lng)) { print "<div align=center>Lat: $lat Long: $lng</div>\n"; my $d = $m / 3958.75; my $div = 57.2958; my $sth = $dbh->prepare(qq! SELECT zip,cname,state, ( 3958.75 * + ACOS( COS($lat/$div ) * COS(lat/$div) + SIN($lat/$div) * SIN(lat/$div) * COS($lng/$div - lng/$div) ) ) as +dist FROM zipcodes WHERE (ACOS( COS($lat/$div) * COS(l +at/$div) + SIN($lat/$div) * SIN(lat/$div) * COS($lng/$div - lng/$div) )) < $d Group by dist limit $m !) || die "Can't select from zipcodes: + ", $dbh->errstr; #$s->write_log($sth->{Statement}); my $b = gettimeofday; $sth->execute() || die "Can't execute select on zipcodes: ", $ +sth->errstr; $s->e->{'qtime'} = gettimeofday-$b; my $r_all = $sth->fetchall_arrayref(); $sth->finish(); return $r_all; } else { print "Invalid Zip Code.\n"; } } sub get_dist_byzip { my ($s) = shift; my ($zip, $zip2) = @_; my ($lat, $lng) = $s->get_coords($zip); my ($lat2,$lng2) = $s->get_coords($zip2); my $div = 57.2958; my $dist = (3958.75 * acos( cos($lat/$div) * cos($lat2/$div) + sin($lat/$div) * sin($lat2/$div) * cos($lng/$div - $lng2/$div) ) ); return $dist; } sub get_zips_bycity { my ($s) = shift; my ($city,$state,$dist) = @_; my $sth = $s->dbh->prepare(qq! SELECT zip FROM zipcodes WHERE cnam +e='$city' and state= '$state' !); $sth->execute(); my $zip = $sth->fetchrow(); $sth->finish(); return $s->radius_select($zip,$dist); } sub acos { atan2( sqrt(1 - $_[0] * $_[0]), $_[0] ); } 1; __END__ =pod =head1 NAME Zips.pm =head1 METHODS AVAILABLE $s->get_zips($zip, $dist) $s->get_coords($zip) => ($lat,$lng); $s->radius_select($zip,$m) => $r_all; $s->dist_select_byzip($zip, $zip2) => $dist; $s->acos() =head1 AUTHOR Stephen T. Walker <swalker@ssdw.com> August 2000 =cut

In reply to Re: Determining the distance from one zip code to another by steveAZ98
in thread Determining the distance from one zip code to another by princepawn

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.