说明:用于打卡签到功能;
用户到达某一地点后,根据用户所在位置经纬度位置和目的地所在位置经纬度进行计算对比,对比值大于设置距离差,不能打卡,小于设置距离差,可以打开。

1、pom.xml
 <!--用于计算两点之间的距离-->
        <dependency>
            <groupId>org.gavaghan</groupId>
            <artifactId>geodesy</artifactId>
            <version>1.1.3</version>
        </dependency>
2、实体类
//由前端传递 用户所在位置经纬度
@Data
public class PlatPunch {
    /*经度*/
    private String longitude;
    /*纬度*/
    private String latitude;
}
3、实现方法
/*  
    活动定位打卡功能 */
    @GetMapping(value = "/getPunch")
    public AjaxResult getPunch(PlatPunch platPunch){
        //1、设置目的地经度
        String longitudeS =106.235321//设置目的纬度
        String latitudeS =37.432579;
        // 2、由前端传过来的用户所在位置 经纬度
        String longitudeT = platPunch.getLongitude();
        String latitudeT = platPunch.getLatitude();
        // 3、对比
        GlobalCoordinates source = new GlobalCoordinates(Double.parseDouble(latitudeS),Double.parseDouble(longitudeS));
        GlobalCoordinates target = new GlobalCoordinates(Double.parseDouble(latitudeT),Double.parseDouble(longitudeT));
        //这是两种坐标系计算方法,这是第一种坐标系计算方法(我们用的这种)
        double geoCurve = getDistanceMeter(source, target, Ellipsoid.Sphere);
        //这是两种坐标系计算方法,这是第二种坐标系计算方法
        double geoCurve2 = getDistanceMeter(source, target, Ellipsoid.WGS84);
        System.out.println(geoCurve+"----------"+geoCurve2);
        //如果用户和目的地位置想吃2千米,在不能打卡;
        if(geoCurve > 2000){
            System.out.println("不能打卡!!!");
            //反之,可以打卡
        }else {
            System.out.println("能打卡");
        }
        return AjaxResult.success();
    }

/* 经纬度计算工具类*/
    public static double getDistanceMeter(GlobalCoordinates gpsFrom, GlobalCoordinates gpsTo, Ellipsoid ellipsoid)
    {
        //创建GeodeticCalculator,调用计算方法,传入坐标系、经纬度用于计算距离
        GeodeticCurve geoCurve = new GeodeticCalculator().calculateGeodeticCurve(ellipsoid, gpsFrom, gpsTo);
        return geoCurve.getEllipsoidalDistance();
    }
Logo

为开发者提供学习成长、分享交流、生态实践、资源工具等服务,帮助开发者快速成长。

更多推荐