基本上,我试图找到一个特定的“中心城市”与该州所有其他城市之间的距离(以英里为单位)。因此,我正在占领一个城市并在该城市和多个城市之间找到多个距离。
我正在使用Haversine函数,当我简单地为每个变量分配纬度和经度时,它产生正确的结果,如下所示。但是,当我从数据库中检索多个纬度和经度时,距离结果从0-300范围变为4,500-5,000范围,非常偏离。
//Works as it should
$center_lat = 30.332184;
$center_lng = -81.655651;
$lat = 29.901244;
$lng = -81.312434;
function haversineGreatCircleDistance(
$latitudeFrom, $longitudeFrom, $latitudeTo, $longitudeTo, $earthRadius = 3961000)
{
// convert from degrees to radians
$latFrom = deg2rad($latitudeFrom);
$lonFrom = deg2rad($longitudeFrom);
$latTo = deg2rad($latitudeTo);
$lonTo = deg2rad($longitudeTo);
$latDelta = $latTo - $latFrom;
$lonDelta = $lonTo - $lonFrom;
$angle = 2 * asin(sqrt(pow(sin($latDelta / 2), 2) +
cos($latFrom) * cos($latTo) * pow(sin($lonDelta / 2), 2)));
return $angle * $earthRadius;
}
$distance = haversineGreatCircleDistance($center_lat, $center_lng, $lat, $lng, 3961);
print($distance) . "<br/><br/>";
// The output should be between 35-36
这是我的代码 - 问题似乎来自第二个SQL查询或while循环:
// This first section works - I also have an input form to get the $_POST['state'] and $_POST['city'] which works fine
if($_SERVER["REQUEST_METHOD"] == "POST") {
$state = $_POST['state'];
$city = $_POST['city'];
$sql = "SELECT DISTINCT lat, lon FROM foodtruck_user WHERE city = '$city' AND state = '$state' LIMIT 1";
$result = mysqli_query($connection, $sql);
while($row = mysqli_fetch_array($result)) {
echo $row['lat'] . "<br/>";
echo $row['lon'] . "<br/><br/>";
$center_lat = $row['lat'];
$center_lng = $row['lon'];
}
// Below is where the I am having the issue...the output from "echo" is accurate, but the calculated $distance is very wrong.
$sql2 = "SELECT DISTINCT lat, lon, city FROM foodtruck_user WHERE state = '$state'";
$result2 = mysqli_query($connection, $sql2);
while($row2 = mysqli_fetch_array($result2)) {
if(!empty($row2['lat']) && !empty($row2['lon']) && !empty($row2['city'])){
echo "Lat: " . $row2['lat'] . " Lon: " . $row2['lon'] . " City: " . $row2['city'] . " center_lat: " . $center_lat . " center_lng: " . $center_lng . " Distance: ";
$lat = $row2['lat'];
$lon = $row2['lon'];
$distance = haversineGreatCircleDistance($center_lat, $center_lng, $lat, $lng, 3961);
print($distance) . "<br/><br/>";
}
}
}
以下是浏览器中输出的一些示例...一切都是正确的,除了“距离”,这是我提出问题的地方...为什么变量被准确回显,但是函数的结果是不准确的?
30.329882
-81.651672
Lat: 30.329882 Lon: -81.651672 City: Jacksonville center_lat: 30.329882 center_lng: -81.651672 Distance: 4749.7501036981 // distance should be 0
Lat: 27.961381 Lon: -82.45972 City: Tampa center_lat: 30.329882 center_lng: -81.651672 Distance: 4816.3124301703
Lat: 26.231529 Lon: -80.12346 City: Pompano Beach center_lat: 30.329882 center_lng: -81.651672 Distance: 4866.2490051465
Lat: 26.113536 Lon: -80.224272 City: Plantation center_lat: 30.329882 center_lng: -81.651672 Distance: 4869.6942811343
Lat: 28.539882 Lon: -81.372668 City: Orlando center_lat: 30.329882 center_lng: -81.651672 Distance: 4799.8575314421
Lat: 26.51747 Lon: -80.083427 City: Boynton Beach center_lat: 30.329882 center_lng: -81.651672 Distance: 4857.9202539164
Lat: 26.249221 Lon: -80.211483 City: Margate center_lat: 30.329882 center_lng: -81.651672 Distance: 4865.7328403355
答案 0 :(得分:3)
在调用您的函数时,您使用 $ lng 而不是 $ lon ,但未定义,因此将其作为0传递 - 导致您的计算成为不准确的。 从上面复制:
$lon = $row2['lon'];
$distance = haversineGreatCircleDistance($center_lat, $center_lng, $lat, $lng, 3961);
错别道,伙计。错字。