【问题标题】:How to parse GeoJSON to a type in Dart/Flutter如何将 GeoJSON 解析为 Dart/Flutter 中的类型
【发布时间】:2021-03-30 07:37:47
【问题描述】:

我的服务器上有这样的 JSON:

        {
          "coordinates": [
            [
              [
                -122.41251225499991,
                37.78047851100007
              ],
              [
                -122.42194124699989,
                37.77303605000009
              ],
              ...trimmed...
              [
                -122.41251225499991,
                37.78047851100007
              ]
            ]
          ],
          "crs": {
            "properties": {
              "name": "EPSG:4326"
            },
            "type": "name"
          },
          "type": "Polygon"
        }

我想在我的服务器上解析这个,但我不知道该怎么做。我找到了两个与 GeoJSON 一起使用的 Dart 库:

geojson https://pub.dev/packages/geojson geojson_vi https://pub.dev/packages/geojson_vi

这两个都只能从字符串函数中解析,但我有一张地图。在 Dart/Flutter 中解析此类内容的最佳方法是什么?

【问题讨论】:

    标签: flutter dart


    【解决方案1】:

    您可以将地图转换为字符串并使用geojson包来解析GeoJson数据。

    【讨论】:

      【解决方案2】:

      是这个意思吗?

      class GetJsonData{
          GetJsonData({
              this.coordinates,
              this.crs,
              this.type,
          });
      
          List<List<List<double>>> coordinates;
          Crs crs;
          String type;
      
          factory GetVehicleInfo.fromJson(Map<String, dynamic> json) => GetVehicleInfo(
              coordinates: List<List<List<double>>>.from(json["coordinates"].map((x) => List<List<double>>.from(x.map((x) => List<double>.from(x.map((x) => x.toDouble())))))),
              crs: Crs.fromJson(json["crs"]),
              type: json["type"],
          );
      
          Map<String, dynamic> toJson() => {
              "coordinates": List<dynamic>.from(coordinates.map((x) => List<dynamic>.from(x.map((x) => List<dynamic>.from(x.map((x) => x)))))),
              "crs": crs.toJson(),
              "type": type,
          };
      }
      
      class Crs {
          Crs({
              this.properties,
              this.type,
          });
      
          Properties properties;
          String type;
      
          factory Crs.fromJson(Map<String, dynamic> json) => Crs(
              properties: Properties.fromJson(json["properties"]),
              type: json["type"],
          );
      
          Map<String, dynamic> toJson() => {
              "properties": properties.toJson(),
              "type": type,
          };
      }
      
      class Properties {
          Properties({
              this.name,
          });
      
          String name;
      
          factory Properties.fromJson(Map<String, dynamic> json) => Properties(
              name: json["name"],
          );
      
          Map<String, dynamic> toJson() => {
              "name": name,
          };
      }
      
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2021-05-17
        • 2020-11-25
        • 1970-01-01
        • 2019-08-31
        • 1970-01-01
        • 2023-03-09
        • 1970-01-01
        • 2022-06-27
        相关资源
        最近更新 更多