【发布时间】:2011-12-02 03:11:19
【问题描述】:
我想要做的是我在数据库中有条目,其中存储了纬度/经度。我想计算用户 lat/long 和条目 lat/long 之间的距离(在 DB 中)。之后,我想回应距离小于 500 米的那些。到目前为止,我可以使用 foreach 来做到这一点。
<?php
mysql_connect("localhost", "beepbee_kunwarh", "kunwar") or die('MySQL Error.');
mysql_select_db("beepbee_demotest") or die('MySQL Error.');
$Lat = $_REQUEST['Lat'];
$long = $_REQUEST['long'];
$query = mysql_query("SELECT a.*, 3956 * 2 * ASIN(SQRT( POWER(SIN(($Lat - Lat) * pi()/180 / 2), 2) + COS($Lat * pi()/180) * COS(Lat * pi()/180) *POWER(SIN(($long - long) * pi()/180 / 2), 2) )) as distance FROM userResponse GROUP BY beepid HAVING distance <= 500 ORDER by distance ASC;");
$data = array();
while ($row = mysql_fetch_array($query)) {
$data[] = $row;
}
echo json_encode($data);
?>
【问题讨论】:
-
你的代码有什么问题?
-
它为所有条目计算相同的距离,基本上我不知道 wat 值正在传入 $lon2 = $data[$to]["long"]; $lat2 = $data[$to]["Lat"];
-
您完全更改了问题中的代码...它现在是在 SQL 中计算,而不是在 PHP 中。你能解释一下为什么/现在有什么问题吗?
-
它给了我在线错误 while ($row = mysql_fetch_array($query))