具有MySQL的Haversine Fomula到达附近位置 [英] Haversine Fomula with MySQL get nearby locations

查看:97
本文介绍了具有MySQL的Haversine Fomula到达附近位置的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试在PHP/MySQL中使用Haversine公式将附近的位置移到我所在的位置

I'm trying to use the haversine formula in my PHP/MySQL to get nearby locations to where I am

我正在使用AJAX在我的Google地图上仅获取附近的标记.

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.

基本上,使用Haversine公式的代码有什么问题?

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);

错误

<b>Warning</b>:  mysqli_fetch_assoc() expects parameter 1 to be mysqli_result, boolean given ... null

推荐答案

哇.我是个白痴.

有问题的代码可以正常工作.我只是为SELECT

Code in question works perfectly. I just was using the wrong table name and column names for the SELECT

希望这对以后需要帮助的人很有用.

Hope this is useful to future people who need help.

这篇关于具有MySQL的Haversine Fomula到达附近位置的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆