You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何正确初始化用于生成多边形的List<List<Point>>集合

正确初始化嵌套List<List<Point>>生成多边形的方法

你的问题出在每次循环都单独添加一个仅含单个Point的子List到外层集合,最终得到的结构是[[Point1], [Point2], ...],但多边形需要的是一个子List包含所有顶点(含首尾闭合点),再把这个子List放到外层List中,正确结构应为[[Point1, Point2, ..., Point1]]。

正确代码示例

// 外层List:用于存储多边形集合(支持多个多边形)
List<List<Point>> latLngPoints = new ArrayList<>();
// 内层List:存储单个多边形的所有顶点
List<Point> polygonVertices = new ArrayList<>();

// 循环遍历你的经纬度数据源(比如yourLatLngList是你的LatLng集合)
for (LatLng latLng : yourLatLngList) {
    polygonVertices.add(Point.fromLngLat(latLng.longitude, latLng.latitude));
}

// 关键:闭合多边形,必须添加第一个点作为最后一个点,确保图形闭合
if (!polygonVertices.isEmpty()) {
    Point firstVertex = polygonVertices.get(0);
    polygonVertices.add(Point.fromLngLat(firstVertex.longitude(), firstVertex.latitude()));
}

// 将完整的多边形顶点列表加入外层集合
latLngPoints.add(polygonVertices);

多多边形场景扩展

如果需要生成多个多边形,只需重复创建内层List、填充顶点、闭合、添加到外层的流程:

List<List<Point>> multiPolygons = new ArrayList<>();

// 第一个多边形
List<Point> polygon1 = new ArrayList<>();
// 填充polygon1的顶点...
// 闭合polygon1...
multiPolygons.add(polygon1);

// 第二个多边形
List<Point> polygon2 = new ArrayList<>();
// 填充polygon2的顶点...
// 闭合polygon2...
multiPolygons.add(polygon2);

内容的提问来源于stack exchange,提问作者kregtopoli

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.06.24 00:04:57