GeomPolygon.fromJson constructor

GeomPolygon.fromJson(
  1. Map<String, dynamic> inputJson, {
  2. bool withInvertedCoords = false,
})

Crée une instance à partir d'un GeoJSON.

Implementation

GeomPolygon.fromJson(Map<String, dynamic> inputJson, {bool withInvertedCoords = false}) {
  List<LatLng> outerPointsList = [];
  List<List<LatLng>> innerPointsList = [];

  final dynamic rawCoords = inputJson['coordinates'];
  if (rawCoords == null || rawCoords is! List) {
    throw const FormatException('Invalid or missing "coordinates" in GeoJSON polygon');
  }

  final List coordinates = rawCoords;

  for (int i = 0; i < coordinates.length; i++) {
    List<LatLng> currentPoints = [];

    final List outerList = coordinates[i];
    for (var point in outerList) {
      if (point.length >= 2) {
        final lat = point[0] is num ? point[0].toDouble() : 0.0;
        final lon = point[1] is num ? point[1].toDouble() : 0.0;
        currentPoints.add(withInvertedCoords ? LatLng(lon, lat) : LatLng(lat, lon));
      }
    }

    if (i == 0) {
      outerPointsList = currentPoints;
    } else {
      innerPointsList.add(currentPoints);
    }
  }

  outerPoints = outerPointsList;
  innerPoints = innerPointsList;
}