From 64f48f8451c83b32dceefb86693b785e43f7e74e Mon Sep 17 00:00:00 2001
From: Leon <leon_lts@hotmail.com>
Date: Wed, 9 Nov 2022 09:36:15 -0300
Subject: [PATCH 1/3] feat: polyline stroke width in meter

---
 lib/src/layer/polyline_layer.dart | 15 ++++++++++++++-
 1 file changed, 14 insertions(+), 1 deletion(-)

diff --git a/lib/src/layer/polyline_layer.dart b/lib/src/layer/polyline_layer.dart
index acf1231fa..f88ec700b 100644
--- a/lib/src/layer/polyline_layer.dart
+++ b/lib/src/layer/polyline_layer.dart
@@ -10,7 +10,7 @@ class Polyline {
   final Key? key;
   final List<LatLng> points;
   final List<Offset> offsets = [];
-  final double strokeWidth;
+  double strokeWidth;
   final Color color;
   final double borderStrokeWidth;
   final Color? borderColor;
@@ -19,6 +19,7 @@ class Polyline {
   final bool isDotted;
   final StrokeCap strokeCap;
   final StrokeJoin strokeJoin;
+  final bool useStrokeWidthInMeter;
   late LatLngBounds boundingBox;
 
   Polyline({
@@ -33,6 +34,7 @@ class Polyline {
     this.isDotted = false,
     this.strokeCap = StrokeCap.round,
     this.strokeJoin = StrokeJoin.round,
+    this.useStrokeWidthInMeter = false,
   });
 }
 
@@ -84,6 +86,17 @@ class PolylineLayer extends StatelessWidget {
 
           _fillOffsets(polylineOpt.offsets, polylineOpt.points, map);
 
+          if (polylineOpt.useStrokeWidthInMeter) {
+            final r = const Distance().offset(
+              polylineOpt.points.first,
+              polylineOpt.strokeWidth,
+              180,
+            );
+            final delta =
+                polylineOpt.offsets.first - map.getOffsetFromOrigin(r);
+            polylineOpt.strokeWidth = delta.distance;
+          }
+
           polylineWidgets.add(
             CustomPaint(
               key: polylineOpt.key,

From a735b689dda49bc1ad8aaf41d914ce1d9ab25e8e Mon Sep 17 00:00:00 2001
From: Leon <leon_lts@hotmail.com>
Date: Wed, 9 Nov 2022 11:53:25 -0300
Subject: [PATCH 2/3] fix: polyline stroke width in meter errors not keeping
 the size after move map camera

---
 lib/src/layer/polyline_layer.dart | 44 ++++++++++++++++++-------------
 1 file changed, 26 insertions(+), 18 deletions(-)

diff --git a/lib/src/layer/polyline_layer.dart b/lib/src/layer/polyline_layer.dart
index f88ec700b..ed6ee67b0 100644
--- a/lib/src/layer/polyline_layer.dart
+++ b/lib/src/layer/polyline_layer.dart
@@ -70,6 +70,7 @@ class PolylineLayer extends StatelessWidget {
   @override
   Widget build(BuildContext context) {
     final map = FlutterMapState.maybeOf(context)!;
+
     return LayoutBuilder(
       builder: (BuildContext context, BoxConstraints bc) {
         final size = Size(bc.maxWidth, bc.maxHeight);
@@ -86,21 +87,10 @@ class PolylineLayer extends StatelessWidget {
 
           _fillOffsets(polylineOpt.offsets, polylineOpt.points, map);
 
-          if (polylineOpt.useStrokeWidthInMeter) {
-            final r = const Distance().offset(
-              polylineOpt.points.first,
-              polylineOpt.strokeWidth,
-              180,
-            );
-            final delta =
-                polylineOpt.offsets.first - map.getOffsetFromOrigin(r);
-            polylineOpt.strokeWidth = delta.distance;
-          }
-
           polylineWidgets.add(
             CustomPaint(
               key: polylineOpt.key,
-              painter: PolylinePainter(polylineOpt, saveLayers),
+              painter: PolylinePainter(polylineOpt, saveLayers, map),
               size: size,
             ),
           );
@@ -136,17 +126,36 @@ class PolylinePainter extends CustomPainter {
   /// {@endtemplate}
   final bool saveLayers;
 
-  PolylinePainter(this.polylineOpt, this.saveLayers);
+  final FlutterMapState map;
+
+  PolylinePainter(this.polylineOpt, this.saveLayers, this.map);
 
   @override
   void paint(Canvas canvas, Size size) {
     if (polylineOpt.offsets.isEmpty) {
       return;
     }
+
     final rect = Offset.zero & size;
     canvas.clipRect(rect);
+
+    late final double strokeWidth;
+    if (polylineOpt.useStrokeWidthInMeter) {
+      final firstPoint = polylineOpt.points.first;
+      final firstOffset = polylineOpt.offsets.first;
+      final r = const Distance().offset(
+        firstPoint,
+        polylineOpt.strokeWidth,
+        180,
+      );
+      final delta = firstOffset - map.getOffsetFromOrigin(r);
+
+      strokeWidth = delta.distance;
+    } else {
+      strokeWidth = polylineOpt.strokeWidth;
+    }
     final paint = Paint()
-      ..strokeWidth = polylineOpt.strokeWidth
+      ..strokeWidth = strokeWidth
       ..strokeCap = polylineOpt.strokeCap
       ..strokeJoin = polylineOpt.strokeJoin
       ..blendMode = BlendMode.srcOver;
@@ -163,7 +172,7 @@ class PolylinePainter extends CustomPainter {
     if (polylineOpt.borderColor != null) {
       filterPaint = Paint()
         ..color = polylineOpt.borderColor!.withAlpha(255)
-        ..strokeWidth = polylineOpt.strokeWidth
+        ..strokeWidth = strokeWidth
         ..strokeCap = polylineOpt.strokeCap
         ..strokeJoin = polylineOpt.strokeJoin
         ..blendMode = BlendMode.dstOut;
@@ -172,8 +181,7 @@ class PolylinePainter extends CustomPainter {
     final borderPaint = polylineOpt.borderStrokeWidth > 0.0
         ? (Paint()
           ..color = polylineOpt.borderColor ?? const Color(0x00000000)
-          ..strokeWidth =
-              polylineOpt.strokeWidth + polylineOpt.borderStrokeWidth
+          ..strokeWidth = strokeWidth + polylineOpt.borderStrokeWidth
           ..strokeCap = polylineOpt.strokeCap
           ..strokeJoin = polylineOpt.strokeJoin
           ..blendMode = BlendMode.srcOver)
@@ -181,7 +189,7 @@ class PolylinePainter extends CustomPainter {
     final radius = paint.strokeWidth / 2;
     final borderRadius = (borderPaint?.strokeWidth ?? 0) / 2;
     if (polylineOpt.isDotted) {
-      final spacing = polylineOpt.strokeWidth * 1.5;
+      final spacing = strokeWidth * 1.5;
       if (saveLayers) canvas.saveLayer(rect, Paint());
       if (borderPaint != null && filterPaint != null) {
         _paintDottedLine(

From 9c7bb6adab59a489876556ef4785e632c89a947c Mon Sep 17 00:00:00 2001
From: Luka S <github@jaffaketchup.dev>
Date: Wed, 21 Dec 2022 12:29:47 +0000
Subject: [PATCH 3/3] Update lib/src/layer/polyline_layer.dart

---
 lib/src/layer/polyline_layer.dart | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/lib/src/layer/polyline_layer.dart b/lib/src/layer/polyline_layer.dart
index ed6ee67b0..ad5df0407 100644
--- a/lib/src/layer/polyline_layer.dart
+++ b/lib/src/layer/polyline_layer.dart
@@ -10,7 +10,7 @@ class Polyline {
   final Key? key;
   final List<LatLng> points;
   final List<Offset> offsets = [];
-  double strokeWidth;
+  final double strokeWidth;
   final Color color;
   final double borderStrokeWidth;
   final Color? borderColor;