欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  后端开发

php 附近的人

程序员文章站 2022-04-19 13:16:23
...
根据经纬度查找附近的人




  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. }
复制代码
相关标签: php 附近的人