Free tool â€“ Veeam Explorer for Microsoft SharePoint, enables fast, easy restores of SharePoint sites, documents, libraries and lists â€” all with no agents to manage and no additional licenses to buy.
Private Function DistanceInMeters(lat1 As Double, long1 As Double, lat2 As Double, long2 As Double) As Double
' Takes lat/longs as expressions in Degrees
' approx radius of Earth in meters. True radius varies from
' 6357km (polar) to 6378km (equatorial).
Const earth_radius = 6367000
Dim rLat1 As Double
Dim rLong1 As Double
Dim rLat2 As Double
Dim rLong2 As Double
rLat1 = DegreesToRadians(lat1)
rLong1 = DegreesToRadians(long1)
rLat2 = DegreesToRadians(lat2)
rLong2 = DegreesToRadians(long2)
' Using Radians
' less subject to rounding errors for small distances
DistanceInMeters = (2 * asin(Sqr((Sin((rLat1 - rLat2) / 2)) ^ 2 + Cos(rLat1) * Cos(rLat2) * (Sin((rLong1 - rLong2) / 2)) ^ 2))) * earth_radius
End Function
Private Function asin(X As Double) As Double
asin = Atn(X / Sqr(-X * X + 1))
End Function
Private Function DegreesToRadians(X As Double) As Double
Const pi = 3.14159265358979
'To convert degrees to radians, multiply degrees by pi/180.
DegreesToRadians = X * pi / 180
End Function
// FUNCTION TO COMPUTE DISTANCE FROM ONE ZIP TO ANOTHER
function compute_zip_distance($from_zip, $to_zip, $source='Z')
{
global $db_connection;
if ("$from_zip" == "$to_zip")
{
return 0.0;
}
$dbt = 'RAY_ZIPCODES';
$fsql = "SELECT latitude, longitude from $dbt WHERE zip = \"$from_zip\" ";
if (!$f = mysql_query("$fsql", $db_connection)) { fatal_query_error($fsql); }
$frow = mysql_fetch_assoc($f);
$f_lat = $frow["latitude"];
$f_lon = $frow["longitude"];
$tsql = "SELECT latitude, longitude from $dbt WHERE zip = \"$to_zip\" ";
if (!$t = mysql_query("$tsql", $db_connection)) { fatal_query_error($tsql); }
$trow = mysql_fetch_assoc($t);
$t_lat = $trow["latitude"];
$t_lon = $trow["longitude"];
if ( (!mysql_num_rows($f)) || (!mysql_num_rows($t)) ) return FALSE;
return compute_distance($f_lat, $f_lon, $t_lat, $t_lon);
}
// FUNCTION TO COMPUTE DISTANCE FROM ONE LAT/LON PAIR TO ANOTHER
function compute_distance($from_lat, $from_lon, $to_lat, $to_lon)
{
if ( ($from_lat == $to_lat) && ($from_lon == $to_lon) )
{
return 0.0;
}
$from_lat = floatval($from_lat);
$from_lon = floatval($from_lon);
$to_lat = floatval($to_lat);
$to_lon = floatval($to_lon);
$dist = acos( sin(deg2rad($from_lat))
* sin(deg2rad($to_lat))
+ cos(deg2rad($from_lat))
* cos(deg2rad($to_lat))
* cos(deg2rad($from_lon - $to_lon)) );
$dist = rad2deg($dist);
$miles = (float) $dist * 69.0;
return round($miles,1);
// To get kilometers, multiply miles by 1.61
// $km = (float) $miles * 1.61;
// return round($km,2);
}
// SOME ASSUMPTIONS
// INPUT ZIP CODE IS IN VAR $my_zip
// $my_zip LAT/LON FIELDS ARE IN VARS $my_lat AND $my_lon
// DATA BASE TABLE HAS z_zip ZIP CODE with z_lat AND z_lon FIELDS
// SOME LOCAL FUNCTIONS ARE USED IN CASE OF QUERY ERRORS
// CONSTANTS FOR SOME ROOM AROUND OUR ZIP CODE - GIVES SEVERAL MILES FOR PROXIMITY SEARCH
DEFINE("LATITUDE_OFFSET", 0.20000);
DEFINE("LONGITUDE_OFFSET", 0.20000);
// COMPUTE THE MIN AND MAX LAT/LON PAIRS
$minlat = $my_lat - LATITUDE_OFFSET;
$maxlat = $my_lat + LATITUDE_OFFSET;
$minlon = $my_lon - LONGITUDE_OFFSET;
$maxlon = $my_lon + LONGITUDE_OFFSET;
// FIND NEARBY ZIP CODES AND STORE IN A SMALL TEMPORARY TABLE
$tsql = "CREATE TEMPORARY TABLE nearbyZipCodes ( ";
$tsql .= "distance decimal(6,1) ";
$tsql .= " ) ENGINE=MEMORY SELECT * FROM ZipCodes WHERE ";
$tsql .= "( z_lat BETWEEN $minlat AND $maxlat ) AND ( z_lon BETWEEN $minlon AND $maxlon ) ";
if (!$t = mysql_query("$tsql", $db_connection)) { fatal_query_error($tsql); }
if (!$t = mysql_query('SELECT COUNT(*) FROM nearbyZipCodes', $db_connection)) { fatal_query_error($tsql); }
$trow = mysql_fetch_array($t, MYSQL_NUM);
$tcount = $trow[0];
if ($tcount == 0)
{
die("Sorry, there is nothing close to that ZIP code");
}
// COMPUTE THE DISTANCE FOR EACH NEARBY ZIP CODE AND UPDATE THE TEMPORARY TABLE
$dsql = "SELECT * FROM nearbyZipCodes";
if (!$d = mysql_query("$dsql", $db_connection)) { fatal_query_error($dsql); }
while ($drow = mysql_fetch_assoc($d))
{
extract ($drow);
$dist = number_format(compute_distance( $my_lat, $my_lon, $z_lat, $z_lon ),1);
$usql = "UPDATE nearbyZipCodes SET distance = $dist WHERE key = $key LIMIT 1";
if (!$u = mysql_query("$usql", $db_connection)) { fatal_query_error($usql); }
}
// ITERATE OVER RESULTS SET FOR DISTANCES OF 5 MILES OR LESS
$rsql = "SELECT * FROM nearbyZipCodes WHERE distance < 5.1 ORDER BY distance ASC";
if (!$r = mysql_query("$rsql", $db_connection)) { fatal_query_error($rsql); }
while ($rrow = mysql_fetch_assoc($r))
{
extract ($rrow);
echo "<br/>$my_zip IS $distance MILES FROM $z_zip";
}
function DistanceInMeters($lat1, $long1, $lat2, $long2) {
// Takes lat/longs as expressions in Degrees
// approx radius of Earth in meters. True radius varies from
// 6357km (polar) to 6378km (equatorial).
$earth_radius = 6367000;
$pi = 3.14159265358979;
$rLat1 = 0.0;
$rLong1 = 0.0;
$rLat2 = 0.0;
$rLong2 = 0.0;
$result = 0.0;
$rLat1 = deg2rad($lat1);
$rLong1 = deg2rad($long1);
$rLat2 = deg2rad($lat2);
$rLong2 = deg2rad($long2);
// Using Radians
// less subject to rounding errors for small distances
$result = (2 * asin(sqrt(pow(sin(($rLat1 - $rLat2) / 2), 2) + cos($rLat1) * cos($rLat2) * pow(sin(($rLong1 - $rLong2) / 2),2)))) * $earth_radius;
return($result);
}
strZip = "98277"
lngDistance = 50
strCalculation = "((1.852*60*(((Atn(-(sin(zc1.RLat)*sin(zc2.RLat)+cos(zc1.RLat)*cos(zc2.RLat)*cos(abs((zc2.RLong)-(zc1.RLong))))/Sqr(-(sin(zc1.RLat)*sin(zc2.RLat)+cos(zc1.RLat)*cos(zc2.RLat)*cos(abs((zc2.RLong)-(zc1.RLong))))*(sin(zc1.RLat)*sin(zc2.RLat)+cos(zc1.RLat)*cos(zc2.RLat)*cos(abs((zc2.RLong)-(zc1.RLong))))+1))+2*Atn(1))/3.14159265358979)*180))/1.609344)"
strSQL = "SELECT DISTINCT zc2.ZIPCODE" & vbCrLf & _
" , " & strCalculation & " AS Distance" & vbCrLf & _
"FROM ZipCodes AS zc1, ZipCodes AS zc2" & vbCrLf & _
"WHERE zc1.ZIPCODE=" & Chr(39) & strZip & Chr(39) & vbCrLf & _
"AND zc2.ZIPCODE<>" & Chr(39) & strZip & Chr(39) & vbCrLf & _
"AND " & strCalculation & "<=" & lngDistance & vbCrLf
If you are experiencing a similar issue, please ask a related question
Join the community of 500,000 technology professionals and ask your questions.