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
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
14 changes: 14 additions & 0 deletions example/lib/pages/tile_builder_example.dart
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ class _TileBuilderPageState extends State<TileBuilderPage> {
bool loadingTime = false;
bool showCoords = false;
bool grid = false;
int panBuffer = 0;

// mix of [coordinateDebugTileBuilder] and [loadingTimeDebugTileBuilder] from tile_builder.dart
Widget tileBuilder(BuildContext context, Widget tileWidget, Tile tile) {
Expand Down Expand Up @@ -102,6 +103,18 @@ class _TileBuilderPageState extends State<TileBuilderPage> {
icon: Icon(darkMode ? Icons.brightness_high : Icons.brightness_2),
onPressed: () => setState(() => darkMode = !darkMode),
),
const SizedBox(height: 8),
FloatingActionButton.extended(
heroTag: 'panBuffer',
label: Text(
panBuffer == 0 ? 'panBuffer off' : 'panBuffer on',
textAlign: TextAlign.center,
),
icon: Icon(grid ? Icons.grid_off : Icons.grid_on),
onPressed: () => setState(() {
panBuffer = panBuffer == 0 ? 1 : 0;
}),
),
],
),
body: Padding(
Expand All @@ -118,6 +131,7 @@ class _TileBuilderPageState extends State<TileBuilderPage> {
tileBuilder: tileBuilder,
tilesContainerBuilder:
darkMode ? darkModeTilesContainerBuilder : null,
panBuffer: panBuffer,
),
MarkerLayer(
markers: <Marker>[
Expand Down
23 changes: 22 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,21 @@ 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).
if (panBuffer != 0) {
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(
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