最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

flutter - I am facing difficulty in convertion of GeoJson MultiPolygon. in dart - Stack Overflow

programmeradmin8浏览0评论

I am building a screen in flutter where by selecting the city we can see the administrative area of the city using thepolyline. I am using open maps api for that . This is working fine in case of polygon . but the issue is when the city is getting multipolygon in the result the model conversion is not working or to be honest I am not capable of it to convert it. I am attaching all the nmultipolygon data using pastebin as that's a large file GeoJson Multi Polygon Data

Now I am attaching my class where I am converting it from the json

    class Geojson {
  final String? type;
  final List<XPolygon> coordinates;
  final List<List> polyCoordinates;

  Geojson({
    this.type,
    this.coordinates = const [],
    this.polyCoordinates = const [],
  });

  factory Geojson.fromJson(Map<String, dynamic> json) => Geojson(
      type: json["type"],
      coordinates: json["type"] != "Polygon"
          ? []
          : json["coordinates"] == null
              ? []
              : (json["coordinates"] as List).isEmpty
                  ? []
                  : (json["coordinates"] as List)
                      .expand((polygonData) => polygonData)
                      .map((e) => XPolygon(
                          lat: e.last.toString(), long: e.first.toString()))
                      .toList(),
      polyCoordinates: json["type"] != "MultiPolygon"
          ? []
          : (json["coordinates"] as List<List>)
              .expand((polygonData) => polygonData)
              .map(
                (e) => e
                    .map(
                      (e) => XPolygon(
                          lat: e.last.toString(), long: e.first.toString()),
                    )
                    .toList(),
              )
              .toList()
      // : [
      //     ...[json["coordinates"][0]]
      //   ]
      //     .map((e) => XPolygon(
      //         lat: e.last.toString(), long: e.first.toString()))
      //     .toList(),
      );

  Map<String, dynamic> toJson() => {
        "type": type,
        "coordinates": coordinates,
        "polyCoordinates": polyCoordinates
      };

  @override
  String toString() => jsonEncode(toJson());
}

Now I am attaching the logic where I am using the list to display the polylines in ui

     List<LatLng> cityboundaryCoordinates = [];
  List<List<LatLng>> multiPolyCityBounderyCoordinates = [];
  createCityPolyLine() {
    if (cityboundaryCoordinates.length > 3) {
      polylines.add(
        Polyline(
          polylineId: PolylineId(cityboundaryCoordinates.first.toString()),
          points: cityboundaryCoordinates,
          width: 3,
          color: Colors.red,
        ),
      );
    }
    if (multiPolyCityBounderyCoordinates.length > 3) {}
  }

for polygos the screen is displaying polylines of the city. For multipolygon what I need to do to make it work?? Please if possible specify the changes so that it will be easy for me to do the changes. Thanks for noticing it.

发布评论

评论列表(0)

  1. 暂无评论