php实现手机定位功能的实例

[复制链接]
查看1 | 回复0 | 2021-11-30 16:55:48 | 显示全部楼层 |阅读模式
  1. <html>

  2. <meta charset="utf8">

  3. <head>

  4. <form name="form1" action="http://test.nwee.cc/ken/baidulocated.php" method="post">

  5. <input type="text" name="lat" id="lat" style="display:none">

  6. <input type="text" name="lng" id="lng" style="display:none">

  7. </form>

  8. <script type="text/javascript">

  9.     var position_option = {

  10.         enableHighAccuracy: true,

  11.         maximumAge: Infinity,

  12.         timeout: 100000

  13.     };

  14.     var lat;

  15.     var lng;

  16.     if(navigator.geolocation){

  17.         navigator.geolocation.getCurrentPosition(getPositionSuccess, getPositionError, position_option);

  18.     }else{

  19.         alert('Geolocation is not supported by this browser.');

  20.     }

  21.     function getPositionSuccess(position) {

  22.         lat = position.coords.latitude;

  23.         lng = position.coords.longitude;

  24.         //alert('您所在的位置: 纬度' + lat + ',经度' + lng);

  25. fillForm();

  26. //填充表单

  27. document.form1.submit();

  28. //post数据到服务器

  29.     }

  30.     function getPositionError(error) {

  31.         switch (error.code) {

  32.             case error.TIMEOUT:

  33.             alert('The request to get user location timed out.');break;

  34.             case error.PERMISSION_DENIED:

  35.             alert('User denied the request for Geolocation.');break;

  36.             case error.POSITION_UNAVAILABLE:

  37.             alert('Location information is unavailable.');break;

  38.             default:

  39.             alert('An unknown error occurred.');

  40.         }

  41.     }

  42.     function fillForm(){

  43.         document.getElementById('lat').value = lat;

  44.         document.getElementById('lng').value = lng;

  45.     }

  46. </script>

  47. </head>

  48. </html>

  49. located.php文件

  50. 通过百度定位api进行解析定位

  51. <?php

  52. header("Content-type:text/html; charset=utf8");

  53. $url="http://api.map.baidu.com/geocoder/v2/?location=".$_POST["lat"].",".$_POST["lng"]."&ak=*****************************&coordtype=bd09ll&output=json";

  54. $json = file_get_contents($url);

  55. $arr = json_decode($json, true);

  56. var_dump($arr);

  57. ?>
复制代码
  1. /* 计算两组经纬度坐标之间的距离
  2.   * @param $lat1 纬度1
  3.   * @param $lng1 经度1
  4.   * @param $lat2 纬度2
  5.   * @param $lng2 经度2
  6.   * @param int $len_type 返回值类型(1-m 2-km)
  7.   * @param int $decimal 保留小数位数
  8.   * @return float
  9.   */
  10.   public function getDistance($lat1, $lng1, $lat2, $lng2, $len_type = 1, $decimal = 2)
  11.   {
  12.     $radLat1 = $lat1 * 3.1415926 / 180.0;
  13.     $radLat2 = $lat2 * 3.1415926 / 180.0;
  14.     $a = $radLat1 - $radLat2;
  15.     $b = ($lng1 * 3.1415926 / 180.0) - ($lng2 * 3.1415926 / 180.0);
  16.     $s = 2 * asin(sqrt(pow(sin($a / 2), 2) + cos($radLat1) * cos($radLat2) * pow(sin($b / 2), 2)));
  17.     $s = $s * 6378.137;
  18.     $s = round($s * 1000);
  19.     if ($len_type > 1) {
  20.       $s /= 1000;
  21.     }
  22.     return round($s, $decimal);
  23.   }
复制代码
  1. /**
  2.   * 将角度换算为弧度
  3.   * @param d 角度
  4.   * @return 弧度
  5.   */
  6. private static double rad(double d) {
  7.      return d * Math.PI / 180.0;
  8. }

  9. /**
  10.   * 先通过经纬度获取距离(单位:米),再判断一个点是否在圆形区域内(根据所给的半径坐比较)
  11.   * @param n1=>app
  12.    * @param n2=>仓库
  13.   * @param radius
  14.   * @return
  15.   */
  16. public static boolean isInCircle(ZJPoint n1 ,ZJPoint n2,String radius){
  17.   final double EARTH_RADIUS = 6378.137;////地球半径 (千米)
  18.   double radLat1 = rad(n1.getX()!=null ? n1.getX().doubleValue():0);
  19.    double radLat2 = rad(n2.getX()!=null ? n2.getX().doubleValue():0);
  20.    double radLon1 = rad(n1.getY()!=null ? n1.getY().doubleValue():0);
  21.    double radLon2 = rad(n2.getY()!=null ? n2.getY().doubleValue():0);
  22.    //两点之间的差值
  23.    double jdDistance = radLat1 - radLat2;
  24.    double wdDistance = radLon1 - radLon2;
  25.    double distance = 2 * Math.asin(Math.sqrt(Math.pow(Math.sin(jdDistance / 2), 2) +
  26.          Math.cos(radLat1) * Math.cos(radLat2) * Math.pow(Math.sin(wdDistance / 2), 2)));
  27.    distance = distance * EARTH_RADIUS;
  28.    distance = Math.round(distance * 10000d) / 10000d;
  29.    distance = distance*1000;//将计算出来的距离千米转为米
  30.    double r = Double.parseDouble(radius);
  31.    //判断一个点是否在圆形区域内
  32.    if (distance > r) {
  33.        return false;
  34.     }
  35.   return true;
  36. }
复制代码


回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

11

主题

3

回帖

187

积分

管理员

Rank: 9Rank: 9Rank: 9

积分
187
热门排行