选择特定距离内的点

时间:2012-01-03 14:26:57

标签: php mysql

我的数据库中有很多优惠。我想通过发送参数“lat”“lon”和半径来从数据库中选择一些特定的交易。 这个查询不适合用它来做什么..

  require('includes/connection.php');  // defines $dsn, $username, $password      
  $lat = $_GET['lat'];  // latitude of centre of bounding circle in degrees
  $lon = $_GET['lon'];  // longitude of centre of bounding circle in degrees
  $rad = $_GET['rad'];  // radius of bounding circle in kilometers
  $R = 6371;  // earth's radius, km
  // first-cut bounding box (in degrees)
  $maxLat = $lat + rad2deg($rad/$R);
  $minLat = $lat - rad2deg($rad/$R);
  // compensate for degrees longitude getting smaller with increasing latitude
  $maxLon = $lon + rad2deg($rad/$R/cos(deg2rad($lat)));
  $minLon = $lon - rad2deg($rad/$R/cos(deg2rad($lat)));

  // convert origin of filter circle to radians
  $lat = deg2rad($lat);
  $lon = deg2rad($lon);
  $sql = "    Select * From  From deals
      Where lat>$minLat And lat<$maxLat
        And lon>$minLon And lon<$maxLon
    Where acos(sin($lat)*sin(radians(lat)) + cos($lat)*cos(radians(lat))*cos(radians(lon)-$lon))*$R < $rad";
echo "Query =".$sql;  
  $points = mysql_query($sql);

2 个答案:

答案 0 :(得分:3)

它没有用,因为你有2个“WHERE”语句。您可以将第二个“WHERE”更改为“AND”。

答案 1 :(得分:2)

需要进行小规模更改将第二个where替换为and。我们不能在此处使用两个where子句。 aS也不允许使用两个from子句。

尝试以下查询。

Select * From  deals 
Where lat>$minLat 
And lat<$maxLat 
And lon>$minLon 
And lon<$maxLon 
and acos(sin($lat)*sin(radians(lat))+cos($lat)*cos(radians(lat))*cos(radians(lon)-$lon))*$R < $rad";`