当向多边形添加数千点时,Flutter应用程序崩溃了
我用
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 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
是的,通过删除解决了此问题导入'tovent'软件包:flutter_map_line_editor/polyeditor.dart'; 并
在代码中 删除此行
@Lan说正确的答案
Yes this problem solved by deleting
import ' package:flutter_map_line_editor/polyeditor.dart';
and delete this linesin code
@lan say correct answer