flutterdartgeojsonarea

How to compute a GeoJson geometry area in dart / flutter


I'm building a flutter mobile app and I need to compute an area of some GeoJson geometries.

Say we have a GeoJson-like object:

final geo = {
    "type": "Polygon",
    "coordinates": [[
        [-122.085, 37.423],
        [-122.083, 37.423],
        [-122.083, 37.421],
        [-122.085, 37.421],
        [-122.085, 37.423]
    ]]
};

Assuming the projection is EPSG:4326, how do we get the actual area of the geometry using flutter or dart?

Tried to use dart-simple-features, but this is no longer maintained and requires SDK < 2.0.0.

Another option that came to my mind is to use some JavaScript library in combination with flutter_webview_plugin, but oh my... That seems like an overkill!

Also there is a possibility to use platform-specific code, but for the sake of development experience: let's avoid testing on multiple platforms if possible...

Any ideas? Or recommendations?


Solution

  • All the answers here are either deprecated packages or don't work. So here is a package that works with flutter 3. package maps_toolkit

    import 'package:maps_toolkit/maps_toolkit.dart';
    
    main() {
    
      final p1 = LatLng(45.153474463955796, 39.33852195739747);
      final p2 = LatLng(45.153474463955796, 39.33972358703614);
      final p3 = LatLng(45.15252112936569, 39.33972358703614);
      final p4 = LatLng(45.1525022138355, 39.3385460972786);
    
      val areaInSquareMeters = SphericalUtil.computeArea([p1, p2, p3, p4, p1]);
    }