【发布时间】:2013-06-27 21:13:26
【问题描述】:
我正在尝试在我的 PHP/MySQL 中使用半正弦公式来获取附近的位置到我所在的位置
我使用 AJAX 仅在我的谷歌地图上获取附近的标记。
我知道我的查询没有返回任何内容。
我知道在我正在寻找的距离内有一个位置。
基本上,我的代码使用 hasrsine 公式有什么问题?
编辑 - 更改了大量从 here 开始的教程。错误还是一样。
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
【问题讨论】:
-
这个问题似乎是题外话,因为代码工作正常,OP 只是忘记更改表/列名称
-
起初情况并非如此,但似乎 OP 更感兴趣的是寻找工作代码而不是试图理解他在做什么:(顺便说一句,不错的教程,介绍了带有返回全局变量的方法的类:D
-
我想问一下代码有什么问题。我最终从 tut 中改变了一些东西,但仍然遇到了一些问题。是的,最终结果是一个愚蠢的错误,但@dev-null-dweller 是对的,我关注的链接帮助我了解了类和全局变量。
-
哪个链接帮助您了解课程?如果你指的是你发布的那个(snipe.net),我很抱歉,但我的评论是讽刺的。本教程中类的使用是错误的、毫无意义的,当然你不应该尝试从中学习类和 OOP。
标签: php mysql location google-maps-markers distance