From d65881ca9e216688b8725e16e327a56283c9467e Mon Sep 17 00:00:00 2001 From: Luka S Date: Fri, 8 Sep 2023 12:04:49 +0100 Subject: [PATCH] Changed `TileLayer`'s default (native) zoom levels (#1627) --- lib/src/layer/tile_layer/tile_layer.dart | 63 +++++++++++++----------- 1 file changed, 35 insertions(+), 28 deletions(-) diff --git a/lib/src/layer/tile_layer/tile_layer.dart b/lib/src/layer/tile_layer/tile_layer.dart index 2e2a3a47a..a3c8ad5bf 100644 --- a/lib/src/layer/tile_layer/tile_layer.dart +++ b/lib/src/layer/tile_layer/tile_layer.dart @@ -53,23 +53,40 @@ class TileLayer extends StatefulWidget { /// Default is 256 final double tileSize; - // The minimum zoom level down to which this layer will be - // displayed (inclusive). + /// The minimum zoom level down to which this layer will be displayed + /// (inclusive) + /// + /// This should usually be 0 (as default). final double minZoom; /// The maximum zoom level up to which this layer will be displayed - /// (inclusive). In most tile providers goes from 0 to 19. + /// (inclusive). + /// + /// Prefer [maxNativeZoom] for setting the maximum zoom level supported by the + /// tile source. The main usage for this is to display a different [TileLayer] + /// when zoomed far in. + /// + /// Otherwise, this should usually be infinite (as default), so that there are + /// tiles always displayed. final double maxZoom; - /// Minimum zoom number the tile source has available. If it is specified, the - /// tiles on all zoom levels lower than minNativeZoom will be loaded from - /// minNativeZoom level and auto-scaled. - final int? minNativeZoom; + /// Minimum zoom level supported by the tile source + /// + /// Tiles from below this zoom level will not be displayed, instead tiles at + /// this zoom level will be displayed and scaled. + /// + /// This should usually be 0 (as default), as most tile sources will support + /// zoom levels onwards from this. + final int minNativeZoom; - /// Maximum zoom number the tile source has available. If it is specified, the - /// tiles on all zoom levels higher than maxNativeZoom will be loaded from - /// maxNativeZoom level and auto-scaled. - final int? maxNativeZoom; + /// Maximum zoom number supported by the tile source has available. + /// + /// Tiles from above this zoom level will not be displayed, instead tiles at + /// this zoom level will be displayed and scaled. + /// + /// Most tile servers support up to zoom level 19, which is the default. + /// Otherwise, this should be specified. + final int maxNativeZoom; /// If set to true, the zoom number used in tile URLs will be reversed /// (`maxZoom - zoom` instead of `zoom`) @@ -227,11 +244,11 @@ class TileLayer extends StatefulWidget { super.key, this.urlTemplate, this.fallbackUrl, - double tileSize = 256.0, - double minZoom = 0.0, - double maxZoom = 18.0, - this.minNativeZoom, - this.maxNativeZoom, + double tileSize = 256, + double minZoom = 0, + double maxZoom = double.infinity, + this.minNativeZoom = 0, + this.maxNativeZoom = 19, this.zoomReverse = false, double zoomOffset = 0.0, this.additionalOptions = const {}, @@ -635,18 +652,8 @@ class _TileLayerState extends State with TickerProviderStateMixin { /// Rounds the zoom to the nearest int and clamps it to the native zoom limits /// if there are any. - int _clampToNativeZoom(double zoom) { - var result = zoom.round(); - - if (widget.minNativeZoom != null) { - result = max(result, widget.minNativeZoom!); - } - if (widget.maxNativeZoom != null) { - result = min(result, widget.maxNativeZoom!); - } - - return result; - } + int _clampToNativeZoom(double zoom) => + zoom.round().clamp(widget.minNativeZoom, widget.maxNativeZoom); void _onTileLoadError(TileImage tile, Object error, StackTrace? stackTrace) { debugPrint(error.toString());