sql数据库中的空间查询

时间:2015-01-03 16:53:31

标签: spatial-query mysql-spatial

您好我正在开发基于地理位置的应用程序,它更新用户的位置并显示他周围的商店,我在sql数据库中使用普通的SQL查询。我的PHP代码选择用户周围的商店是:         

 /*
    * Following code will list all the products
  */

 // array for JSON response
 $response = array();

// include db connect class
 require_once __DIR__ . '/db_connect.php';

 // connecting to db
  $db = new DB_CONNECT();
  if (isset($_POST['code']) && isset($_POST['lat']) && isset($_POST['lng'])) {
 $code = $_POST['code'];
   $lng = $_POST['lng'];
  $lat = $_POST['lat'];

//从表中获取所有数据这是我的代码,用于获取需要更改的数据

         $result = mysql_query("SELECT *, ( 3959 * acos( cos( radians($lat) ) * cos(   radians(   lat ) ) * cos( radians(lng) - radians($lng) ) + sin( radians($lat) ) * sin(     radians(lat))))        AS distance 
  FROM    maintable 
  HAVING distance < 25 
 ORDER BY distance
  ") or die(mysql_error());

//检查空结果

     if (mysql_num_rows($result) > 0) {
    // looping through all results
    // products node
   $response["people"] = array();

while ($row = mysql_fetch_array($result)) {
    // temp user array
    $people = array();
    $product["_id"] = $row["_id"];
    $product["name"] = $row["name"];
    $product["distance"] = $row["distance"];
    $product["lat"] = $row["lat"];
    $product["lng"] = $row["lng"];
    $product["image_bit"] = $row["image_bit"];
    $product["security"] = $row["security"];
    $product["status"] = $row["status"];
    $product["code"] = $row["country_code"];
    $product["phone"] = $row["phone"];


    // push single product into final response array
    array_push($response["people"], $people);
  }
// success
    $response["success"] = 1;

   // echoing JSON response
   echo json_encode($response);
   } else {
  // no products found
  $response["success"] = 0;
  $response["message"] = "No people found";

// echo no users JSON
  echo json_encode($response);
   }
 }
      else{
   //required field is missing
   $response["success"] = 0;
   $response["message"] = "Required field missing";
   // echoing JSON response
   echo json_encode($response);
    }

?>

我将用户的纬度和经度值分别存储为lat和lng。   但我最近听说过sql中的空间数据类型,这将更有效,我需要知道的是,如果我需要使用空间查询,我应该在sql表中的lat和lng列中进行哪些更改,并且可以请任何人修改mysql查询作为一个空间查询。任何帮助都会很明显

1 个答案:

答案 0 :(得分:0)

你可以尝试一个四核。将点转换为二进制并交错。将其视为基数为4的数字。四元组类似于四叉树,但它也具有有趣的属性。它通常用于平铺。

相关问题