百度地图API-JAVA判断2个区域是否重合(使用java.awt.geom.Line2D)

思路

1判断区域的线段是否相交

2判断区域的包含关系

图例

区域使用百度地图API-开源库-鼠标绘制工具条库制作

测试

boolean a = isCoinCide(locationList1, locationList3);
System.out.println("是否存在重合区域:" + a);

是否存在重合区域:false

boolean a = isCoinCide(locationList1, locationList2);
System.out.println("是否存在重合区域:" + a);

是否存在重合区域:true

boolean a = isCoinCide(locationList2, locationList2);
System.out.println("是否存在重合区域:" + a);

是否存在重合区域:true

boolean a = isCoinCide(locationList3, locationList4);
System.out.println("是否存在重合区域:" + a);

是否存在重合区域:true

boolean a = isCoinCide(locationList2, locationList4);
System.out.println("是否存在重合区域:" + a);

是否存在重合区域:false

说明

重点方法

1判断线段是否相交

private static boolean isIntersect(List<Line> lineList, List<Line> lineList2) {
        for (Line line : lineList) {
            for (Line line1 : lineList2) {
                //两条线段是否相交
                boolean b = Line2D.linesIntersect(line.location1.longitude, line.location1.latitude, line.location2.longitude, line.location2.latitude,
                        line1.location1.longitude, line1.location1.latitude, line1.location2.longitude, line1.location2.latitude);
                if (b) {
                    return true;
                }
            }
        }
        return false;
    }

2判断点是否在多边形内

private static boolean isPointInPolygon(Location location, List<Location> locationList2) {
        //点是否在多边形内
        GeneralPath path = new GeneralPath();
        //设定多边形起始点
        path.moveTo(locationList2.get(0).getLongitude(), locationList2.get(0).getLatitude());
        for (Location l : locationList2) {
            path.lineTo(l.getLongitude(), l.getLatitude());
        }
        //图像完成,封闭
        path.moveTo(locationList2.get(0).getLongitude(), locationList2.get(0).getLatitude());
        //多边形结束
        path.closePath();
        return path.contains(location.getLongitude(), location.getLatitude());
    }

源码

package com.asyf.demo.map.test02;

import java.awt.geom.GeneralPath;
import java.awt.geom.Line2D;
import java.util.ArrayList;
import java.util.List;

public class Test {

    /**
     * 判断2个区域是否重合
     *
     * @param args
     */
    public static void main(String[] args) {
        //区域1-四边形
        List<Location> locationList1 = new ArrayList<>();
        locationList1.add(new Location(117.21455815237613, 39.1469865872148));
        locationList1.add(new Location(117.22001984987679, 39.1441884617389));
        locationList1.add(new Location(117.21624696673489, 39.1395713095368));
        locationList1.add(new Location(117.2074795049575, 39.14060669749416));

        //区域2-三角形
        List<Location> locationList2 = new ArrayList<>();
        locationList2.add(new Location(117.21915747658721, 39.147798022626574));
        locationList2.add(new Location(117.22458324186748, 39.14323707353132));
        locationList2.add(new Location(117.21660628893888, 39.14270540976905));

        //区域3-三角形-和其他的不重合
        List<Location> locationList3 = new ArrayList<>();
        locationList3.add(new Location(117.22109781648876, 39.14122232629915));
        locationList3.add(new Location(117.22591273402224, 39.13820009645245));
        locationList3.add(new Location(117.21775611999165, 39.13710870353047));

        //区域4-三角形-在3的外面
        List<Location> locationList4 = new ArrayList<>();
        locationList4.add(new Location(117.22055883318278, 39.14228567236032));
        locationList4.add(new Location(117.22849985389098, 39.137780331962446));
        locationList4.add(new Location(117.2157079834289, 39.13526169203842));

        boolean a = isCoinCide(locationList2, locationList3);
        System.out.println("是否存在重合区域:" + a);

    }

    private static boolean isCoinCide(List<Location> locationList, List<Location> locationList2) {
        //获取四边形的线段
        List<Line> lineList = getLines(locationList);
        //获取三角形的线段
        List<Line> lineList2 = getLines(locationList2);
        //判断是否相交
        boolean isIntersect = isIntersect(lineList, lineList2);
        if (isIntersect) return true;
        //如果不相交判断是否包含-由于没有相交线段只要存在点在多边形内就说明包含
        boolean isPolygonInPolygon = isPolygonInPolygon(locationList, locationList2);
        if (isPolygonInPolygon) return true;
        return false;
    }

    private static boolean isPolygonInPolygon(List<Location> locationList, List<Location> locationList2) {
        //判断第一个多边形是否在第二个多边形内
        for (Location location : locationList) {
            boolean isPointInPolygon = isPointInPolygon(location, locationList2);
            if (isPointInPolygon) return true;
        }
        //判断第二个多边形是否在第一个多边形内
        for (Location location : locationList2) {
            boolean isPointInPolygon = isPointInPolygon(location, locationList);
            if (isPointInPolygon) return true;
        }
        return false;
    }

    private static boolean isPointInPolygon(Location location, List<Location> locationList2) {
        //点是否在多边形内
        GeneralPath path = new GeneralPath();
        //设定多边形起始点
        path.moveTo(locationList2.get(0).getLongitude(), locationList2.get(0).getLatitude());
        for (Location l : locationList2) {
            path.lineTo(l.getLongitude(), l.getLatitude());
        }
        //图像完成,封闭
        path.moveTo(locationList2.get(0).getLongitude(), locationList2.get(0).getLatitude());
        //多边形结束
        path.closePath();
        return path.contains(location.getLongitude(), location.getLatitude());
    }

    private static boolean isIntersect(List<Line> lineList, List<Line> lineList2) {
        for (Line line : lineList) {
            for (Line line1 : lineList2) {
                //两条线段是否相交
                boolean b = Line2D.linesIntersect(line.location1.longitude, line.location1.latitude, line.location2.longitude, line.location2.latitude,
                        line1.location1.longitude, line1.location1.latitude, line1.location2.longitude, line1.location2.latitude);
                if (b) {
                    return true;
                }
            }
        }
        return false;
    }

    private static List<Line> getLines(List<Location> locationList) {
        List<Line> lineList = new ArrayList();
        for (int i = 0; i < locationList.size(); i++) {
            if (i < locationList.size() - 1) {
                Location l = locationList.get(i);
                Location l2 = locationList.get(i + 1);
                Line line = new Line(l, l2);
                lineList.add(line);
            } else {
                Location l = locationList.get(i);
                Location l2 = locationList.get(0);
                Line line = new Line(l, l2);
                lineList.add(line);
            }
        }
        return lineList;
    }

    //线段
    public static class Line {
        private Location location1;//起点
        private Location location2;//终点

        public Line(Location location1, Location location2) {
            this.location1 = location1;
            this.location2 = location2;
        }

        public Location getLocation1() {
            return location1;
        }

        public void setLocation1(Location location1) {
            this.location1 = location1;
        }

        public Location getLocation2() {
            return location2;
        }

        public void setLocation2(Location location2) {
            this.location2 = location2;
        }

        @Override
        public String toString() {
            return "Line{" +
                    "location1=" + location1 +
                    ", location2=" + location2 +
                    '}';
        }
    }

    //经纬度坐标
    public static class Location {

        private double longitude;//经度
        private double latitude;//纬度

        public Location(double longitude, double latitude) {
            this.longitude = longitude;
            this.latitude = latitude;
        }

        public double getLongitude() {
            return longitude;
        }

        public void setLongitude(double longitude) {
            this.longitude = longitude;
        }

        public double getLatitude() {
            return latitude;
        }

        public void setLatitude(double latitude) {
            this.latitude = latitude;
        }

        @Override
        public String toString() {
            return "Location{" +
                    "longitude=" + longitude +
                    ", latitude=" + latitude +
                    '}';
        }
    }

}

参考文献

https://blog.csdn.net/c5113620/article/details/82014058

  • 4
    点赞
  • 15
    收藏
    觉得还不错? 一键收藏
  • 2
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值