Java to determine the map range_Baidu map java to determine whether the current location is within the polygonal area

 pom

   <!-- https://mvnrepository.com/artifact/org.geotools/gt-shapefile -->
        <dependency>
            <groupId>org.geotools</groupId>
            <artifactId>gt-shapefile</artifactId>
            <version>26.0</version>
        </dependency>
        <!-- https://mvnrepository.com/artifact/org.geotools/gt-geojson -->
        <dependency>
            <groupId>org.geotools</groupId>
            <artifactId>gt-geojson</artifactId>
            <version>26.0</version>
            <exclusions>
                <exclusion>
                    <artifactId>junit</artifactId>
                    <groupId>junit</groupId>
                </exclusion>
            </exclusions>
        </dependency>
    public static void main(String[] args) {

        //首先创建一个GeometryFactory对象,用于创建点和多边形对象
        GeometryFactory geometryFactory = JTSFactoryFinder.getGeometryFactory();

        //定义经纬度区域范围的四个顶点坐标
        Coordinate[] coordinates = new Coordinate[]{
                new Coordinate(36, 120),
                new Coordinate(36, 122),
                new Coordinate(34, 122),
                new Coordinate(34, 120),
                new Coordinate(36, 120)
        };

        //使用这些坐标创建一个多边形对象
        Polygon polygon = geometryFactory.createPolygon(coordinates);

        //定义一个点的坐标
        Coordinate pointCoordinate = new Coordinate(35, 121);

        //使用这个坐标创建一个点对象
        Point point = geometryFactory.createPoint(pointCoordinate);

        //判断点是否在多边形中
        boolean isContained = polygon.contains(point);

        //输出结果
        if (isContained) {
            System.out.println("该点在经纬度区域范围内!");
        } else {
            System.out.println("该点不在经纬度区域范围内!");
        }
    }

Guess you like

Origin blog.csdn.net/weixin_41796956/article/details/130851990