Skip to content

Commit 18f352a

Browse files
committed
Remove weird left-side label pruning and add an option to draw labels last on top of all the polygons.
1 parent 6e8cc20 commit 18f352a

File tree

2 files changed

+63
-41
lines changed

2 files changed

+63
-41
lines changed

lib/src/layer/polygon_layer/label.dart

+29-38
Original file line numberDiff line numberDiff line change
@@ -17,46 +17,38 @@ void Function(Canvas canvas)? buildLabelTextPainter(
1717
TextStyle? labelStyle,
1818
double padding = 0,
1919
}) {
20-
double dx = placementPoint.dx;
21-
double dy = placementPoint.dy;
20+
final textSpan = TextSpan(text: labelText, style: labelStyle);
21+
final textPainter = TextPainter(
22+
text: textSpan,
23+
textAlign: TextAlign.center,
24+
textDirection: TextDirection.ltr,
25+
maxLines: 2,
26+
)..layout();
2227

23-
if (dx > 0) {
24-
final textSpan = TextSpan(text: labelText, style: labelStyle);
25-
final textPainter = TextPainter(
26-
text: textSpan,
27-
textAlign: TextAlign.center,
28-
textDirection: TextDirection.ltr,
29-
maxLines: 1,
30-
);
28+
final dx = placementPoint.dx - textPainter.width / 2;
29+
final dy = placementPoint.dy - textPainter.height / 2;
3130

32-
textPainter.layout();
33-
dx -= textPainter.width / 2;
34-
dy -= textPainter.height / 2;
31+
double maxDx = 0;
32+
var minDx = double.infinity;
33+
for (final point in points) {
34+
maxDx = math.max(maxDx, point.dx);
35+
minDx = math.min(minDx, point.dx);
36+
}
3537

36-
var maxDx = 0.0;
37-
var minDx = double.infinity;
38-
for (final point in points) {
39-
maxDx = math.max(maxDx, point.dx);
40-
minDx = math.min(minDx, point.dx);
41-
}
38+
if (maxDx - minDx - padding > textPainter.width) {
39+
return (canvas) {
40+
if (rotate) {
41+
canvas.save();
42+
canvas.translate(placementPoint.dx, placementPoint.dy);
43+
canvas.rotate(-rotationRad);
44+
canvas.translate(-placementPoint.dx, -placementPoint.dy);
45+
}
4246

43-
if (maxDx - minDx - padding > textPainter.width) {
44-
return (canvas) {
45-
if (rotate) {
46-
canvas.save();
47-
canvas.translate(placementPoint.dx, placementPoint.dy);
48-
canvas.rotate(-rotationRad);
49-
canvas.translate(-placementPoint.dx, -placementPoint.dy);
50-
}
51-
textPainter.paint(
52-
canvas,
53-
Offset(dx, dy),
54-
);
55-
if (rotate) {
56-
canvas.restore();
57-
}
58-
};
59-
}
47+
textPainter.paint(canvas, Offset(dx, dy));
48+
if (rotate) {
49+
canvas.restore();
50+
}
51+
};
6052
}
6153
return null;
6254
}
@@ -89,9 +81,8 @@ LatLng _computePolylabel(List<LatLng> points) {
8981
// i.e. cheaper at the expense off less optimal label placement.
9082
precision: 0.000001,
9183
);
92-
final latlng = LatLng(
84+
return LatLng(
9385
labelPosition.point.y.toDouble(),
9486
labelPosition.point.x.toDouble(),
9587
);
96-
return latlng;
9788
}

lib/src/layer/polygon_layer/polygon_layer.dart

+34-3
Original file line numberDiff line numberDiff line change
@@ -98,11 +98,15 @@ class PolygonLayer extends StatelessWidget {
9898
// Turn on/off per-polygon label drawing on the layer-level.
9999
final bool polygonLabels;
100100

101+
// Whether to draw labels last and thus over all the polygons.
102+
final bool drawLabelsLast;
103+
101104
const PolygonLayer({
102105
super.key,
103106
required this.polygons,
104107
this.polygonCulling = false,
105108
this.polygonLabels = true,
109+
this.drawLabelsLast = false,
106110
});
107111

108112
@override
@@ -118,7 +122,7 @@ class PolygonLayer extends StatelessWidget {
118122

119123
return MobileLayerTransformer(
120124
child: CustomPaint(
121-
painter: PolygonPainter(pgons, map, polygonLabels),
125+
painter: PolygonPainter(pgons, map, polygonLabels, drawLabelsLast),
122126
size: size,
123127
isComplex: true,
124128
),
@@ -131,8 +135,10 @@ class PolygonPainter extends CustomPainter {
131135
final MapCamera map;
132136
final LatLngBounds bounds;
133137
final bool polygonLabels;
138+
final bool drawLabelsLast;
134139

135-
PolygonPainter(this.polygons, this.map, this.polygonLabels)
140+
PolygonPainter(
141+
this.polygons, this.map, this.polygonLabels, this.drawLabelsLast)
136142
: bounds = map.visibleBounds;
137143

138144
int get hash {
@@ -235,7 +241,7 @@ class PolygonPainter extends CustomPainter {
235241
}
236242
}
237243

238-
if (polygonLabels && polygon.label != null) {
244+
if (polygonLabels && !drawLabelsLast && polygon.label != null) {
239245
// Labels are expensive because:
240246
// * they themselves cannot easily be pulled into our batched path
241247
// painting with the given text APIs
@@ -267,6 +273,31 @@ class PolygonPainter extends CustomPainter {
267273
}
268274

269275
drawPaths();
276+
277+
if (polygonLabels && drawLabelsLast) {
278+
for (final polygon in polygons) {
279+
final offsets = getOffsets(polygon.points);
280+
if (offsets.isEmpty) {
281+
continue;
282+
}
283+
284+
if (polygon.label != null) {
285+
final painter = buildLabelTextPainter(
286+
polygon.points,
287+
polygon.labelPosition,
288+
placementPoint: map.getOffsetFromOrigin(polygon.labelPosition),
289+
points: offsets,
290+
labelText: polygon.label!,
291+
labelStyle: polygon.labelStyle,
292+
rotationRad: map.rotationRad,
293+
rotate: polygon.rotateLabel,
294+
padding: 50,
295+
);
296+
297+
painter?.call(canvas);
298+
}
299+
}
300+
}
270301
}
271302

272303
Paint _getBorderPaint(Polygon polygon) {

0 commit comments

Comments
 (0)