要判断一个经纬度是否在一个区域内,可以使用点与多边形的相交判断方法。以下是一个使用Java实现的示例代码:\n\njava\nimport java.util.ArrayList;\nimport java.util.List;\n\npublic class Main {\n public static void main(String[] args) {\n // 定义一个表示区域边界范围的经纬度集合\n List<LatLng> boundary = new ArrayList<>();\n boundary.add(new LatLng(39.9366, 116.3845));\n boundary.add(new LatLng(39.9366, 116.3945));\n boundary.add(new LatLng(39.9466, 116.3945));\n boundary.add(new LatLng(39.9466, 116.3845));\n\n // 定义一个待判断的经纬度点\n LatLng point = new LatLng(39.9416, 116.3895);\n\n // 判断经纬度点是否在区域内\n boolean isInBoundary = isPointInBoundary(point, boundary);\n System.out.println(isInBoundary);\n }\n\n // 判断经纬度点是否在区域内\n public static boolean isPointInBoundary(LatLng point, List<LatLng> boundary) {\n int intersectCount = 0;\n for (int i = 0; i < boundary.size() - 1; i++) {\n if (rayCastIntersect(point, boundary.get(i), boundary.get(i + 1))) {\n intersectCount++;\n }\n }\n return intersectCount % 2 == 1;\n }\n\n // 判断点与线段的相交情况\n private static boolean rayCastIntersect(LatLng point, LatLng p1, LatLng p2) {\n double px = point.longitude;\n double py = point.latitude;\n double p1x = p1.longitude;\n double p1y = p1.latitude;\n double p2x = p2.longitude;\n double p2y = p2.latitude;\n\n if (p1y == p2y) {\n return false;\n }\n if (py < Math.min(p1y, p2y) || py > Math.max(p1y, p2y)) {\n return false;\n }\n if (px > Math.max(p1x, p2x)) {\n return false;\n }\n double x = p1x + (py - p1y) * (p2x - p1x) / (p2y - p1y);\n return x >= px;\n }\n}\n\nclass LatLng {\n double latitude;\n double longitude;\n\n public LatLng(double latitude, double longitude) {\n this.latitude = latitude;\n this.longitude = longitude;\n }\n}\n\n\n在上述代码中,我们首先定义了一个表示区域边界范围的经纬度集合boundary,然后定义了一个待判断的经纬度点point。接下来,我们通过调用isPointInBoundary方法来判断point是否在boundary区域内。\n\nisPointInBoundary方法遍历boundary集合中每一条边,通过调用rayCastIntersect方法判断point与边的相交情况。rayCastIntersect方法使用射线法判断点与线段的相交情况。\n\n最后,根据相交次数的奇偶性来判断point是否在区域内,如果相交次数为奇数,则表示在区域内,否则表示在区域外。\n\n希望对你有所帮助!

Java 判断经纬度点是否在区域内 - 详细代码示例

原文地址: https://www.cveoy.top/t/topic/pvzy 著作权归作者所有。请勿转载和采集!

免费AI点我,无需注册和登录