@@ -7,6 +7,8 @@ import 'package:flutter/foundation.dart';
7
7
import 'package:flutter/widgets.dart' ;
8
8
import 'package:flutter_map/flutter_map.dart' ;
9
9
import 'package:flutter_map/src/layer/shared/layer_interactivity/internal_hit_detectable.dart' ;
10
+ import 'package:flutter_map/src/layer/shared/layer_projection_simplification/state.dart' ;
11
+ import 'package:flutter_map/src/layer/shared/layer_projection_simplification/widget.dart' ;
10
12
import 'package:flutter_map/src/layer/shared/line_patterns/pixel_hiker.dart' ;
11
13
import 'package:flutter_map/src/misc/offsets.dart' ;
12
14
import 'package:flutter_map/src/misc/point_in_polygon.dart' ;
@@ -21,7 +23,8 @@ part 'projected_polygon.dart';
21
23
22
24
/// A polygon layer for [FlutterMap] .
23
25
@immutable
24
- class PolygonLayer <R extends Object > extends StatefulWidget {
26
+ base class PolygonLayer <R extends Object >
27
+ extends ProjectionSimplificationManagementSupportedWidget {
25
28
/// [Polygon] s to draw
26
29
final List <Polygon <R >> polygons;
27
30
@@ -52,16 +55,6 @@ class PolygonLayer<R extends Object> extends StatefulWidget {
52
55
/// Defaults to `true` . Disabling is not recommended.
53
56
final bool polygonCulling;
54
57
55
- /// Distance between two neighboring polygon points, in logical pixels scaled
56
- /// to floored zoom
57
- ///
58
- /// Increasing this value results in points further apart being collapsed and
59
- /// thus more simplified polygons. Higher values improve performance at the
60
- /// cost of visual fidelity and vice versa.
61
- ///
62
- /// Defaults to 0.5. Set to 0 to disable simplification.
63
- final double simplificationTolerance;
64
-
65
58
/// Whether to draw per-polygon labels
66
59
///
67
60
/// Defaults to `true` .
@@ -82,79 +75,63 @@ class PolygonLayer<R extends Object> extends StatefulWidget {
82
75
this .useAltRendering = false ,
83
76
this .debugAltRenderer = false ,
84
77
this .polygonCulling = true ,
85
- this .simplificationTolerance = 0.5 ,
86
78
this .polygonLabels = true ,
87
79
this .drawLabelsLast = false ,
88
80
this .hitNotifier,
89
- }) : assert (
90
- simplificationTolerance >= 0 ,
91
- 'simplificationTolerance cannot be negative: $simplificationTolerance ' ,
92
- );
81
+ super .simplificationTolerance,
82
+ super .useDynamicUpdate,
83
+ }) : super ();
93
84
94
85
@override
95
86
State <PolygonLayer <R >> createState () => _PolygonLayerState <R >();
96
87
}
97
88
98
- class _PolygonLayerState <R extends Object > extends State <PolygonLayer <R >> {
99
- List <_ProjectedPolygon <R >>? _cachedProjectedPolygons;
100
- final _cachedSimplifiedPolygons = < int , List <_ProjectedPolygon <R >>> {};
101
-
102
- double ? _devicePixelRatio;
89
+ class _PolygonLayerState <R extends Object > extends State <PolygonLayer <R >>
90
+ with
91
+ ProjectionSimplificationManagement <_ProjectedPolygon <R >, Polygon <R >,
92
+ PolygonLayer <R >> {
93
+ @override
94
+ _ProjectedPolygon <R > projectElement ({
95
+ required Projection projection,
96
+ required Polygon <R > element,
97
+ }) =>
98
+ _ProjectedPolygon ._fromPolygon (projection, element);
103
99
104
100
@override
105
- void didUpdateWidget (PolygonLayer <R > oldWidget) {
106
- super .didUpdateWidget (oldWidget);
101
+ _ProjectedPolygon <R > simplifyProjectedElement ({
102
+ required _ProjectedPolygon <R > projectedElement,
103
+ required double tolerance,
104
+ }) =>
105
+ _ProjectedPolygon ._(
106
+ polygon: projectedElement.polygon,
107
+ points: simplifyPoints (
108
+ points: projectedElement.points,
109
+ tolerance: tolerance,
110
+ highQuality: true ,
111
+ ),
112
+ holePoints: List .generate (
113
+ projectedElement.holePoints.length,
114
+ (j) => simplifyPoints (
115
+ points: projectedElement.holePoints[j],
116
+ tolerance: tolerance,
117
+ highQuality: true ,
118
+ ),
119
+ growable: false ,
120
+ ),
121
+ );
107
122
108
- if (! listEquals (oldWidget.polygons, widget.polygons)) {
109
- // If the polylines have changed, then both the projections and the
110
- // projection-dependendent simplifications must be invalidated
111
- _cachedProjectedPolygons = null ;
112
- _cachedSimplifiedPolygons.clear ();
113
- } else if (oldWidget.simplificationTolerance !=
114
- widget.simplificationTolerance) {
115
- // If only the simplification tolerance has changed, this does not affect
116
- // the projections (as that is done before simplification), so only
117
- // invalidate the simplifications
118
- _cachedSimplifiedPolygons.clear ();
119
- }
120
- }
123
+ @override
124
+ Iterable <Polygon <R >> getElements (PolygonLayer <R > widget) => widget.polygons;
121
125
122
126
@override
123
127
Widget build (BuildContext context) {
124
- final camera = MapCamera . of (context);
128
+ super . build (context);
125
129
126
- final projected = _cachedProjectedPolygons ?? = List .generate (
127
- widget.polygons.length,
128
- (i) => _ProjectedPolygon ._fromPolygon (
129
- camera.crs.projection,
130
- widget.polygons[i],
131
- ),
132
- growable: false ,
133
- );
134
-
135
- late final List <_ProjectedPolygon <R >> simplified;
136
- if (widget.simplificationTolerance == 0 ) {
137
- simplified = projected;
138
- } else {
139
- // If the DPR has changed, invalidate the simplification cache
140
- final newDPR = MediaQuery .devicePixelRatioOf (context);
141
- if (newDPR != _devicePixelRatio) {
142
- _devicePixelRatio = newDPR;
143
- _cachedSimplifiedPolygons.clear ();
144
- }
145
-
146
- simplified = _cachedSimplifiedPolygons[camera.zoom.floor ()] ?? =
147
- _computeZoomLevelSimplification (
148
- camera: camera,
149
- polygons: projected,
150
- pixelTolerance: widget.simplificationTolerance,
151
- devicePixelRatio: newDPR,
152
- );
153
- }
130
+ final camera = MapCamera .of (context);
154
131
155
132
final culled = ! widget.polygonCulling
156
- ? simplified
157
- : simplified
133
+ ? simplifiedElements. toList ()
134
+ : simplifiedElements
158
135
.where (
159
136
(p) => p.polygon.boundingBox.isOverlapping (camera.visibleBounds),
160
137
)
@@ -213,45 +190,4 @@ class _PolygonLayerState<R extends Object> extends State<PolygonLayer<R>> {
213
190
yield prevValue += polygon.holePoints[i].length;
214
191
}
215
192
}
216
-
217
- List <_ProjectedPolygon <R >> _computeZoomLevelSimplification ({
218
- required MapCamera camera,
219
- required List <_ProjectedPolygon <R >> polygons,
220
- required double pixelTolerance,
221
- required double devicePixelRatio,
222
- }) {
223
- final tolerance = getEffectiveSimplificationTolerance (
224
- crs: camera.crs,
225
- zoom: camera.zoom.floor (),
226
- pixelTolerance: pixelTolerance,
227
- devicePixelRatio: devicePixelRatio,
228
- );
229
-
230
- return List <_ProjectedPolygon <R >>.generate (
231
- polygons.length,
232
- (i) {
233
- final polygon = polygons[i];
234
- final holes = polygon.holePoints;
235
-
236
- return _ProjectedPolygon ._(
237
- polygon: polygon.polygon,
238
- points: simplifyPoints (
239
- points: polygon.points,
240
- tolerance: tolerance,
241
- highQuality: true ,
242
- ),
243
- holePoints: List .generate (
244
- holes.length,
245
- (j) => simplifyPoints (
246
- points: holes[j],
247
- tolerance: tolerance,
248
- highQuality: true ,
249
- ),
250
- growable: false ,
251
- ),
252
- );
253
- },
254
- growable: false ,
255
- );
256
- }
257
193
}
0 commit comments