- /* 计算两组经纬度坐标之间的距离
- * @param $lat1 纬度1
- * @param $lng1 经度1
- * @param $lat2 纬度2
- * @param $lng2 经度2
- * @param int $len_type 返回值类型(1-m 2-km)
- * @param int $decimal 保留小数位数
- * @return float
- */
- public function getDistance($lat1, $lng1, $lat2, $lng2, $len_type = 1, $decimal = 2)
- {
- $radLat1 = $lat1 * 3.1415926 / 180.0;
- $radLat2 = $lat2 * 3.1415926 / 180.0;
- $a = $radLat1 - $radLat2;
- $b = ($lng1 * 3.1415926 / 180.0) - ($lng2 * 3.1415926 / 180.0);
- $s = 2 * asin(sqrt(pow(sin($a / 2), 2) + cos($radLat1) * cos($radLat2) * pow(sin($b / 2), 2)));
- $s = $s * 6378.137;
- $s = round($s * 1000);
- if ($len_type > 1) {
- $s /= 1000;
- }
- return round($s, $decimal);
- }
复制代码- /**
- * 将角度换算为弧度
- * @param d 角度
- * @return 弧度
- */
- private static double rad(double d) {
- return d * Math.PI / 180.0;
- }
-
- /**
- * 先通过经纬度获取距离(单位:米),再判断一个点是否在圆形区域内(根据所给的半径坐比较)
- * @param n1=>app
- * @param n2=>仓库
- * @param radius
- * @return
- */
- public static boolean isInCircle(ZJPoint n1 ,ZJPoint n2,String radius){
- final double EARTH_RADIUS = 6378.137;////地球半径 (千米)
- double radLat1 = rad(n1.getX()!=null ? n1.getX().doubleValue():0);
- double radLat2 = rad(n2.getX()!=null ? n2.getX().doubleValue():0);
- double radLon1 = rad(n1.getY()!=null ? n1.getY().doubleValue():0);
- double radLon2 = rad(n2.getY()!=null ? n2.getY().doubleValue():0);
- //两点之间的差值
- double jdDistance = radLat1 - radLat2;
- double wdDistance = radLon1 - radLon2;
- double distance = 2 * Math.asin(Math.sqrt(Math.pow(Math.sin(jdDistance / 2), 2) +
- Math.cos(radLat1) * Math.cos(radLat2) * Math.pow(Math.sin(wdDistance / 2), 2)));
- distance = distance * EARTH_RADIUS;
- distance = Math.round(distance * 10000d) / 10000d;
- distance = distance*1000;//将计算出来的距离千米转为米
- double r = Double.parseDouble(radius);
- //判断一个点是否在圆形区域内
- if (distance > r) {
- return false;
- }
- return true;
- }
复制代码
|