我正在尝试在我的 PHP/MySQL 中使用 hasrsine 公式来获取附近的位置到我所在的位置
我正在使用 AJAX 仅在我的谷歌地图上获取附近的标记。
我所知道的是我的查询没有返回任何内容。
我知道在我正在寻找的距离内有一个位置。
基本上,我的代码使用半正弦公式有什么问题?
编辑 - 从这里更改了大量以遵循教程。错误仍然相同。
PHP
$lati = 40.78405877579076;
$longi = -73.94800172194275;
class RadiusCheck
{
var $maxLat;
var $minLat;
var $maxLong;
var $minLong;
function RadiusCheck($Latitude, $Longitude, $Miles)
{
global $maxLat,$minLat,$maxLong,$minLong;
$EQUATOR_LAT_MILE = 69.172;
$maxLat = $Latitude + $Miles / $EQUATOR_LAT_MILE;
$minLat = $Latitude - ($maxLat - $Latitude);
$maxLong = $Longitude + $Miles / (cos($minLat * M_PI / 180) * $EQUATOR_LAT_MILE);
$minLong = $Longitude - ($maxLong - $Longitude);
}
function MaxLatitude()
{
return $GLOBALS["maxLat"];
}
function MinLatitude()
{
return $GLOBALS["minLat"];
}
function MaxLongitude()
{
return $GLOBALS["maxLong"];
}
function MinLongitude()
{
return $GLOBALS["minLong"];
}
}
class DistanceCheck
{
function DistanceCheck()
{
}
function Calculate($dblLat1, $dblLong1, $dblLat2, $dblLong2)
{
$EARTH_RADIUS_MILES = 3963;
$dist = 0;
//convert degrees to radians
$dblLat1 = $dblLat1 * M_PI / 180;
$dblLong1 = $dblLong1 * M_PI / 180;
$dblLat2 = $dblLat2 * M_PI / 180;
$dblLong2 = $dblLong2 * M_PI / 180;
if ($dblLat1 != $dblLat2 || $dblLong1 != $dblLong2)
{
//the two points are not the same
$dist = sin($dblLat1) * sin($dblLat2) + cos($dblLat1) * cos($dblLat2) * cos($dblLong2 - $dblLong1);
$dist = $EARTH_RADIUS_MILES * (-1 * atan($dist / sqrt(1 - $dist * $dist)) + M_PI / 2);
}
return $dist;
}
}
// set a default number of miles to search within
$Miles = '2';
// set the user's latitude and longitude as the one to search against
$Latitude = $lati;
$Longitude = $longi;
$zcdRadius = new RadiusCheck($Latitude,$Longitude,$Miles);
$minLat = $zcdRadius->MinLatitude();
$maxLat = $zcdRadius->MaxLatitude();
$minLong = $zcdRadius->MinLongitude();
$maxLong = $zcdRadius->MaxLongitude();
$sql = "SELECT `uname`, `it`, `name`, SQRT((((69.1*(latitude-$Latitude))*(69.1*(latitude-$Latitude)))+((53*(longitude-$Longitude))*(53*(longitude-$Longitude))))) AS calc FROM stores WHERE latitude >= '$minLat' AND latitude <= '$maxLat' AND longitude >= '$minLong' AND longitude <= '$maxLong'";
$get_data = mysqli_query($conn, $sql);
while($row = mysqli_fetch_assoc($get_data))
{
// calculate the number of miles away the result is
$zcdDistance = new DistanceCheck;
$Distance = $zcdDistance->Calculate($Latitude, $Longitude, $row['latitude'], $row['longitude']);
$lat = $row['lat'];
$lon = $row['lon'];
$name = $row['name'];
$it = $row['it'];
$uname = $row['uname'];
$data["markers"][] = array
(
"latitude" => $lat,
"longitude" => $lon,
"name" => $name,
"it" => $it,
"uname" => $uname
);
}
echo json_encode($data);
错误
<b>Warning</b>: mysqli_fetch_assoc() expects parameter 1 to be mysqli_result, boolean given ... null