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

Flutter中如何将List<List<String>>转为List<List<num>>用于谷歌地图多边形校验

Flutter 坐标类型转换解决方案

核心实现逻辑

REST API返回的嵌套字符串列表需要两层遍历,将每个字符串格式的经纬度值解析为num类型即可适配poly包的参数要求。

基础转换代码

直接嵌套调用map方法完成转换,适合可以确保API返回的坐标字符串全部为合法数字的场景:

// 原始API返回数据
List<List<String>> rawPolygon = [
  ["113.264385", "23.129110"],
  ["113.274385", "23.139110"],
  ["113.264385", "23.139110"]
];

// 类型转换
List<List<num>> polygonCoords = rawPolygon
    .map((pointStr) => pointStr.map(num.parse).toList())
    .toList();

带容错的转换方案

如果存在API返回非数字字符串的可能,建议增加异常处理,过滤无效坐标避免运行崩溃:

List<List<num>> parsePolygonCoords(List<List<String>> rawData) {
  List<List<num>> validCoords = [];
  for (var rawPoint in rawData) {
    // 每个坐标点必须包含经度、纬度两个值
    if (rawPoint.length != 2) continue;
    num? lng = num.tryParse(rawPoint[0]);
    num? lat = num.tryParse(rawPoint[1]);
    if (lng != null && lat != null) {
      validCoords.add([lng, lat]);
    }
  }
  return validCoords;
}

// 调用示例
List<List<num>> processedCoords = parsePolygonCoords(rawPolygon);

转换后得到的processedCoords即可直接传入poly包的相关方法执行点在多边形内的检测逻辑。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.09.26 03:57:03