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

Creating centered label for polygons #1220

Merged
merged 8 commits into from
Apr 21, 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
45 changes: 45 additions & 0 deletions lib/src/layer/label.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
import 'dart:math';

import 'package:flutter/material.dart';

class Label {
static void paintText(
Canvas canvas,
List<Offset> points,
String? labelText,
TextStyle? labelStyle,
) {
var maxDx = 0.0;
var minDx = double.infinity;
var dx = points.map((e) => e.dx).toList().fold<double>(0.0,
(previousValue, element) {
maxDx = max(maxDx, element);
minDx = min(minDx, element);
return previousValue + element;
}) /
points.length;
var dy = points.map((e) => e.dy).toList().fold<double>(
0.0, (previousValue, element) => previousValue + element) /
points.length;

final textSpan = TextSpan(text: labelText, style: labelStyle);
final textPainter = TextPainter(
text: textSpan,
textAlign: TextAlign.center,
textDirection: TextDirection.ltr,
maxLines: 1,
);
if (dx > 0) {
textPainter.layout(minWidth: 0, maxWidth: double.infinity);
dx = dx - textPainter.width / 2;
dy = dy - textPainter.height / 2;

if (maxDx - minDx > textPainter.width) {
textPainter.paint(
canvas,
Offset(dx, dy),
);
}
}
}
}
14 changes: 14 additions & 0 deletions lib/src/layer/polygon_layer.dart
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import 'dart:ui';

import 'package:flutter/widgets.dart';
import 'package:flutter_map/flutter_map.dart';
import 'package:flutter_map/src/layer/label.dart';
import 'package:flutter_map/src/map/map.dart';
import 'package:latlong2/latlong.dart' hide Path; // conflict with Path from UI

Expand Down Expand Up @@ -37,6 +38,8 @@ class Polygon {
final bool isDotted;
final bool isFilled;
late final LatLngBounds boundingBox;
final String? label;
final TextStyle labelStyle;

Polygon({
required this.points,
Expand All @@ -47,6 +50,8 @@ class Polygon {
this.disableHolesBorder = false,
this.isDotted = false,
this.isFilled = false,
this.label,
this.labelStyle = const TextStyle(),
}) : holeOffsetsList = null == holePointsList || holePointsList.isEmpty
? null
: List.generate(holePointsList.length, (_) => []);
Expand Down Expand Up @@ -258,6 +263,15 @@ class PolygonPainter extends CustomPainter {
canvas.drawPath(path, paint);

_paintBorder(canvas);

if (polygonOpt.label != null) {
Label.paintText(
canvas,
polygonOpt.offsets,
polygonOpt.label,
polygonOpt.labelStyle,
);
}
}
}

Expand Down