我在这里找到了一些代码,可以在数学上找到两个邮政编码之间的距离。我正在使用 Google geocode api 从每个邮政编码中获取经纬度。一切正常,但我想知道如何将$distance
回报转换为英里:
function calc_distance($zip1, $zip2)
{
$geo = new GoogleGeocode( $apiKey );
$point1 = $geo->geocode( $zip1 );
$point1 = $point1['Placemarks'][0];
echo "P1:"; print_r( $zip1 );
$point2 = $geo->geocode( $zip2 );
$point2 = $point2['Placemarks'][0];
echo "<br>P2: "; print_r( $zip2 ); echo "<br>";
var_dump($point1);
echo "<br><br>";
var_dump($point2);
echo "<br>";
$radius = 3958; // Earth's radius (miles)
$deg_per_rad = 57.29578; // Number of degrees/radian (for conversion)
$distance = ($radius * pi() * sqrt(
($point1['Latitude'] - $point2['Latitude'])
* ($point1['Latitude'] - $point2['Latitude'])
+ cos($point1['Latitude'] / $deg_per_rad) // Convert these to
* cos($point2['Latitude'] / $deg_per_rad) // radians for cos()
* ($point1['Longitude'] - $point2['Longitude'])
* ($point1['Longitude'] - $point2['Longitude'])
) / 180);
echo $distance;
return $distance; // Returned using the units used for $radius.
}
当我测试使用:calc_distance(28025, 22044);
它返回:
P1:28025
P2: 22044
array(7) { ["Accuracy"]=> int(5) ["Country"]=> string(2) "US" ["AdministrativeArea"]=> string(2) "NC" ["Locality"]=> string(7) "Concord" ["PostalCode"]=> int(28025) ["Latitude"]=> float(35.3898417) ["Longitude"]=> float(-80.5216184) }
array(7) { ["Accuracy"]=> int(5) ["Country"]=> string(2) "US" ["AdministrativeArea"]=> string(2) "VA" ["Locality"]=> string(12) "Falls Church" ["PostalCode"]=> int(22044) ["Latitude"]=> float(38.8607388) ["Longitude"]=> float(-77.1571443) }
302.952750233