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

Center label for polygons #800

Closed
wants to merge 1 commit into from
Closed
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
73 changes: 43 additions & 30 deletions lib/src/layer/polygon_layer.dart
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import 'dart:math';
import 'dart:ui';

import 'package:flutter/material.dart';
import 'package:flutter/widgets.dart';
import 'package:flutter_map/flutter_map.dart';
import 'package:flutter_map/src/map/map.dart';
Expand Down Expand Up @@ -35,6 +36,8 @@ class Polygon {
final Color borderColor;
final bool disableHolesBorder;
final bool isDotted;
final String label;
final TextStyle labelStyle;
LatLngBounds boundingBox;

Polygon({
Expand All @@ -45,9 +48,9 @@ class Polygon {
this.borderColor = const Color(0xFFFFFF00),
this.disableHolesBorder = false,
this.isDotted = false,
}) : holeOffsetsList = null == holePointsList || holePointsList.isEmpty
? null
: List.generate(holePointsList.length, (_) => []);
this.label,
this.labelStyle = const TextStyle(),
}) : holeOffsetsList = null == holePointsList || holePointsList.isEmpty ? null : List.generate(holePointsList.length, (_) => []);
}

class PolygonLayerWidget extends StatelessWidget {
Expand All @@ -66,8 +69,7 @@ class PolygonLayer extends StatelessWidget {
final MapState map;
final Stream stream;

PolygonLayer(this.polygonOpts, this.map, this.stream)
: super(key: polygonOpts.key);
PolygonLayer(this.polygonOpts, this.map, this.stream) : super(key: polygonOpts.key);

@override
Widget build(BuildContext context) {
Expand Down Expand Up @@ -95,8 +97,7 @@ class PolygonLayer extends StatelessWidget {
}
}

if (polygonOpts.polygonCulling &&
!polygon.boundingBox.isOverlapping(map.bounds)) {
if (polygonOpts.polygonCulling && !polygon.boundingBox.isOverlapping(map.bounds)) {
// skip this polygon as it's offscreen
continue;
}
Expand All @@ -105,11 +106,9 @@ class PolygonLayer extends StatelessWidget {

if (null != polygon.holePointsList) {
for (var i = 0, len = polygon.holePointsList.length; i < len; ++i) {
_fillOffsets(
polygon.holeOffsetsList[i], polygon.holePointsList[i]);
_fillOffsets(polygon.holeOffsetsList[i], polygon.holePointsList[i]);
}
}

polygons.add(
CustomPaint(
painter: PolygonPainter(polygon),
Expand All @@ -132,8 +131,7 @@ class PolygonLayer extends StatelessWidget {
var point = points[i];

var pos = map.project(point);
pos = pos.multiplyBy(map.getZoomScale(map.zoom, map.zoom)) -
map.getPixelOrigin();
pos = pos.multiplyBy(map.getZoomScale(map.zoom, map.zoom)) - map.getPixelOrigin();
offsets.add(Offset(pos.x.toDouble(), pos.y.toDouble()));
if (i > 0) {
offsets.add(Offset(pos.x.toDouble(), pos.y.toDouble()));
Expand Down Expand Up @@ -166,21 +164,17 @@ class PolygonPainter extends CustomPainter {

if (polygonOpt.isDotted) {
var spacing = polygonOpt.borderStrokeWidth * 1.5;
_paintDottedLine(
canvas, polygonOpt.offsets, borderRadius, spacing, borderPaint);
_paintDottedLine(canvas, polygonOpt.offsets, borderRadius, spacing, borderPaint);

if (!polygonOpt.disableHolesBorder &&
null != polygonOpt.holeOffsetsList) {
if (!polygonOpt.disableHolesBorder && null != polygonOpt.holeOffsetsList) {
for (var offsets in polygonOpt.holeOffsetsList) {
_paintDottedLine(
canvas, offsets, borderRadius, spacing, borderPaint);
_paintDottedLine(canvas, offsets, borderRadius, spacing, borderPaint);
}
}
} else {
_paintLine(canvas, polygonOpt.offsets, borderRadius, borderPaint);

if (!polygonOpt.disableHolesBorder &&
null != polygonOpt.holeOffsetsList) {
if (!polygonOpt.disableHolesBorder && null != polygonOpt.holeOffsetsList) {
for (var offsets in polygonOpt.holeOffsetsList) {
_paintLine(canvas, offsets, borderRadius, borderPaint);
}
Expand All @@ -189,8 +183,7 @@ class PolygonPainter extends CustomPainter {
}
}

void _paintDottedLine(Canvas canvas, List<Offset> offsets, double radius,
double stepLength, Paint paint) {
void _paintDottedLine(Canvas canvas, List<Offset> offsets, double radius, double stepLength, Paint paint) {
var startDistance = 0.0;
for (var i = 0; i < offsets.length - 1; i++) {
var o0 = offsets[i];
Expand All @@ -204,15 +197,12 @@ class PolygonPainter extends CustomPainter {
canvas.drawCircle(offset, radius, paint);
distance += stepLength;
}
startDistance = distance < totalDistance
? stepLength - (totalDistance - distance)
: distance - totalDistance;
startDistance = distance < totalDistance ? stepLength - (totalDistance - distance) : distance - totalDistance;
}
canvas.drawCircle(offsets.last, radius, paint);
}

void _paintLine(
Canvas canvas, List<Offset> offsets, double radius, Paint paint) {
void _paintLine(Canvas canvas, List<Offset> offsets, double radius, Paint paint) {
canvas.drawPoints(PointMode.lines, [...offsets, offsets[0]], paint);
for (var offset in offsets) {
canvas.drawCircle(offset, radius, paint);
Expand All @@ -239,9 +229,10 @@ class PolygonPainter extends CustomPainter {
var path = Path();
path.addPolygon(polygonOpt.offsets, true);
canvas.drawPath(path, paint);

if (polygonOpt.label != null) {
_paintText(canvas, polygonOpt.offsets);
}
_paintBorder(canvas);

canvas.restore();
} else {
canvas.clipRect(rect);
Expand All @@ -250,13 +241,35 @@ class PolygonPainter extends CustomPainter {
..color = polygonOpt.color;

var path = Path();

path.addPolygon(polygonOpt.offsets, true);
canvas.drawPath(path, paint);

if (polygonOpt.label != null) {
_paintText(canvas, polygonOpt.offsets);
}
_paintBorder(canvas);
}
}

void _paintText(Canvas canvas, List<Offset> points) {
var dx = points.map((e) => e.dx).toList().fold(0.0, (previousValue, element) => previousValue + element) / points.length;
var dy = points.map((e) => e.dy).toList().fold(0.0, (previousValue, element) => previousValue + element) / points.length;

final textSpan = TextSpan(text: polygonOpt.label, style: polygonOpt.labelStyle);
final textPainter = TextPainter(
text: textSpan,
textAlign: TextAlign.center,
textDirection: TextDirection.ltr,
);
textPainter.layout(minWidth: 0, maxWidth: dx);
dx = dx - textPainter.width / 2;

textPainter.paint(
canvas,
Offset(dx, dy),
);
}

@override
bool shouldRepaint(PolygonPainter other) => false;

Expand Down