From f24963c80520a5597adbfd3359413c202a5c599b Mon Sep 17 00:00:00 2001 From: Jefferey Petersen Date: Tue, 2 Aug 2022 14:23:52 -0700 Subject: [PATCH 1/2] Add ability to set polygon labels to rotate Rotation will keep the polygon labels facing the top of the display similar to rotation of a marker. Rotation is about the center of the text. --- lib/src/layer/label.dart | 11 ++++++++++- lib/src/layer/polygon_layer.dart | 9 +++++++-- 2 files changed, 17 insertions(+), 3 deletions(-) diff --git a/lib/src/layer/label.dart b/lib/src/layer/label.dart index b7406a869..31d951c05 100644 --- a/lib/src/layer/label.dart +++ b/lib/src/layer/label.dart @@ -10,7 +10,9 @@ class Label { Canvas canvas, List points, String? labelText, - TextStyle? labelStyle, { + TextStyle? labelStyle, + double rotationRad, { + bool rotate = false, PolygonLabelPlacement labelPlacement = PolygonLabelPlacement.polylabel, }) { late Offset placementPoint; @@ -46,10 +48,17 @@ class Label { } if (maxDx - minDx > textPainter.width) { + canvas.save(); + if (rotate) { + canvas.translate(placementPoint.dx, placementPoint.dy); + canvas.rotate(-rotationRad); + canvas.translate(-placementPoint.dx, -placementPoint.dy); + } textPainter.paint( canvas, Offset(dx, dy), ); + canvas.restore(); } } } diff --git a/lib/src/layer/polygon_layer.dart b/lib/src/layer/polygon_layer.dart index daaccb812..59e592d21 100644 --- a/lib/src/layer/polygon_layer.dart +++ b/lib/src/layer/polygon_layer.dart @@ -47,6 +47,7 @@ class Polygon { final String? label; final TextStyle labelStyle; final PolygonLabelPlacement labelPlacement; + final bool rotateLabel; Polygon({ required this.points, @@ -62,6 +63,7 @@ class Polygon { this.label, this.labelStyle = const TextStyle(), this.labelPlacement = PolygonLabelPlacement.centroid, + this.rotateLabel = false, }) : holeOffsetsList = null == holePointsList || holePointsList.isEmpty ? null : List.generate(holePointsList.length, (_) => []); @@ -129,7 +131,7 @@ class PolygonLayer extends StatelessWidget { polygons.add( CustomPaint( - painter: PolygonPainter(polygon), + painter: PolygonPainter(polygon, map.rotationRad), size: size, ), ); @@ -154,8 +156,9 @@ class PolygonLayer extends StatelessWidget { class PolygonPainter extends CustomPainter { final Polygon polygonOpt; + final double rotationRad; - PolygonPainter(this.polygonOpt); + PolygonPainter(this.polygonOpt, this.rotationRad); @override void paint(Canvas canvas, Size size) { @@ -277,6 +280,8 @@ class PolygonPainter extends CustomPainter { polygonOpt.offsets, polygonOpt.label, polygonOpt.labelStyle, + rotationRad, + rotate: polygonOpt.rotateLabel, labelPlacement: polygonOpt.labelPlacement, ); } From 587f01c815f41a68d67ab544e80c13ac9cc608fd Mon Sep 17 00:00:00 2001 From: Jefferey Petersen Date: Tue, 2 Aug 2022 14:46:01 -0700 Subject: [PATCH 2/2] Add examples with labels --- example/lib/pages/polygon.dart | 28 ++++++++++++++++++++++++++++ 1 file changed, 28 insertions(+) diff --git a/example/lib/pages/polygon.dart b/example/lib/pages/polygon.dart index 35e28f351..7ced55f2c 100644 --- a/example/lib/pages/polygon.dart +++ b/example/lib/pages/polygon.dart @@ -35,6 +35,21 @@ class PolygonPage extends StatelessWidget { LatLng(44.399, 1.76), ]; + final labelPoints = [ + LatLng(60.16, -9.38), + LatLng(60.16, -4.16), + LatLng(61.18, -4.16), + LatLng(61.18, -9.38), + ]; + + final labelRotatedPoints = [ + LatLng(58.21, -10.28), + LatLng(58.21, -7.01), + LatLng(59.77, -7.01), + LatLng(59.77, -10.28), + ]; + + return Scaffold( appBar: AppBar(title: const Text('Polygons')), drawer: buildDrawer(context, PolygonPage.route), @@ -88,6 +103,19 @@ class PolygonPage extends StatelessWidget { borderColor: Colors.lightBlue, color: Colors.lightBlue, ), + Polygon( + points: labelPoints, + borderStrokeWidth: 4, + borderColor: Colors.purple, + label: "Label!", + ), + Polygon( + points: labelRotatedPoints, + borderStrokeWidth: 4, + borderColor: Colors.purple, + label: "Rotated!", + rotateLabel: true + ), ]), ], ),