I'm trying to use the haversine formula in my PHP/MySQL to get nearby locations to where I am
I'm using AJAX to get only nearby markers on my google map.
What I know is that my query isn't returning anything.
I know that there is a location within the distance of where I'm looking.
Basically, what's wrong with my code using the haversine formula?
EDIT - Changed a ton to follow tutorial from here. The error is still the same.
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);
Error
<b>Warning</b>: mysqli_fetch_assoc() expects parameter 1 to be mysqli_result, boolean given ... null