简单的电子围栏工具类

这个工具类展示了如何利用经纬度计算两点间的距离,并判断是否在预设精度范围内,适用于电子围栏的实现。通过将角度转换为弧度,然后应用地理距离计算公式,可以确定坐标是否在设定的误差范围内。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

说明

该工具类为简单的电子围栏计算, 通过两点之间的经纬度,及范围值, 计算出当前坐标是否在电子围栏范围内,有问题的可以留言,大家一起沟通改进

电子围栏工具类

import java.math.BigDecimal;

/**
 * @desc 电子围栏计算
 * @author huyang
 */
public class GISUtils {

    /**
     * 把经纬度转为度(°)
     * 角度转换成弧度
     */
    private static double rad(double d){
        return d * Math.PI / 180.0;
    }

     /**
     * 计算两点之间的距离
     * @param visitorLatitude
     * @param visitorLongitude
     * @param fixedPlaceLatitude
     * @param fixedPlaceLongitude
     * @return 两点之间的距离,单位米
     */
    public static double getDistance(double visitorLatitude, double visitorLongitude, double fixedPlaceLatitude, double fixedPlaceLongitude) {
        // 纬度
        double Lat1 = rad(visitorLatitude);
        double Lat2 = rad(fixedPlaceLatitude);
        //两点纬度之差
        double a = Lat1 - Lat2;
        //经度之差
        double b = rad(visitorLongitude) - rad(fixedPlaceLongitude);
        //计算两点距离的公式
        double s = 2 * Math.asin(Math
                .sqrt(Math.pow(Math.sin(a / 2), 2) + Math.cos(Lat1) * Math.cos(Lat2) * Math.pow(Math.sin(b / 2), 2)));
        //弧长乘地球半径(半径为米)
        s = s * 6378137.0;
        //精确距离的数值
        s = Math.round(s * 10000d) / 10000d;
        return s;
    }

    /**
     * 判断两点距离是否在范围内
     * @param currentLatitude 当前坐标纬度
     * @param currentLongitude 当前坐标经度
     * @param fixedPlaceLatitude 目标坐标纬度
     * @param fixedPlaceLongitude 目标坐标经度
     * @param precision 误差范围, 单位米
     * @return
     */
    public static boolean checkDistanceInPrecision(Double currentLatitude, Double currentLongitude, Double fixedPlaceLatitude, Double fixedPlaceLongitude, double precision){
        double distance = getDistance(currentLatitude, currentLongitude, fixedPlaceLatitude, fixedPlaceLongitude);
        return distance <= precision;
    }
    
    /**
     * 判断两点距离是否在范围内
     * @param currentLatitude 当前坐标纬度
     * @param currentLongitude 当前坐标经度
     * @param fixedPlaceLatitude 目标坐标纬度
     * @param fixedPlaceLongitude 目标坐标经度
     * @param precision 误差范围, 单位米
     * @return
     */
    public static boolean checkDistanceInPrecision(BigDecimal currentLatitude, BigDecimal visitorLongitude, BigDecimal fixedPlaceLatitude, BigDecimal fixedPlaceLongitude,
                                                   double precision){
        return checkDistanceInPrecision(currentLatitude.doubleValue(), visitorLongitude.doubleValue(),
                fixedPlaceLatitude.doubleValue(), fixedPlaceLongitude.doubleValue(), precision);
    }
}

调用示例

boolean passCheck = GISUtils.checkDistanceInPrecision(new BigDecimal("116.397128"),
new BigDecimal("39.916527"), new BigDecimal("116.397128"),
new BigDecimal("39.916527"), 100);
if (passCheck) {
	System.out.println("在外围内");
} else {
	System.out.println("在外围外");
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值