java 根据经纬度判断定位是否在电子围栏内

文章介绍了如何使用Java编程语言中的几何库,如Point2D,实现一个方法IsPointInPoly,用于判断给定点是否在由多个经纬度坐标定义的电子围栏多边形内。

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

java 根据经纬度判断定位是否在电子围栏内.

定位点要按顺时针或者逆时针顺序上传

import java.awt.geom.Point2D;
import java.util.ArrayList;
import java.util.List;
 
public class PolygonUtil {
    /**
     * 判断点是否在多边形内
     * @Title: IsPointInPoly
     * @Description: TODO()
     * @param point 测试点
     * @param pts 多边形的点
     * @return
     * @return boolean
     * @throws
     */
    public static boolean isInPolygon(Point2D.Double point, List<Point2D.Double> pts){
        int N = pts.size();
        boolean boundOrVertex = true;
        int intersectCount = 0;//交叉点数量
        double precision = 2e-10; //浮点类型计算时候与0比较时候的容差
        Point2D.Double p1, p2;//临近顶点
        Point2D.Double p = point; //当前点
 
        p1 = pts.get(0);
        for(int i = 1; i <= N; ++i){
            if(p.equals(p1)){
                return boundOrVertex;
            }
 
            p2 = pts.get(i % N);
            if(p.x < Math.min(p1.x, p2.x) || p.x > Math.max(p1.x, p2.x)){
                p1 = p2;
                continue;
            }
 
            //射线穿过算法
            if(p.x > Math.min(p1.x, p2.x) && p.x < Math.max(p1.x, p2.x)){
                if(p.y <= Math.max(p1.y, p2.y)){
                    if(p1.x == p2.x && p.y >= Math.min(p1.y, p2.y)){
                        return boundOrVertex;
                    }
 
                    if(p1.y == p2.y){
                        if(p1.y == p.y){
                            return boundOrVertex;
                        }else{
                            ++intersectCount;
                        }
                    }else{
                        double xinters = (p.x - p1.x) * (p2.y - p1.y) / (p2.x - p1.x) + p1.y;
                        if(Math.abs(p.y - xinters) < precision){
                            return boundOrVertex;
                        }
 
                        if(p.y < xinters){
                            ++intersectCount;
                        }
                    }
                }
            }else{
                if(p.x == p2.x && p.y <= p2.y){
                    Point2D.Double p3 = pts.get((i+1) % N);
                    if(p.x >= Math.min(p1.x, p3.x) && p.x <= Math.max(p1.x, p3.x)){
                        ++intersectCount;
                    }else{
                        intersectCount += 2;
                    }
                }
            }
            p1 = p2;
        }
        if(intersectCount % 2 == 0){//偶数在多边形外
            return false;
        } else { //奇数在多边形内
            return true;
        }
    }
    public static void main(String[] args) {
        Point2D.Double point = new Point2D.Double();
        point.setLocation(116.52759064648436,39.88327456483143);
        List<Point2D.Double> pts = new ArrayList<>();
        Point2D.Double point1 = new Point2D.Double();
        point1.setLocation(116.48965348217772,39.90816602515441);
        Point2D.Double point2 = new Point2D.Double();
        point2.setLocation(116.54338349316404,39.9097461301072);
        Point2D.Double point3 = new Point2D.Double();
        point3.setLocation(116.54836167309568,39.8699690729595);
        Point2D.Double point4 = new Point2D.Double();
        point4.setLocation(116.4889668366699,39.87181355267268);
        pts.add(point1);
        pts.add(point2);
        pts.add(point3);
        pts.add(point4);
        boolean inPolygon = isInPolygon(point,pts);
        System.out.println(inPolygon);
    }
 
}
电子围栏通常是指通过GPS技术实现对某个范围内的设备或者人员的位置进行监控和管理。在Java中可以使用GPS定位库和地图API来实现电子围栏的功能。 以下是一个基本的电子围栏Java代码示例: ```java import com.google.maps.GeoApiContext; import com.google.maps.GeocodingApi; import com.google.maps.model.GeocodingResult; import com.google.maps.model.LatLng; import com.google.maps.model.LatLngBounds; public class ElectronicFence { private LatLngBounds bounds; // 定义电子围栏的范围 public ElectronicFence(LatLng center, double radius) { double north = center.lat + radius; double south = center.lat - radius; double east = center.lng + radius; double west = center.lng - radius; bounds = new LatLngBounds(new LatLng(south, west), new LatLng(north, east)); } public boolean inFence(String address) { try { GeoApiContext context = new GeoApiContext.Builder() .apiKey("YOUR_API_KEY_HERE") .build(); GeocodingResult[] results = GeocodingApi.geocode(context, address).await(); LatLng location = results[0].geometry.location; return bounds.contains(location); } catch (Exception e) { e.printStackTrace(); return false; } } public static void main(String[] args) { LatLng center = new LatLng(37.422045, -122.084347); double radius = 1000; // 1km ElectronicFence fence = new ElectronicFence(center, radius); String address = "1600 Amphitheatre Parkway, Mountain View, CA"; boolean inFence = fence.inFence(address); System.out.println("Is " + address + " in fence? " + inFence); } } ``` 在这个示例中,我们使用了Google Maps API来获取地址对应的经纬度坐标,并使用LatLngBounds类定义了一个圆形范围。然后我们可以通过inFence方法来判断某个地址是否电子围栏内。 需要注意的是,在实际使用中,我们需要使用真实的API密钥和地图数据来完成电子围栏的功能。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值