获得经度值为null。 PHP

时间:2016-08-09 11:23:53

标签: php mysqli google-geolocation

我有一张场地表,其中有纬度,经度列,表示它的位置。我想从场地表中获取位置,并获得靠近用户当前位置的位置。

当前位置将作为函数的参数给出。

对于第一次,我试图从场地表中获得所有纬度和经度,并创建一个数组。

然后我尝试计算场地阵列中每个场地的距离。但距离数组似乎是0,0。 $ lat1,$ lon1,$ lat2,$ lon2的值为空。

我是php的新手,所以不知道怎么做。

我想返回离用户当前位置近10公里的位置。

搜索功能:

  public function searchVendors($lat1,$lon1)
    {
      //  $lat2 = -10.4212157;
      //  $lon2 = 28.6031842;

      // $lat1 = 19.9950050;
      // $lon1 = 73.8413080;

    try{

   //  $lat1 = $fields -> lat1;
   //  $lon1 = $fields -> lon1;

     $con = DB::getConnection();

     $query = "SELECT `lattitude`,`longitude` FROM venues";

     $rs = mysqli_query($con,$query) or die (json_encode(array("result"=>-1, "message"=>mysqli_error())));
     $n = mysqli_num_rows($rs);

     $venues = array();

     if ( $n > 0 ) {

     while ( $row = mysqli_fetch_assoc($rs)) {
        $venues[] = $row;
     }

     $distances = array();

     foreach($venues as $venue)
     {
        $lat2 = $venue -> lattitude;
        $lon2 = $venue -> longitude;

        $dist = $this -> distance($lat1, $lon1, $lat2, $lon2);

        $distances[] = $dist;

     }
        echo $lat1;
        echo $lon1;
        echo $lat2;
        echo $lon2;

        $result = array("lat1" => $lat1,"lon1" =>$lon1,"lat2" => $lat2 ,"lon2" => $lon2, "result"=>1, "message"=>"success", "distances" => $distances);
        return json_encode($result);

     } else {
        $result = array("result"=>-1, "message"=>"Distances list is empty");
        return json_encode($result);
     }
     } catch(DBConnectionException $e) {
        $result = array("result"=>-1, "message"=> $e -> getMessage());
        return json_encode($result);
     }
        return null;
    }

getVendors php

   <?php

header("Content-type: application/json");

    if ( $_SERVER['REQUEST_METHOD']=='POST') {
        include_once ("../include/Vendor.php");
    try {

    $con = DB::getConnection();

    $raw = file_get_contents("php://input");
    $data = json_decode($raw, true);

    $lat1 = $data -> lattitude;
    $lon1 = $data -> longitude;

    echo $lat1;
    echo $lon1;

    $v = new Vendor();
    $response = $v -> searchVendors($lat1,$lon1);

    json_encode($response);


    if ( $response == null ) {
            $response = json_encode(array("result" => -2, "message" => "Empty result"));
            echo $response;
        } else {
            echo $response;
        }
    } catch(Exception $e) {
        $result = array("result" => -1, "message" => $e -> getMessage());
        echo json_encode($result);
        }
}

?>

我从邮递员传来的输入是:

{

    "lat1" : "19.9764360",
    "lon1" : "73.8573440"

}

我得到的输出是:

{
  "lat1": null,
  "lon1": null,
  "lat2": null,
  "lon2": null,
  "result": 1,
  "message": "success",
  "distances": [
    0,
    0,
    0,
    0,
    0,
    0,
    0,
    0
  ]
}

编辑:正如RigsFolly所回答的

getVendors php:

    <?php

header("Content-type: application/json");

    if ( $_SERVER['REQUEST_METHOD']=='POST') {
        include_once ("../include/Vendor.php");
    try {

    $con = DB::getConnection();

    $raw = file_get_contents("php://input");
    $data = json_decode($raw);          // now its an object if it was stored as an object

    $lat1 = $data -> lattitude;
    $lon1 = $data -> longitude;

    echo $lat1;
    echo $lon1;

    $v = new Vendor();
    $response = $v -> searchVendors($data);

    json_encode($response);

    if ( $response == null ) {
            $response = json_encode(array("result" => -2, "message" => "Empty result"));
            echo $response;
        } else {
            echo $response;
        }
    } catch(Exception $e) {
        $result = array("result" => -1, "message" => $e -> getMessage());
        echo json_encode($result);
        }
}

?>

搜索功能:

  public function searchVendors($fields)
    {

    try{

     $con = DB::getConnection();

     $query = "SELECT `lattitude`,`longitude` FROM venues";

     $rs = mysqli_query($con,$query) or die (json_encode(array("result"=>-1, "message"=>mysqli_error())));
     $n = mysqli_num_rows($rs);

     $venues = array();

     $lat1 = $fields -> lattitude;
     $lon1 = $fields -> longitude;

     if ( $n > 0 ) {

     while ( $row = mysqli_fetch_object($rs)) {
    $venues[] = $row;
 }

 $distances = array();

 foreach($venues as $venue)
 {
    $lat2 = $venue -> lattitude;
    $lon2 = $venue -> longitude;

    $dist = $this -> distance($lat1, $lon1, $lat2, $lon2);

    $distances[] = $dist;

 }
        echo $lat1;
        echo $lon1;
        echo $lat2;
        echo $lon2;

        $result = array("lat1" => $lat1,"lon1" =>$lon1,"lat2" => $lat2 ,"lon2" => $lon2, "result"=>1, "message"=>"success", "distances" => $distances);
        return json_encode($result);

     } else {
        $result = array("result"=>-1, "message"=>"Distances list is empty");
        return json_encode($result);
     }
     } catch(DBConnectionException $e) {
        $result = array("result"=>-1, "message"=> $e -> getMessage());
        return json_encode($result);
     }
        return null;
    }

我的输出为:

    {
  "lat1": null,
  "lon1": null,
  "lat2": "-2.6357434",
  "lon2": "18.4276047",
  "result": 1,
  "message": "success",
  "distances": [
    8321.16,
    8322.48,
    8321.37,
    2617.96,
    0,
    2535.52,
    3368.05,
    2069.09
  ]
}

lat1,lon1似乎仍然是空的,距离是对的吗?我怎么能以公里数得到它?

距离函数:

  public function distance($lat1, $lon1, $lat2, $lon2) {

        $theta = $lon1 - $lon2;
        $dist = sin(deg2rad($lat1)) * sin(deg2rad($lat2)) +  cos(deg2rad($lat1)) * cos(deg2rad($lat2)) * cos(deg2rad($theta));
        $dist = acos($dist);
        $dist = rad2deg($dist);
        $miles = $dist * 60 * 1.1515;
        $unit = strtoupper($unit);

        $km = $miles * 1.609344;

        $result = round($km ,2);

        return $result;
    }

这里有什么问题?有人可以帮忙吗?谢谢..

1 个答案:

答案 0 :(得分:1)

您使用了mysqli_fetch_assoc,因此$row将是一个关联数组,而不是一个对象。因此,请使用mysqli_fetch_object或将其引用为$lat2 = $venue['lattitude'];

我建议修改mysqli_fetch_assocmysqli_fetch_object,修改更少的代码!

 //while ( $row = mysqli_fetch_assoc($rs)) {
 while ( $row = mysqli_fetch_object($rs)) {
    $venues[] = $row;
 }

 $distances = array();

 foreach($venues as $venue)
 {
    $lat2 = $venue -> lat1;     // name corrected
    $lon2 = $venue -> lon1;     // name corrected

    $dist = $this -> distance($lat1, $lon1, $lat2, $lon2);

    $distances[] = $dist;

 }

同样在getVendors中,您将json字符串转换为数组。

您将它作为对象从javascript传递,因此请将其用作一个,如此

//$data = json_decode($raw, true);  // true = convert to array
$data = json_decode($raw);          // now its an object if it was stored as an object

$lat1 = $data -> lattitude;
$lon1 = $data -> longitude;