当向多边形添加数千点时,Flutter应用程序崩溃了

发布于 2025-02-07 19:53:41 字数 4591 浏览 2 评论 0原文

我用

       try {
                          await state.getLocationPolygonByAddress(
                              state.foundedAddress[index].address);
                          var foundedSvg = state.foundedSvg;
                          List<LatLng> list = [];
                          setState(() {
                            testPolygon.points.clear();
                          });

                          for (int i = 0; i < foundedSvg.length; i++) {
                            for (int j = 0; j <
                                foundedSvg[i].geojson.coordinates
                                    .length; j++) {
                              for (int k =0;k<foundedSvg[i].geojson.coordinates[j].length;k++) 
                               {

                                for (int z = 0; z <
                                    foundedSvg[i].geojson.coordinates[j][k]
                                        .length; z++) {

                                 /* if(foundedSvg[i].geojson
                                      .coordinates[j][k].first is double  && 
                                      foundedSvg[i].geojson
                                      .coordinates[j][k].last is double) {*/
                                    list.add(LatLng(foundedSvg[i].geojson
                                        .coordinates[j][k].first,
                                        foundedSvg[i].geojson
                                            .coordinates[j][k].last));
                               /*   }
                                  else{
                                    print(k);
                                  }*/

                                }
                              }
                            }
                          }
                          print(list.length);

                         state.clearfoundedAddress();






                          setState(() {
                           testPolygon.points.addAll(list);

                          });
                          print('polyeditor length: ${polyEditor.points.length}');

                        }catch(e){
                          print('error :'+e.toString());
                        }

要收到伦敦城市的坐标并尝试绘制伦敦城市的多边形,但是当我使用此代码时,当我尝试在此行应用程序中添加Polygon的积分列表时,

testPolygon.points.addAll(list);

。 但是,如果积分数很少,它可以很好地工作。

这是我的fluttermap层,

     late PolyEditor polyEditor;

  List<Polygon> polygons = [];
 // LatLngBounds boundingBox = LatLngBounds();
   var testPolygon = Polygon(
  color: Colors.black.withOpacity(0.3),
  points: [],
  borderColor: Colors.black,
  isFilled: true,

  borderStrokeWidth: 1.0);



 @override
 void initState() {
 super.initState();
 //  processData();
  polyEditor = PolyEditor(
  addClosePathMarker: true,
  points: testPolygon.points,

  pointIcon: const Icon(
    Icons.lens,
    size: 10,
    color: Colors.black,
  ),
  intermediateIcon: const Icon(Icons.lens, size: 10, color: 
  Colors.black),
  callbackRefresh: () => {setState(() {})},
  );

polygons.add(testPolygon);


 }

  SearchController searchController = Get.put(SearchController());
  MapController controller = MapController();



  @override
  Widget build(BuildContext context) {
  return Scaffold(
  body: Stack(
    alignment: Alignment.center,
    children: [
      Center(
        child: FlutterMap(
          mapController: controller,


          options: MapOptions(
            // bounds: LatLngBounds(),


            allowPanningOnScrollingParent: false,


              onTap: (_, ll) {
              print(ll);

              polyEditor.add(testPolygon.points, ll);

            },
            plugins: [
              DragMarkerPlugin(),
            ],
            center: LatLng(32.5231, 51.6765),
            zoom: 4.0,

          ),
          layers: [
            TileLayerOptions(
                urlTemplate:
                    
                'https://{s}.tile.openstreetmap.org/{z}/{x}/{y}.png',
                subdomains: ['a', 'b', 'c']),
            PolygonLayerOptions(polygons: polygons,polygonCulling: 
                    true),
           DragMarkerPluginOptions(markers: polyEditor.edit(),),
          ],
        ),
      ),

结果应该像这样 结果图片

我想要这样的结果 link

I used nominatim OpenStreetMap API to recieve coordinate of london city and try to draw polygon of london city, but cause the number of points is too alot when I use this code

       try {
                          await state.getLocationPolygonByAddress(
                              state.foundedAddress[index].address);
                          var foundedSvg = state.foundedSvg;
                          List<LatLng> list = [];
                          setState(() {
                            testPolygon.points.clear();
                          });

                          for (int i = 0; i < foundedSvg.length; i++) {
                            for (int j = 0; j <
                                foundedSvg[i].geojson.coordinates
                                    .length; j++) {
                              for (int k =0;k<foundedSvg[i].geojson.coordinates[j].length;k++) 
                               {

                                for (int z = 0; z <
                                    foundedSvg[i].geojson.coordinates[j][k]
                                        .length; z++) {

                                 /* if(foundedSvg[i].geojson
                                      .coordinates[j][k].first is double  && 
                                      foundedSvg[i].geojson
                                      .coordinates[j][k].last is double) {*/
                                    list.add(LatLng(foundedSvg[i].geojson
                                        .coordinates[j][k].first,
                                        foundedSvg[i].geojson
                                            .coordinates[j][k].last));
                               /*   }
                                  else{
                                    print(k);
                                  }*/

                                }
                              }
                            }
                          }
                          print(list.length);

                         state.clearfoundedAddress();






                          setState(() {
                           testPolygon.points.addAll(list);

                          });
                          print('polyeditor length: ${polyEditor.points.length}');

                        }catch(e){
                          print('error :'+e.toString());
                        }

and when try to add list of points to polygon in this line

testPolygon.points.addAll(list);

app crashed .
but it work good If the number of points is small.

this is my FlutterMap layer

     late PolyEditor polyEditor;

  List<Polygon> polygons = [];
 // LatLngBounds boundingBox = LatLngBounds();
   var testPolygon = Polygon(
  color: Colors.black.withOpacity(0.3),
  points: [],
  borderColor: Colors.black,
  isFilled: true,

  borderStrokeWidth: 1.0);



 @override
 void initState() {
 super.initState();
 //  processData();
  polyEditor = PolyEditor(
  addClosePathMarker: true,
  points: testPolygon.points,

  pointIcon: const Icon(
    Icons.lens,
    size: 10,
    color: Colors.black,
  ),
  intermediateIcon: const Icon(Icons.lens, size: 10, color: 
  Colors.black),
  callbackRefresh: () => {setState(() {})},
  );

polygons.add(testPolygon);


 }

  SearchController searchController = Get.put(SearchController());
  MapController controller = MapController();



  @override
  Widget build(BuildContext context) {
  return Scaffold(
  body: Stack(
    alignment: Alignment.center,
    children: [
      Center(
        child: FlutterMap(
          mapController: controller,


          options: MapOptions(
            // bounds: LatLngBounds(),


            allowPanningOnScrollingParent: false,


              onTap: (_, ll) {
              print(ll);

              polyEditor.add(testPolygon.points, ll);

            },
            plugins: [
              DragMarkerPlugin(),
            ],
            center: LatLng(32.5231, 51.6765),
            zoom: 4.0,

          ),
          layers: [
            TileLayerOptions(
                urlTemplate:
                    
                'https://{s}.tile.openstreetmap.org/{z}/{x}/{y}.png',
                subdomains: ['a', 'b', 'c']),
            PolygonLayerOptions(polygons: polygons,polygonCulling: 
                    true),
           DragMarkerPluginOptions(markers: polyEditor.edit(),),
          ],
        ),
      ),

the result should be like this
result picture

I want a result like this link

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

入怼 2025-02-14 19:53:41

是的,通过删除解决了此问题导入'tovent'软件包:flutter_map_line_editor/polyeditor.dart'; 并

  DragMarkerPluginOptions(markers: polyEditor.edit(),),
  polyEditor = PolyEditor(
  addClosePathMarker: true,
  points: testPolygon.points,

  pointIcon: const Icon(
    Icons.lens,
    size: 10,
    color: Colors.black,
  ),
  intermediateIcon: const Icon(Icons.lens, size: 10, color: Colors.black),
  callbackRefresh: () => {setState(() {})},
);`

在代码中 删除此行
@Lan说正确的答案

Yes this problem solved by deleting import ' package:flutter_map_line_editor/polyeditor.dart'; and delete this lines

  DragMarkerPluginOptions(markers: polyEditor.edit(),),
  polyEditor = PolyEditor(
  addClosePathMarker: true,
  points: testPolygon.points,

  pointIcon: const Icon(
    Icons.lens,
    size: 10,
    color: Colors.black,
  ),
  intermediateIcon: const Icon(Icons.lens, size: 10, color: Colors.black),
  callbackRefresh: () => {setState(() {})},
);`

in code
@lan say correct answer

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文