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 panBuffer feature to include extended tilerange #1405

Merged
merged 3 commits into from
Dec 21, 2022
Merged
Changes from 1 commit
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
21 changes: 20 additions & 1 deletion lib/src/layer/tile_layer/tile_layer.dart
Original file line number Diff line number Diff line change
Expand Up @@ -138,6 +138,12 @@ class TileLayer extends StatefulWidget {
/// unloading them.
final int keepBuffer;

/// When panning the map, extend the tilerange by this many tiles in each
/// direction.
/// Will cause extra tile loads, and impact performance.
/// Be careful increasing this beyond 0 or 1.
final int panBuffer;

/// Tile image to show in place of the tile that failed to load.
final ImageProvider? errorImage;

Expand Down Expand Up @@ -254,6 +260,7 @@ class TileLayer extends StatefulWidget {
Map<String, String>? additionalOptions,
this.subdomains = const <String>[],
this.keepBuffer = 2,
this.panBuffer = 0,
this.backgroundColor = const Color(0xFFE0E0E0),
this.errorImage,
TileProvider? tileProvider,
Expand Down Expand Up @@ -584,7 +591,19 @@ class _TileLayerState extends State<TileLayer> with TickerProviderStateMixin {
center ??= map.center;

final pixelBounds = _getTiledPixelBounds(map, center);
final tileRange = _pxBoundsToTileRange(pixelBounds);
Bounds tileRange = _pxBoundsToTileRange(pixelBounds);

final panBuffer = widget.panBuffer;

// Increase the tilerange if we have panBuffer set, but make sure we
// don't use values outside valid tiles, eg (0,-1).
tileRange = tileRange.extend(CustomPoint(
math.max(_globalTileRange.min.x, tileRange.min.x - panBuffer),
math.max(_globalTileRange.min.y, tileRange.min.y - panBuffer)));
tileRange = tileRange.extend(CustomPoint(
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We could use a condition if (panBuffer > 0) to avoid unecessary computation on tileRange 🤔
Otherwise the code looks good to me

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, I think I like that, every little helps!

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ok, have added an if( panBuffer != 0), maybe it should be > 0, but maybe there is some weird reason for someone to reduce it as well that I can't think of (although maybe there could be bugs if they did that, but there is a bit of caveat emptor or whatever it is)!

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Was thinking about it too but I supposed that it wouldn't make sense to buffer -1 tiles 🤔

math.min(_globalTileRange.max.x, tileRange.max.x + panBuffer),
math.min(_globalTileRange.max.y, tileRange.max.y + panBuffer)));

final tileCenter = tileRange.center;
final queue = <Coords<double>>[];
final margin = widget.keepBuffer;
Expand Down