-3

PHP/MySQL で hasersine 式を使用して、近くの場所を取得しようとしています。

AJAX を使用して、Google マップで近くのマーカーのみを取得しています。

私が知っているのは、私のクエリが何も返していないということです。

私が見ている場所の距離内に場所があることを知っています。

基本的に、harsine 式を使用したコードの何が問題になっていますか?

編集 - 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
4

2 に答える 2

5

あなたのSQLにエラーがあります。$resultに渡す前に確認する必要がありmysqli_fetch_assocます。

問題は、演算子が 1 つ不足しているか、括弧が一致していないことです。cos()radians(lon)

于 2013-06-30T17:10:17.340 に答える
-1

わお。私はアホです。

問題のコードは完全に機能します。間違ったテーブル名と列名を使用していましたSELECT

これが助けを必要とする将来の人々に役立つことを願っています。

于 2013-06-30T18:44:29.337 に答える