嗨,在我的数据库中,我总共有100个位置,这50个位置最接近我,另外50个位置对我来说最长,所以我想使用此顺序对数据进行排序,因此我在这里计算距离
$latt=$_REQUEST['latt'];
$long=$_REQUEST['long'];
$start=$_REQUEST['start'];
function distancefind($lat1, $lon1, $lat2, $lon2, $unit) {
$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);
if ($unit == "K") {
return ($miles * 1.609344);
} else if ($unit == "N") {
return ($miles * 0.8684);
} else {
return $miles;
}
}
$c=0;
$limt=10;
if($start!=null)
{
$query="select * from tbl_MapDetails LIMIT $start,$limt";
}
$result=mysql_query($query);
if($result)
{
while($row=mysql_fetch_array($result))
{
$distance1=distancefind($latt,$long,$row['Latitude'],$row['Longitude'],"");
$message[$c]=array("ID"=>$row['LocationID'],"Address"=>$address,"City"=>$row['City']=($row['City'] != null)?$row['City']:"","State"=>$row['State']=($row['State'] !=null)?$row['State']:"","Country"=>$row['Country']=($row['Country']!=null)?$row['Country']:"","Zip"=>$row['Zip']=($row['Zip'] !=null)?$row['Zip']:"","Country"=>$row['Country']=($row['Country']!=null)?$row['Country']:"","Distance"=>$distance1=($distance1==null)?"0":$distance1,"Latitude"=>$row['Latitude']=($row['Latitude']!=null)?$row['Latitude']:"","Longitude"=>$row['Longitude']=($row['Longitude']!=null)?$row['Longitude']:"","Pic"=>$pic,"rating"=>$row1[0]=($row1[0]!=null)?$row1[0]:"","name"=>$row['LocationName']=($row['LocationName']!=null)?$row['LocationName']:"","note"=>$row['Note']=($row['Note']!=null)?$row['Note']:"","feature1"=>$row['FeatureIcon1']=($row['FeatureIcon1']!=null)?$row['FeatureIcon1']:"","feature2"=>$row['FeatureIcon2']=($row['FeatureIcon2']!=null)?$row['FeatureIcon2']:"","feature3"=>$row['FeatureIcon3']=($row['FeatureIcon3']!=null)?$row['FeatureIcon3']:"","selectLogo"=>$row['SelectIcon']=($row['SelectIcon']!=null)?$row['SelectIcon']:"");
$c++;
}
所以在这里我每次获得10个位置时都会得到消息,因此消息数组数据从最近到最长开始如何检查请指导我
谢谢前进
最佳答案
如果您想先进行排序,然后再进行限制(即选择$limt
最接近的东西(由$start
偏移)到($lat,$long)
),则可以在MySQL中进行:
$query = "SELECT *,
((ACOS( SIN($lat*PI()/180)*SIN(Latitude*PI()/180)
+ COS($lat*PI()/180)*COS(Latitude*PI()/180)*COS(($long-Longitude)*PI()/180
)
) * 180/PI()
)*60 * 1.1515) AS dist
FROM tbl_MapDetails
ORDER BY dist
LIMIT $start,$limt";
但是,如果要先执行
$limt
然后按距离排序(因此请选择$limt
项目,然后按距离排序-尽管可能不包含最近的位置),请使用@SergeyRatnikov的答案,或者您需要执行嵌套选择为:SELECT * from
(SELECT *,
...[distancecalculation]... AS dist
FROM tbl_MapDetails
LIMIT $start,$limt)
ORDER BY dist
关于php - 使用Php获取最靠近最长的位置?,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/8798830/