Skip to content

Commit eaecd23

Browse files
Allow Polyline width to be specified in meters (#1404)
* feat: polyline stroke width in meter * fix: polyline stroke width in meter errors not keeping the size after move map camera * Update lib/src/layer/polyline_layer.dart Co-authored-by: Luka S <github@jaffaketchup.dev>
1 parent 89f6c2b commit eaecd23

File tree

1 file changed

+28
-7
lines changed

1 file changed

+28
-7
lines changed

lib/src/layer/polyline_layer.dart

+28-7
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ class Polyline {
1919
final bool isDotted;
2020
final StrokeCap strokeCap;
2121
final StrokeJoin strokeJoin;
22+
final bool useStrokeWidthInMeter;
2223
late LatLngBounds boundingBox;
2324

2425
Polyline({
@@ -33,6 +34,7 @@ class Polyline {
3334
this.isDotted = false,
3435
this.strokeCap = StrokeCap.round,
3536
this.strokeJoin = StrokeJoin.round,
37+
this.useStrokeWidthInMeter = false,
3638
});
3739
}
3840

@@ -68,6 +70,7 @@ class PolylineLayer extends StatelessWidget {
6870
@override
6971
Widget build(BuildContext context) {
7072
final map = FlutterMapState.maybeOf(context)!;
73+
7174
return LayoutBuilder(
7275
builder: (BuildContext context, BoxConstraints bc) {
7376
final size = Size(bc.maxWidth, bc.maxHeight);
@@ -87,7 +90,7 @@ class PolylineLayer extends StatelessWidget {
8790
polylineWidgets.add(
8891
CustomPaint(
8992
key: polylineOpt.key,
90-
painter: PolylinePainter(polylineOpt, saveLayers),
93+
painter: PolylinePainter(polylineOpt, saveLayers, map),
9194
size: size,
9295
),
9396
);
@@ -123,17 +126,36 @@ class PolylinePainter extends CustomPainter {
123126
/// {@endtemplate}
124127
final bool saveLayers;
125128

126-
PolylinePainter(this.polylineOpt, this.saveLayers);
129+
final FlutterMapState map;
130+
131+
PolylinePainter(this.polylineOpt, this.saveLayers, this.map);
127132

128133
@override
129134
void paint(Canvas canvas, Size size) {
130135
if (polylineOpt.offsets.isEmpty) {
131136
return;
132137
}
138+
133139
final rect = Offset.zero & size;
134140
canvas.clipRect(rect);
141+
142+
late final double strokeWidth;
143+
if (polylineOpt.useStrokeWidthInMeter) {
144+
final firstPoint = polylineOpt.points.first;
145+
final firstOffset = polylineOpt.offsets.first;
146+
final r = const Distance().offset(
147+
firstPoint,
148+
polylineOpt.strokeWidth,
149+
180,
150+
);
151+
final delta = firstOffset - map.getOffsetFromOrigin(r);
152+
153+
strokeWidth = delta.distance;
154+
} else {
155+
strokeWidth = polylineOpt.strokeWidth;
156+
}
135157
final paint = Paint()
136-
..strokeWidth = polylineOpt.strokeWidth
158+
..strokeWidth = strokeWidth
137159
..strokeCap = polylineOpt.strokeCap
138160
..strokeJoin = polylineOpt.strokeJoin
139161
..blendMode = BlendMode.srcOver;
@@ -150,7 +172,7 @@ class PolylinePainter extends CustomPainter {
150172
if (polylineOpt.borderColor != null) {
151173
filterPaint = Paint()
152174
..color = polylineOpt.borderColor!.withAlpha(255)
153-
..strokeWidth = polylineOpt.strokeWidth
175+
..strokeWidth = strokeWidth
154176
..strokeCap = polylineOpt.strokeCap
155177
..strokeJoin = polylineOpt.strokeJoin
156178
..blendMode = BlendMode.dstOut;
@@ -159,16 +181,15 @@ class PolylinePainter extends CustomPainter {
159181
final borderPaint = polylineOpt.borderStrokeWidth > 0.0
160182
? (Paint()
161183
..color = polylineOpt.borderColor ?? const Color(0x00000000)
162-
..strokeWidth =
163-
polylineOpt.strokeWidth + polylineOpt.borderStrokeWidth
184+
..strokeWidth = strokeWidth + polylineOpt.borderStrokeWidth
164185
..strokeCap = polylineOpt.strokeCap
165186
..strokeJoin = polylineOpt.strokeJoin
166187
..blendMode = BlendMode.srcOver)
167188
: null;
168189
final radius = paint.strokeWidth / 2;
169190
final borderRadius = (borderPaint?.strokeWidth ?? 0) / 2;
170191
if (polylineOpt.isDotted) {
171-
final spacing = polylineOpt.strokeWidth * 1.5;
192+
final spacing = strokeWidth * 1.5;
172193
if (saveLayers) canvas.saveLayer(rect, Paint());
173194
if (borderPaint != null && filterPaint != null) {
174195
_paintDottedLine(

0 commit comments

Comments
 (0)