Skip to content
New issue

Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? # to your account

Polyline stroke width in meters #1404

Merged
merged 3 commits into from
Dec 21, 2022
Merged
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
37 changes: 29 additions & 8 deletions lib/src/layer/polyline_layer.dart
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -19,6 +19,7 @@ class Polyline {
final bool isDotted;
final StrokeCap strokeCap;
final StrokeJoin strokeJoin;
final bool useStrokeWidthInMeter;
late LatLngBounds boundingBox;

Polyline({
Expand All @@ -33,6 +34,7 @@ class Polyline {
this.isDotted = false,
this.strokeCap = StrokeCap.round,
this.strokeJoin = StrokeJoin.round,
this.useStrokeWidthInMeter = false,
});
}

Expand Down Expand Up @@ -68,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);
Expand All @@ -87,7 +90,7 @@ class PolylineLayer extends StatelessWidget {
polylineWidgets.add(
CustomPaint(
key: polylineOpt.key,
painter: PolylinePainter(polylineOpt, saveLayers),
painter: PolylinePainter(polylineOpt, saveLayers, map),
size: size,
),
);
Expand Down Expand Up @@ -123,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;
Expand All @@ -150,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;
Expand All @@ -159,16 +181,15 @@ 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)
: null;
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(
Expand Down