无法从 GeoJson 未处理的异常中提取坐标:类型“List<dynamic>”不是类型“double”的子类型

问题描述 投票:0回答:1

我正在尝试从 geojson 数据中获取坐标列表,当坐标不在数组中时一切正常,例如这里的最后 4 个坐标,我正在尝试这样做以从 world map 获取世界地图的边界,简而言之,没有任何坐标阵列的国家可以正常工作,而那些有坐标阵列的国家会出错

{
    "type": "FeatureCollection",
    "features": [
        {
            "type": "Feature",
            "properties": {
                "ADMIN": "Anguilla",
                "ISO_A3": "AIA"
            },
            "geometry": {
                "type": "MultiPolygon",
                "coordinates": [
                    [
                        [
                            [
                                -63.037668423999946,
                                18.212958075000031
                            ],
                            [
                                -63.099517381999959,
                                18.176174221000124
                            ],
                            [
                                -63.102365688999896,
                                18.180405992000161
                            ],
                            [
                                -63.109120245999918,
                                18.185044664000188
                            ],
                            [
                                -63.113840298999946,
                                18.189846096000068
                            ],
                            [
                                -63.136830206999917,
                                18.173407294000086
                            ],
                            [
                                -63.150502081999946,
                                18.169094143000095
                            ],
                            [
                                -63.167836066999939,
                                18.169338283000044
                            ],
                            [
                                -63.142079230999911,
                                18.198146877000156
                            ],
                            [
                                -63.134348110999895,
                                18.204087632
                            ],
                            [
                                -63.122792120999861,
                                18.20807526200015
                            ],
                            [
                                -63.09723873599998,
                                18.212469794000143
                            ],
                            [
                                -63.085845506999902,
                                18.217718817000119
                            ],
                            [
                                -63.0677791009999,
                                18.2364769550001
                            ],
                            [
                                -63.055083787999934,
                                18.254339911000059
                            ],
                            [
                                -63.038197394999941,
                                18.267726955000157
                            ],
                            [
                                -63.007394985999952,
                                18.273016669000029
                            ],
                            [
                                -62.983998175999886,
                                18.276434637000037
                            ],
                            [
                                -62.972645636999914,
                                18.275864976000079
                            ],
                            [
                                -62.972889777999853,
                                18.269273179
                            ],
                            [
                                -62.992909308999913,
                                18.236883856000091
                            ],
                            [
                                -63.000559048999946,
                                18.227362372000087
                            ],
                            [
                                -63.011545376999919,
                                18.220445054
                            ],
                            [
                                -63.037668423999946,
                                18.212958075000031
                            ]
                        ]
                    ],
                    [
                        [
                            [
                                -63.423573370999861,
                                18.600043036000059
                            ],
                            [
                                -63.427967902999939,
                                18.592840887000122
                            ],
                            [
                                -63.428822394999941,
                                18.601263739000061
                            ],
                            [
                                -63.423573370999861,
                                18.600043036000059
                            ]
                        ]
                    ]
                ]
            }
        }
       
    ]
}

这是我尝试使用的代码


  List<LatLng> border = [];
  List<List<LatLng>> borders = [];
  Future<void> loadGeoJson() async {
    // Load GeoJSON data from asset file
    String data = await rootBundle.loadString('assets/nepal.geojson');
    Map<String, dynamic> geoJson = await json.decode(data);

    List<dynamic> features = geoJson['features'];
    for (int i = 0; i < features.length; i++) {
      List<dynamic> coordinates = features[i]['geometry']['coordinates'][0];
      border = coordinates
          .map((coordinate) => LatLng(coordinate[1], coordinate[0]))
          .toList();
      borders.add(border);
    }
  }

如果坐标不在数组中,它工作正常,但是当一个存在时我得到错误

Unhandled Exception: type 'List<dynamic>' is not a subtype of type 'double'

flutter dart geojson
1个回答
0
投票

您有 3 个嵌套数组,但您将其视为单个数组,因此您应该再添加两个

[0]

List<dynamic> coordinates = features[i]['geometry']['coordinates'][0][0][0];
© www.soinside.com 2019 - 2024. All rights reserved.