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

Improve Marker.anchor performance #1558

Merged
merged 1 commit into from
Jun 19, 2023
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
5 changes: 2 additions & 3 deletions example/lib/pages/markers.dart
Original file line number Diff line number Diff line change
Expand Up @@ -129,7 +129,7 @@ class MarkerPageState extends State<MarkerPage> {
47.18664724067855, -1.5436768515939427),
width: 64,
height: 64,
anchorPos: AnchorPos.align(AnchorAlign.left),
anchorPos: const AnchorPos.align(AnchorAlign.left),
builder: (context) => const ColoredBox(
color: Colors.lightBlue,
child: Align(
Expand All @@ -143,7 +143,7 @@ class MarkerPageState extends State<MarkerPage> {
47.18664724067855, -1.5436768515939427),
width: 64,
height: 64,
anchorPos: AnchorPos.align(AnchorAlign.right),
anchorPos: const AnchorPos.align(AnchorAlign.right),
builder: (context) => const ColoredBox(
color: Colors.pink,
child: Align(
Expand All @@ -156,7 +156,6 @@ class MarkerPageState extends State<MarkerPage> {
point: const LatLng(
47.18664724067855, -1.5436768515939427),
rotate: false,
anchorPos: AnchorPos.align(AnchorAlign.center),
builder: (context) =>
const ColoredBox(color: Colors.black),
),
Expand Down
30 changes: 17 additions & 13 deletions lib/src/layer/marker_layer.dart
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import 'package:flutter/widgets.dart';
import 'package:flutter_map/flutter_map.dart';
import 'package:flutter_map/src/misc/private/bounds.dart';
import 'package:flutter_map/src/map/state.dart';
import 'package:flutter_map/src/misc/private/bounds.dart';
import 'package:latlong2/latlong.dart';

/// Defines the positioning of a [Marker.builder] widget relative to the center
Expand All @@ -10,11 +10,13 @@ import 'package:latlong2/latlong.dart';
/// Can be defined exactly (using [AnchorPos.exactly] with an [Anchor]) or in
/// a relative alignment (using [AnchorPos.align] with an [AnchorAlign]).
class AnchorPos {
static const defaultAnchorPos = AnchorPos.align(AnchorAlign.center);

final Anchor? anchor;
final AnchorAlign? alignment;

AnchorPos.exactly(Anchor this.anchor) : alignment = null;
AnchorPos.align(AnchorAlign this.alignment) : anchor = null;
const AnchorPos.exactly(Anchor this.anchor) : alignment = null;
const AnchorPos.align(AnchorAlign this.alignment) : anchor = null;
}

/// Exact alignment for a [Marker.builder] widget relative to the center
Expand All @@ -26,7 +28,7 @@ class Anchor {
final double left;
final double top;

Anchor(this.left, this.top);
const Anchor(this.left, this.top);

factory Anchor.fromPos(AnchorPos pos, double width, double height) {
if (pos.anchor case final anchor?) return anchor;
Expand Down Expand Up @@ -97,8 +99,8 @@ class Marker {
final double height;

/// Positioning of the [builder] widget relative to the center of its bounding
/// box defined by its [height] & [width]
final AnchorPos? anchorPos;
/// box.
final Anchor? anchor;

/// Whether to counter rotate markers to the map's rotation, to keep a fixed
/// orientation
Expand Down Expand Up @@ -131,11 +133,12 @@ class Marker {
required this.builder,
this.width = 30.0,
this.height = 30.0,
this.anchorPos,
AnchorPos? anchorPos,
this.rotate,
this.rotateOrigin,
this.rotateAlignment,
});
}) : anchor =
anchorPos == null ? null : Anchor.fromPos(anchorPos, width, height);
}

class MarkerLayer extends StatelessWidget {
Expand Down Expand Up @@ -200,11 +203,12 @@ class MarkerLayer extends StatelessWidget {
// This calculation works for any Anchor position whithin the Marker
// Note that Anchor coordinates of (0,0) are at bottom-right of the Marker
// unlike the map coordinates.
final anchor = Anchor.fromPos(
marker.anchorPos ?? anchorPos ?? AnchorPos.align(AnchorAlign.center),
marker.width,
marker.height,
);
final anchor = marker.anchor ??
Anchor.fromPos(
anchorPos ?? AnchorPos.defaultAnchorPos,
marker.width,
marker.height,
);
final rightPortion = marker.width - anchor.left;
final leftPortion = anchor.left;
final bottomPortion = marker.height - anchor.top;
Expand Down