Java实现电子围栏的小例子

news2024/10/6 18:35:31

主要需求是实现一个电子围栏判断的小例子其中包括前端和后端的demo代码

public class GeoFenceUtils {

    /** geometryFactory */
    private static final GeometryFactory geometryFactory = new GeometryFactory();

    /**
     * 判断指定的GPS点是否在电子围栏内
     *
     * @param fencePointsList 包含电子围栏的经纬度数据的列表,格式为 "经度,纬度"
     * @param pointStr        指定的GPS点,格式为 "经度,纬度"
     * @return 如果在电子围栏内则返回true,否则返回false
     */
    public static boolean isPointInGeoFence(List<String> fencePointsList, String pointStr) {
        // 将电子围栏的经纬度数据转换为坐标数组
        Coordinate[] fencePoints = parseCoordinates(fencePointsList);

        // 将指定的GPS点转换为坐标
        Coordinate targetPoint = parseCoordinate(pointStr);

        // 创建电子围栏多边形
        Polygon geoFencePolygon = createPolygon(fencePoints);

        // 创建指定的GPS点
        Point point = geometryFactory.createPoint(targetPoint);

        // 检查指定的GPS点是否在电子围栏内
        return geoFencePolygon.contains(point);
    }

    /**
     * 判断指定的GPS点是否在电子围栏内
     *
     * @param geoFencePolygon geo fence polygon
     * @param pointStr        指定的GPS点,格式为 "经度,纬度"
     * @return 如果在电子围栏内则返回true ,否则返回false
     * @since 2.0.0
     */
    public static boolean isPointInGeoFence(Polygon geoFencePolygon, String pointStr) {
        // 将指定的GPS点转换为坐标
        Coordinate testPoint = parseCoordinate(pointStr);

        // 创建指定的GPS点
        Point point = geometryFactory.createPoint(testPoint);

        // 检查指定的GPS点是否在电子围栏内
        return geoFencePolygon.contains(point);
    }

    /**
     * 判断指定的GPS点是否在电子围栏内
     *
     * @param fencePointsList 包含电子围栏的经纬度数据的列表,格式为 "经度,纬度"
     * @return 如果在电子围栏内则返回true,否则返回false
     */
    public static Polygon getPointInGeoFence(List<String> fencePointsList) {
        // 将电子围栏的经纬度数据转换为坐标数组
        Coordinate[] fencePoints = parseCoordinates(fencePointsList);
        // 创建电子围栏
        return createPolygon(fencePoints);
    }


    /**
     * 根据GPS点集合创建多边形
     *
     * @param coordinates GPS点集合
     * @return 多边形对象
     */
    private static Polygon createPolygon(Coordinate[] coordinates) {
        // Ensure the polygon is closed by adding the first coordinate at the end if necessary
        if (!coordinates[0].equals(coordinates[coordinates.length - 1])) {
            Coordinate[] closedCoordinates = new Coordinate[coordinates.length + 1];
            System.arraycopy(coordinates, 0, closedCoordinates, 0, coordinates.length);
            closedCoordinates[closedCoordinates.length - 1] = coordinates[0];
            coordinates = closedCoordinates;
        }
        return geometryFactory.createPolygon(coordinates);
    }

    /**
     * 将包含经纬度数据的列表转换为坐标数组
     *
     * @param pointsList 包含经纬度数据的列表
     * @return 坐标数组
     */
    private static Coordinate[] parseCoordinates(List<String> pointsList) {
        Coordinate[] coordinates = new Coordinate[pointsList.size()];
        for (int i = 0; i < pointsList.size(); i++) {
            coordinates[i] = parseCoordinate(pointsList.get(i));
        }
        return coordinates;
    }

    /**
     * 将经纬度数据字符串转换为坐标
     *
     * @param pointStr 经纬度数据字符串,格式为 "经度,纬度"
     * @return 坐标
     */
    private static Coordinate parseCoordinate(String pointStr) {
        String[] parts = pointStr.split(StringPool.COMMA);
        double lon = Double.parseDouble(parts[0]);
        double lat = Double.parseDouble(parts[1]);
        return new Coordinate(lon, lat);
    }

}

单元测试类

public class GeoFenceUtilTest {


    /**
     * Test geo
     *
     * @since 2.0.0
     */
    @Test
    public void testGeo() {
        // 示例电子围栏点集合
        List<String> fencePointsList = List.of(
                "116.403322,39.920255",
                "116.429043,39.913906",
                "116.438447,39.882655",
                "116.419252,39.873514",
                "116.406656,39.881143",
                "116.389347,39.884167",
                "116.389846,39.891365",
                "116.377436,39.908228",
                "116.410512,39.901984"
        );

        // 指定的GPS点
        String testPointStr = "116.384112,39.899172";
        // 检查指定的GPS点是否在电子围栏内
        boolean isWithinFence = GeoFenceUtils.isPointInGeoFence(fencePointsList, testPointStr);
        System.out.println("指定的GPS点是否在电子围栏内: " + isWithinFence);
    }

}

前端的测试demo,高德开放平台JS API测试平台,找到多边形编辑器,将下面的代码粘贴到执行器里面,然后点击右上角的执行代码,就可以获取到对应范围的点位地址

在这里插入图片描述

<!doctype html>
<html>
<head>
    <meta charset="utf-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="initial-scale=1.0, user-scalable=no, width=device-width">
    <style>
    html,
    body,
    #container {
      width: 100%;
      height: 100%;
    }
    </style>
    <title>多边形的绘制和编辑</title>
    <link rel="stylesheet" href="https://a.amap.com/jsapi_demos/static/demo-center/css/demo-center.css" />
    <script src="https://webapi.amap.com/maps?v=2.0&key=您申请的key值&plugin=AMap.PolygonEditor"></script>
    <script src="https://a.amap.com/jsapi_demos/static/demo-center/js/demoutils.js"></script>
</head>
<body>
<div id="container"></div>
<div class="input-card" style="width: 120px">
   <button class="btn" onclick="polyEditor.open()" style="margin-bottom: 5px">开始编辑</button> 
   <button class="btn" onclick="polyEditor.close()">结束编辑</button> 
</div>
<script type="text/javascript">
    var map = new AMap.Map("container", {
      center: [116.400274, 39.905812],
      zoom: 14,
      viewMode: '3D',
    });

    var path = [
      [116.403322, 39.920255],
      [116.410703, 39.897555],
      [116.402292, 39.892353],
      [116.389846, 39.891365]
    ]
    var path1 = [
      [116.453322, 39.920255],
      [116.460703, 39.897555],
      [116.452292, 39.892353],
      [116.439846, 39.891365]
    ]

    var polygon = new AMap.Polygon({
      path: path,
      strokeColor: "#FF33FF",
      strokeWeight: 6,
      strokeOpacity: 0.2,
      fillOpacity: 0.4,
      fillColor: '#1791fc',
      zIndex: 50,
      bubble: true,
    })
    var polygon1 = new AMap.Polygon({
      path: path1,
      strokeColor: "green",
      strokeWeight: 6,
      strokeOpacity: 0.2,
      fillOpacity: 0.4,
      fillColor: '#1791fc',
      zIndex: 50,
      bubble: true,

    })
    map.add([polygon, polygon1])

    // 缩放地图到合适的视野级别
    map.setFitView()
    var polyEditor;
      // var polyEditor = new AMap.PolyEditor(map, polygon)
      // polyEditor = new AMap.PolygonEditor(map)
    polyEditor = new AMap.PolygonEditor(map, polygon);
    polyEditor.addAdsorbPolygons(polygon1)
    polyEditor.open();

   polyEditor.on("addnode", (event) => {
        let path = event.target.getPath();
       console.log('addnode');
  console.log(path);
     
      });

 

      polyEditor.on("adjust", (event) => {
        let path = event.target.getPath();
             console.log('adjust');
  console.log(path);
      });

      polyEditor.on("removenode", (event) => {
        let path = event.target.getPath();
          console.log('removenode');
  console.log(path);
      });

      polyEditor.on("end", (event) => {
        let path = event.target.getPath();
            console.log('end');
  console.log(path);
      });


     map.on('click',(e)=>{
       console.log('您在 [ '+e.lnglat.getLng()+','+e.lnglat.getLat()+']的位置点击了多边形!');
    });

  </script>
</body>
</html>

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.coloradmin.cn/o/1891400.html

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈,一经查实,立即删除!

相关文章

上海小程序开发需要进行定制开发吗?

随着互联网技术与移动设备的不断成熟&#xff0c;小程序也已普及到人们日常生活的方方面面。随着企业与互联网联结的愈发深入&#xff0c;小程序的开发可以为企业带来更高效的经营模式&#xff0c;降本增效。那么&#xff0c;上海小程序作为无需安装且开发门槛较低的应用&#…

udp发送数据如果超过1个mtu时,抓包所遇到的问题记录说明

最近在测试Syslog udp发送相关功能&#xff0c;测试环境是centos udp头部的数据长度是2个字节&#xff0c;最大传输长度理论上是65535&#xff0c;除去头部这些字节&#xff0c;可以大概的说是64k。 写了一个超过64k的数据(随便用了一个7w字节的buffer)发送demo&#xff0c;打…

Three.js机器人与星系动态场景(二):强化三维空间认识

在上篇博客中介绍了如何快速利用react搭建three.js平台&#xff0c;并实现3D模型的可视化。本文将在上一篇的基础上强化坐标系的概念。引入AxesHelper辅助工具&#xff0c;带你快速理解camer、坐标原点、可视区域。 Three.js机器人与星系动态场景&#xff1a;实现3D渲染与交互式…

AMEYA360代理:海凌科60G客流量统计雷达模块 4T4R出入口绊数计数

数字化时代&#xff0c;不管是大型商城还是各种连锁店&#xff0c;客流统计分析都可以帮助企业更加精准地了解顾客需求和消费行为。 海凌科推出一款专用于客流量统计的60G雷达模块&#xff0c;4T4R&#xff0c;可以实时进行固定范围内的人体运动轨迹检测&#xff0c;根据人体的…

使用Python3和Selenium打造百度图片爬虫

开篇 本文的目的在于实现一个用来爬取百度图片的爬虫程序,因该网站不需要登录&#xff0c;所以相对来说较为简单。下面的爬虫程序中我写了比较多的注释&#xff0c;以便于您的理解。 准备 请确保电脑上已经安装了与chrome浏览器版本匹配的chromeDriver&#xff0c;且电脑中已经…

使用 HBuilder X 进行 uniapp 小程序开发遇到的问题合集

文章目录 背景介绍问题集锦1. 在 HBuilderX 点击浏览器运行时&#xff0c;报 uni-app vue3编译器下载失败 安装错误2.在 HBuilderX 点击微信小程序运行时&#xff0c;报 微信开发者工具打开项目失败&#xff0c;请参阅启动日志错误 背景介绍 HBuilder X 版本&#xff1a;HBui…

NoSQL 之 Redis 集群部署

前言&#xff1a; &#xff08;1&#xff09;主从复制&#xff1a;主从复制是高可用Redis的基础&#xff0c;哨兵和集群都是在主从复制基础上实现高可用 的。主从复制主要实现了数据的多机备份&#xff0c;以及对于读操作的负载均衡和简单的故障恢复。缺陷&#xff1a; 故障…

Elasticsearch备份数据到本地,并导入到新的服务 es 服务中

文章目录 使用elasticsearch-dump工具备份安装node.js(二进制安装)解压设置环境变量安装elasticsearch-dump docker安装使用ES备份文件到本地 使用elasticsearch-dump工具备份 这个工具备份时间比较长 安装node.js(二进制安装) wget https://nodejs.org/dist/v16.18.0/node-…

E1696 无法打开 源 文件 “point.h“

一段时间没碰vs2022突然导入一个项目就出现下面错误 在网上查了很多办法&#xff0c;都没什么有用。 试了试&#xff0c;相对路径可以解决。 但是每次都要用相对路径太麻烦了。 又试了试&#xff0c;发现还是硬件问题&#xff0c;就像摩托长期不开等到突然想开的时候就死活打…

零障碍入门:SSH免密登录与Hadoop生态系统的完美搭档【实训Day02】

一、 SSH免密登录配置 1 生成公钥和秘钥(在hadoop101上) # su star # cd /home/star/.ssh # ssh-keygen -t rsa 2 公钥和私钥 公钥id_rsa.pub 私钥id_rsa 3 将公钥拷贝到目标机器上(在hadoop101上) # ssh-copy-id hadoop101 # ssh-copy-id hadoop102 # ssh-co…

Hi3861 OpenHarmony嵌入式应用入门--TCP Client

本篇使用的是lwip编写tcp客户端。需要提前准备好一个PARAM_HOTSPOT_SSID宏定义的热点&#xff0c;并且密码为PARAM_HOTSPOT_PSK。还需要准备一个tcp服务&#xff0c;服务ip为PARAM_SERVER_ADDR宏定义&#xff0c;端口为PARAM_SERVER_PORT宏定义。 修改网络参数 在Hi3861开发板…

[C++][设计模式][访问器]详细讲解

目录 1.动机2.模式定义3.要点总结4.代码感受1.代码一2.代码二 1.动机 在软件构件过程中&#xff0c;由于需求的变化&#xff0c;某些类层次结构中常常需要增加新的行为(方法)&#xff0c;如果直接在基类中做这样的更改&#xff0c; 将会给子类带来很繁重的变更负担&#xff0c…

zabbix小白入门:从SNMP配置到图形展示——以IBM服务器为例

作者 乐维社区&#xff08;forum.lwops.cn&#xff09;许远 在运维实践中&#xff0c;Zabbix作为一款强大的开源监控工具&#xff0c;被广泛应用于服务器、网络设备和应用程序的监控&#xff0c;成为保障业务连续性和高效运行的关键。然而&#xff0c;对于Zabbix的初学者来说&a…

法国工程师IMT联盟 密码学及其应用 2023年期末考试题

1 在 Unix 下的安全性 (30 分钟) 1.1 问题 1 1.1.1 问题 我们注意constat到通过 SMTP 服务器发送“假”电子邮件&#xff08;垃圾邮件&#xff09;相对容易。越来越常见的做法是在 SMTP 连接之上部署dployer TLS 协议protocole&#xff08;即 SMTPS&#xff09;。这解决了垃圾…

【IDEA配置一个maven项目(详细操作流程)】

目录 一、安装Maven 1、官网下载maven链接地址&#xff1a;Maven – Download Apache Maven 2、下载完成后&#xff0c;解压到某一路径下。E:\JavaTools\apache-maven-3.9.8为例&#xff0c;实际配置环境变量时以自己安装的路径为准。 二、配置环境变量 1、右键此电脑–&g…

MybatisPlus实现AES加密解密,实现yml配置文件中数据库连接信息如用户名,密码等信息加密解密

1 生成秘钥&#xff0c;使用AES工具生成一个随机秘钥&#xff0c;然后对用户名&#xff0c;密码加密 //数据库用户名和密码加密工具测试类 public class MpDemoApplicationTests {Testvoid contextLoads() {// 数据库用户名和密码String dbUsername"改成你的数据库连接用…

LabVIEW汽车转向器测试系统

绍了一种基于LabVIEW的汽车转向器测试系统。该系统集成了数据采集、控制和分析功能&#xff0c;能够对转向器进行高效、准确的测试。通过LabVIEW平台&#xff0c;实现了对转向器性能参数的实时监测和分析&#xff0c;提升了测试效率和数据精度&#xff0c;为汽车转向器的研发和…

Ubuntu查看opencv版本c++

✗命令行中直接输入&#xff1a; pkg-config --modversion opencv✔命令行中直接输入&#xff1a; pkg-config --modversion opencv4注解&#xff1a;附上在markdown中打勾&#xff0c;对号和打叉。使用时将&和#之间的空格去掉&#xff0c;这里只是为了不让CSDN自动转换才…

UE5 04-重新加载当前场景

给关卡加一个淡出的效果 给关卡加一个淡入的效果, 这个最好放置在Player 上,这样切关卡依然有这个效果

金斗云 HKMP智慧商业软件 任意用户创建漏洞复现

0x01 产品简介 金斗云智慧商业软件是一款功能强大、易于使用的智慧管理系统,通过智能化的管理工具,帮助企业实现高效经营、优化流程、降低成本,并提升客户体验。无论是珠宝门店、4S店还是其他零售、服务行业,金斗云都能提供量身定制的解决方案,助力企业实现数字化转型和智…