|
-
May 23rd, 2005, 05:10 PM
#1
Thread Starter
Lively Member
distance between two zip codes
Hey guys. Just wanted to know if someone can give me an idea of how to calculate the distance between two zip codes. I was searching the web for information and the only thing I have found is what follows. Can anyone tells me how to convert this into vb.net code? ( I am pretty new at this).
Thanks in advance
code:
#:::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
#::: :::
#::: This routine calculates the distance between two points (given the :::
#::: latitude/longitude of those points). It is being used to calculate :::
#::: the distance between two ZIP Codes or Postal Codes using our :::
#::: ZIPCodeWorld(TM) and PostalCodeWorld(TM) products. :::
#::: :::
#::: Definitions: :::
#::: South latitudes are negative, east longitudes are positive :::
#::: :::
#::: Passed to function: :::
#::: lat1, lon1 = Latitude and Longitude of point 1 (in decimal degrees) :::
#::: lat2, lon2 = Latitude and Longitude of point 2 (in decimal degrees) :::
#::: unit = the unit you desire for results :::
#::: where: 'M' is statute miles :::
#::: 'K' is kilometers (default) :::
#::: 'N' is nautical miles :::
#::: :::
#::: United States ZIP Code/ Canadian Postal Code databases with latitude :::
#::: & longitude are available at http://www.zipcodeworld.com :::
#::: :::
#::: For enquiries, please contact [email protected] :::
#::: :::
#::: Official Web site: http://www.zipcodeworld.com :::
#::: :::
#::: Hexa Software Development Center © All Rights Reserved 2004 :::
#::: :::
#:::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
$pi = atan2(1,1) * 4;
sub distance {
my ($lat1, $lon1, $lat2, $lon2, $unit) = @_;
my $theta = $lon1 - $lon2;
my $dist = sin(deg2rad($lat1)) * sin(deg2rad($lat2)) + cos(deg2rad($lat1)) * cos(deg2rad($lat2)) * cos(deg2rad($theta));
$dist = acos($dist);
$dist = rad2deg($dist);
$dist = $dist * 60 * 1.1515;
if ($unit eq "K") {
$dist = $dist * 1.609344;
} elsif ($unit eq "N") {
$dist = $dist * 0.8684;
}
return ($dist);
}
#::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
#::: This function get the arccos function using arctan function :::
#::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
sub acos {
my ($rad) = @_;
my $ret = atan2(sqrt(1 - $rad**2), $rad);
return $ret;
}
#::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
#::: This function converts decimal degrees to radians :::
#::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
sub deg2rad {
my ($deg) = @_;
return ($deg * $pi / 180);
}
#::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
#::: This function converts radians to decimal degrees :::
#::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
sub rad2deg {
my ($rad) = @_;
return ($rad * 180 / $pi);
}
print distance(32.9697, -96.80322, 29.46786, -98.53506, "M") . " Miles\n";
print distance(32.9697, -96.80322, 29.46786, -98.53506, "K") . " Kilometers\n";
print distance(32.9697, -96.80322, 29.46786, -98.53506, "N") . " Nautical Miles\n";
None for now 
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|