高德地图变相实现简单的地理围栏技术

代码地址:

原理

开发 -> Web服务 API -> 开发指南 -> API文档 -> 地理围栏

使用规则

配置好数据库

global:
  datasource:
    service:
      type: com.alibaba.druid.pool.DruidDataSource
      driver-class-name: com.mysql.cj.jdbc.Driver
      url: jdbc:mysql://90service:2019/geo-fencing?useUnicode=true&characterEncoding=UTF-8&useJDBCCompliantTimezoneShift=true&useLegacyDatetimeCode=false&serverTimezone=Asia/Shanghai
      username: root
      password: 123456 
  geo-fencing-backend:
    server-name: geo-fencing-backend
    port: 8081

配置前端地址:

const url = 'http://192.168.30.95:8081'; 
export default {url}

启动前端:

使用命令 

npm start

浏览器打开地址 http://localhost:3333/#/ 可以查看界面

启动后端

配置好数据库后,启动系统

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

@SpringBootApplication
public class GeoFencingBackendApplication {

    public static void main(String[] args) {
        SpringApplication.run(GeoFencingBackendApplication.class, args);
    }

}

使用

打开界面后,首先点击编辑围栏

开始画区域,双击鼠标完成画图,点击结束编辑围栏后,数据后传输到后台

最后,拖动坐标,可以查看坐标是不是在多边形里面。

核心算法


import java.awt.geom.Point2D;
import java.util.List;

public class InOrOutPolygonUtils {

    /**
     * 判断点是否在多边形内
     * @param point 检测点
     * @param pts   多边形的顶点
     * @return      点在多边形内返回true,否则返回false
     */
    public static boolean IsPtInPoly(Point2D.Double point, List<Point2D.Double> pts){

        int N = pts.size();
        boolean boundOrVertex = true; //如果点位于多边形的顶点或边上,也算做点在多边形内,直接返回true
        int intersectCount = 0;//cross points count of x
        double precision = 2e-10; //浮点类型计算时候与0比较时候的容差
        Point2D.Double p1, p2;//neighbour bound vertices
        Point2D.Double p = point; //当前点

        p1 = pts.get(0);//left vertex
        for(int i = 1; i <= N; ++i){//check all rays
            if(p.equals(p1)){
                return boundOrVertex;//p is an vertex
            }

            p2 = pts.get(i % N);//right vertex
            if(p.x < Math.min(p1.x, p2.x) || p.x > Math.max(p1.x, p2.x)){//ray is outside of our interests
                p1 = p2;
                continue;//next ray left point
            }

            if(p.x > Math.min(p1.x, p2.x) && p.x < Math.max(p1.x, p2.x)){//ray is crossing over by the algorithm (common part of)
                if(p.y <= Math.max(p1.y, p2.y)){//x is before of ray
                    if(p1.x == p2.x && p.y >= Math.min(p1.y, p2.y)){//overlies on a horizontal ray
                        return boundOrVertex;
                    }

                    if(p1.y == p2.y){//ray is vertical
                        if(p1.y == p.y){//overlies on a vertical ray
                            return boundOrVertex;
                        }else{//before ray
                            ++intersectCount;
                        }
                    }else{//cross point on the left side
                        double xinters = (NumberUtils.sub(p.x , p1.x)) * (NumberUtils.sub(p2.y , p1.y)) / (NumberUtils.sub(p2.x , p1.x)) + p1.y;//cross point of y
                        if(Math.abs(p.y - xinters) < precision){//overlies on a ray
                            return boundOrVertex;
                        }

                        if(p.y < xinters){//before ray
                            ++intersectCount;
                        }
                    }
                }
            }else{//special case when ray is crossing through the vertex
                if(p.x == p2.x && p.y <= p2.y){//p crossing over p2
                    Point2D.Double p3 = pts.get((i+1) % N); //next vertex
                    if(p.x >= Math.min(p1.x, p3.x) && p.x <= Math.max(p1.x, p3.x)){//p.x lies between p1.x & p3.x
                        ++intersectCount;
                    }else{
                        intersectCount += 2;
                    }
                }
            }
            p1 = p2;//next ray left point
        }

        if(intersectCount % 2 == 0){//偶数在多边形外
            return false;
        } else { //奇数在多边形内
            return true;
        }

    }
}

  • 1
    点赞
  • 21
    收藏
    觉得还不错? 一键收藏
  • 6
    评论
好的,我了解了您的问题。实现高德地图地理围栏设置功能需要使用高德地图的API和Java编程语言。下面是一个简单的Java工具类,可以帮助您实现电子围栏的设置: ```java import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; import java.net.HttpURLConnection; import java.net.URL; import java.net.URLEncoder; public class GeoFenceTool { private static final String KEY = "您的高德地图API密钥"; // 高德地图API密钥 /** * 创建电子围栏 * * @param name 电子围栏名称 * @param points 电子围栏坐标点(多边形) * @return 返回电子围栏ID */ public static String createGeoFence(String name, String points) { String url = "https://restapi.amap.com/v4/geofence/meta?key=" + KEY; String param = "{\"name\":\"" + name + "\",\"points\":\"" + points + "\"}"; String result = sendPost(url, param); String geofenceId = result.substring(result.indexOf("gid\":\"") + 6, result.indexOf("\",\"name\"")); return geofenceId; } /** * 删除电子围栏 * * @param geofenceId 电子围栏ID * @return 返回删除结果 */ public static String deleteGeoFence(String geofenceId) { String url = "https://restapi.amap.com/v4/geofence/meta/" + geofenceId + "?key=" + KEY; String result = sendDelete(url); return result; } /** * 发送POST请求 * * @param url 请求地址 * @param param 请求参数 * @return 返回请求结果 */ private static String sendPost(String url, String param) { StringBuilder result = new StringBuilder(); BufferedReader in = null; HttpURLConnection conn = null; try { URL realUrl = new URL(url); conn = (HttpURLConnection) realUrl.openConnection(); conn.setRequestMethod("POST"); conn.setRequestProperty("Content-Type", "application/json"); conn.setDoOutput(true); conn.getOutputStream().write(param.getBytes()); in = new BufferedReader(new InputStreamReader(conn.getInputStream())); String line; while ((line = in.readLine()) != null) { result.append(line); } } catch (Exception e) { e.printStackTrace(); } finally { try { if (in != null) { in.close(); } } catch (IOException e) { e.printStackTrace(); } if (conn != null) { conn.disconnect(); } } return result.toString(); } /** * 发送DELETE请求 * * @param url 请求地址 * @return 返回请求结果 */ private static String sendDelete(String url) { StringBuilder result = new StringBuilder(); BufferedReader in = null; HttpURLConnection conn = null; try { URL realUrl = new URL(url); conn = (HttpURLConnection) realUrl.openConnection(); conn.setRequestMethod("DELETE"); in = new BufferedReader(new InputStreamReader(conn.getInputStream())); String line; while ((line = in.readLine()) != null) { result.append(line); } } catch (Exception e) { e.printStackTrace(); } finally { try { if (in != null) { in.close(); } } catch (IOException e) { e.printStackTrace(); } if (conn != null) { conn.disconnect(); } } return result.toString(); } } ``` 使用方法: 1. 在高德地图开放平台上申请API密钥,并将其替换到KEY变量中。 2. 调用createGeoFence方法创建电子围栏,传入电子围栏名称和坐标点参数。坐标点格式为:经度,纬度;经度,纬度;经度,纬度...(多边形)。 3. 如果需要删除电子围栏,调用deleteGeoFence方法并传入电子围栏ID参数。 注意:本工具类仅供参考,具体实现需要根据项目需求进行调整。同时,使用高德地图API时请遵守《高德地图开放平台服务协议》等相关法律法规。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 6
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值