当前位置:Gxlcms > PHP教程 > php附近的人

php附近的人

时间:2021-07-01 10:21:17 帮助过:31人阅读

根据经纬度查找附近的人




  1. $uid=$_POST[uid];
  2. if($uid>0)
  3. {
  4. $re=mysql_query("select `lat`,`long` from pre_common_member where uid='$uid'")or die(mysql_error());
  5. $row=mysql_fetch_assoc($re);
  6. if(!empty($row[lat]) and !empty($row[long]))
  7. {
  8. $re0=mysql_query("select `username`,`lat`,`long` from pre_common_member where uid!='$uid'");
  9. while($row0=mysql_fetch_assoc($re0))
  10. {
  11. $distance = getDistanceBetweenPointsNew($row['lat'], $row['long'], $row0['lat'], $row0['long']);
  12. $row0[meter]=$distance[meters];
  13. $arr[]=$row0;
  14. }
  15. $arr=array_sort($arr,'meter');
  16. $arr = array_slice($arr,0,10);
  17. $data=json_encode($arr);
  18. $s=mysql_errno();
  19. //echo $data;
  20. echo "{\"s\":$s,\"data\":$data}";
  21. }
  22. }
  23. // 比较两个地点的距离
  24. function getDistanceBetweenPointsNew($latitude1, $longitude1, $latitude2, $longitude2)
  25. {
  26. $theta = $longitude1 - $longitude2;
  27. $miles = (sin(deg2rad($latitude1)) * sin(deg2rad($latitude2))) + (cos(deg2rad($latitude1)) * cos(deg2rad($latitude2)) * cos(deg2rad($theta)));
  28. $miles = acos($miles);
  29. $miles = rad2deg($miles);
  30. $miles = $miles * 60 * 1.1515;
  31. $feet = $miles * 5280;
  32. $yards = $feet / 3;
  33. $kilometers = $miles * 1.609344;
  34. $meters = $kilometers * 1000;
  35. return compact('miles', 'feet', 'yards', 'kilometers', 'meters');
  36. }
  37. // 二维数组按某个key排序
  38. function array_sort($arr,$keys,$type='asc')
  39. {
  40. $keysvalue = $new_array = array();
  41. foreach ($arr as $k=>$v){
  42. $keysvalue[$k] = $v[$keys];
  43. }
  44. if($type == 'asc'){
  45. asort($keysvalue);
  46. }else{
  47. arsort($keysvalue);
  48. }
  49. reset($keysvalue);
  50. foreach ($keysvalue as $k=>$v){
  51. $new_array[$k] = $arr[$k];
  52. }
  53. return $new_array;
  54. }

人气教程排行