Skip to content
New issue

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

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

Already on GitHub? Sign in to your account

Add polygon label rotate #1332

Merged
merged 2 commits into from
Aug 17, 2022
Merged
Show file tree
Hide file tree
Changes from all 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
28 changes: 28 additions & 0 deletions example/lib/pages/polygon.dart
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,21 @@ class PolygonPage extends StatelessWidget {
LatLng(44.399, 1.76),
];

final labelPoints = <LatLng>[
LatLng(60.16, -9.38),
LatLng(60.16, -4.16),
LatLng(61.18, -4.16),
LatLng(61.18, -9.38),
];

final labelRotatedPoints = <LatLng>[
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),
Expand Down Expand Up @@ -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
),
]),
],
),
Expand Down
11 changes: 10 additions & 1 deletion lib/src/layer/label.dart
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,9 @@ class Label {
Canvas canvas,
List<Offset> points,
String? labelText,
TextStyle? labelStyle, {
TextStyle? labelStyle,
double rotationRad, {
bool rotate = false,
PolygonLabelPlacement labelPlacement = PolygonLabelPlacement.polylabel,
}) {
late Offset placementPoint;
Expand Down Expand Up @@ -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();
}
}
}
Expand Down
9 changes: 7 additions & 2 deletions lib/src/layer/polygon_layer.dart
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@ class Polygon {
final String? label;
final TextStyle labelStyle;
final PolygonLabelPlacement labelPlacement;
final bool rotateLabel;

Polygon({
required this.points,
Expand All @@ -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, (_) => []);
Expand Down Expand Up @@ -129,7 +131,7 @@ class PolygonLayer extends StatelessWidget {

polygons.add(
CustomPaint(
painter: PolygonPainter(polygon),
painter: PolygonPainter(polygon, map.rotationRad),
size: size,
),
);
Expand All @@ -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) {
Expand Down Expand Up @@ -277,6 +280,8 @@ class PolygonPainter extends CustomPainter {
polygonOpt.offsets,
polygonOpt.label,
polygonOpt.labelStyle,
rotationRad,
rotate: polygonOpt.rotateLabel,
labelPlacement: polygonOpt.labelPlacement,
);
}
Expand Down