mysql pdo距离经度和纬度

时间:2015-03-05 11:32:25

标签: php mysql pdo latitude-longitude

我有一个用户表,每个用户的地理位置都存储为一对经度和纬度。

我想向圆形区域内的所有用户显示已知的中心和半径。以下代码是我目前所拥有的,但它没有给出正确的答案:

$lat = 54.635214;
$lon = -3.551677;
$bounding_distance = 10;

$sql = "SELECT *
            ,((ACOS(SIN(:find_lat * PI() / 180) * SIN(`latitude` * PI() / 180) + COS(:find_lat * PI() / 180) * COS(`latitude` * PI() / 180) * COS((:find_long - `longitude`) * PI() / 180)) * 180 / PI()) * 60 * 1.15078) AS `distance` 
            FROM `user` 
            WHERE
                `latitude` BETWEEN (:find_lat - :bounding_distance) AND (:find_lat + :bounding_distance)
                AND `longitude` BETWEEN (:find_long - :bounding_distance) AND (:find_long + :bounding_distance)
                AND `distance` <= 100
            ORDER BY `distance` ASC"; 

$stmt = $this->pdo->prepare($sql);

$stmt->bindValue(':find_lat', $lat);
$stmt->bindValue(':find_long', $lon);
$stmt->bindValue(':bounding_distance', $bounding_distance);

$stmt->execute();
return $stmt->fetchAll();

0 个答案:

没有答案