From 3234219e2b5d7941bc1886ec57405bb64c108a92 Mon Sep 17 00:00:00 2001 From: JaffaKetchup Date: Mon, 5 Aug 2024 18:10:34 +0200 Subject: [PATCH 1/4] Changed type of `tileSize` to integer throughout Minor documentation improvements --- .../layer/tile_layer/tile_bounds/tile_bounds.dart | 6 +++--- lib/src/layer/tile_layer/tile_layer.dart | 15 +++++++++------ .../tile_provider/base_tile_provider.dart | 7 ++++--- lib/src/layer/tile_layer/tile_range.dart | 2 +- .../layer/tile_layer/tile_range_calculator.dart | 2 +- .../layer/tile_layer/tile_scale_calculator.dart | 4 ++-- 6 files changed, 20 insertions(+), 16 deletions(-) diff --git a/lib/src/layer/tile_layer/tile_bounds/tile_bounds.dart b/lib/src/layer/tile_layer/tile_bounds/tile_bounds.dart index 819191778..238b70a52 100644 --- a/lib/src/layer/tile_layer/tile_bounds/tile_bounds.dart +++ b/lib/src/layer/tile_layer/tile_bounds/tile_bounds.dart @@ -9,7 +9,7 @@ import 'package:meta/meta.dart'; abstract class TileBounds { /// Reference to the coordinate reference system. final Crs crs; - final double _tileSize; + final int _tileSize; final LatLngBounds? _latLngBounds; /// Constructor that creates an instance of a subclass of [TileBounds]: @@ -18,7 +18,7 @@ abstract class TileBounds { /// [WrappedTileBounds] if the CRS is wrapped. factory TileBounds({ required Crs crs, - required double tileSize, + required int tileSize, LatLngBounds? latLngBounds, }) { if (crs.infinite && latLngBounds == null) { @@ -43,7 +43,7 @@ abstract class TileBounds { /// parameters. bool shouldReplace( Crs crs, - double tileSize, + int tileSize, LatLngBounds? latLngBounds, ) => crs != this.crs || tileSize != _tileSize || latLngBounds != _latLngBounds; diff --git a/lib/src/layer/tile_layer/tile_layer.dart b/lib/src/layer/tile_layer/tile_layer.dart index c0b8e19e2..fab4a29e1 100644 --- a/lib/src/layer/tile_layer/tile_layer.dart +++ b/lib/src/layer/tile_layer/tile_layer.dart @@ -56,9 +56,13 @@ class TileLayer extends StatefulWidget { /// If not `null`, then tiles will pull's WMS protocol requests final WMSTileLayerOptions? wmsOptions; - /// Size for the tile. - /// Default is 256 - late final double tileSize; + /// Size in pixels of each tile image + /// + /// Should be a positive power of 2. + /// + /// If increasing past 256(px) (default), adjust [zoomOffset] as necessary, + /// for example 512px: -1. + late final int tileSize; /// The minimum zoom level down to which this layer will be displayed /// (inclusive) @@ -216,7 +220,7 @@ class TileLayer extends StatefulWidget { super.key, this.urlTemplate, this.fallbackUrl, - double tileSize = 256, + int tileSize = 256, double minZoom = 0, double maxZoom = double.infinity, int minNativeZoom = 0, @@ -319,8 +323,7 @@ class TileLayer extends StatefulWidget { this.zoomOffset = useSimulatedRetina ? (zoomReverse ? zoomOffset - 1.0 : zoomOffset + 1.0) : zoomOffset; - this.tileSize = - useSimulatedRetina ? (tileSize / 2.0).floorToDouble() : tileSize; + this.tileSize = useSimulatedRetina ? (tileSize / 2).floor() : tileSize; } @override diff --git a/lib/src/layer/tile_layer/tile_provider/base_tile_provider.dart b/lib/src/layer/tile_layer/tile_provider/base_tile_provider.dart index 954684aa5..f335cb00e 100644 --- a/lib/src/layer/tile_layer/tile_provider/base_tile_provider.dart +++ b/lib/src/layer/tile_layer/tile_provider/base_tile_provider.dart @@ -213,8 +213,9 @@ abstract class TileProvider { /// When creating a specialized [TileProvider], prefer overriding URL /// generation related methods in the following order: /// - /// 1. [populateTemplatePlaceholders] - /// 2. [generateReplacementMap] + /// 1. [TileLayer.additionalOptions] (or [generateReplacementMap] for more + /// advanced usage) + /// 2. [populateTemplatePlaceholders] /// 3. [getTileUrl] and/or [getTileFallbackUrl] /// /// Note to implementors: it is not safe to assume that at least one of @@ -224,7 +225,7 @@ abstract class TileProvider { populateTemplatePlaceholders( options.wmsOptions?.getUrl( coordinates, - options.tileSize.toInt(), + options.tileSize, options.resolvedRetinaMode == RetinaMode.simulation, ) ?? options.urlTemplate ?? diff --git a/lib/src/layer/tile_layer/tile_range.dart b/lib/src/layer/tile_layer/tile_range.dart index e39200be1..186d5b54a 100644 --- a/lib/src/layer/tile_layer/tile_range.dart +++ b/lib/src/layer/tile_layer/tile_range.dart @@ -41,7 +41,7 @@ class DiscreteTileRange extends TileRange { /// Calculate a [DiscreteTileRange] by using the pixel bounds. factory DiscreteTileRange.fromPixelBounds({ required int zoom, - required double tileSize, + required int tileSize, required Bounds pixelBounds, }) { final Bounds bounds; diff --git a/lib/src/layer/tile_layer/tile_range_calculator.dart b/lib/src/layer/tile_layer/tile_range_calculator.dart index 87c3e9096..470434086 100644 --- a/lib/src/layer/tile_layer/tile_range_calculator.dart +++ b/lib/src/layer/tile_layer/tile_range_calculator.dart @@ -7,7 +7,7 @@ import 'package:meta/meta.dart'; @immutable class TileRangeCalculator { /// The tile size in pixels. - final double tileSize; + final int tileSize; /// Create a new [TileRangeCalculator] instance. const TileRangeCalculator({required this.tileSize}); diff --git a/lib/src/layer/tile_layer/tile_scale_calculator.dart b/lib/src/layer/tile_layer/tile_scale_calculator.dart index 01df1d3ef..f4136ff25 100644 --- a/lib/src/layer/tile_layer/tile_scale_calculator.dart +++ b/lib/src/layer/tile_layer/tile_scale_calculator.dart @@ -6,7 +6,7 @@ class TileScaleCalculator { final Crs crs; /// The size in pixel of tiles. - final double tileSize; + final int tileSize; double? _cachedCurrentZoom; final Map _cache = {}; @@ -18,7 +18,7 @@ class TileScaleCalculator { }); /// Returns true to indicate that the TileSizeCache should get replaced. - bool shouldReplace(Crs crs, double tileSize) => + bool shouldReplace(Crs crs, int tileSize) => this.crs != crs || this.tileSize != tileSize; /// Clears the cache if the zoom level does not match the current cached one From 07431a4256712ba9cf2e69ee2ba8a38f31b22f74 Mon Sep 17 00:00:00 2001 From: JaffaKetchup Date: Sun, 17 Nov 2024 21:01:24 +0000 Subject: [PATCH 2/4] Revert changes --- .../layer/tile_layer/tile_bounds/tile_bounds.dart | 6 +++--- lib/src/layer/tile_layer/tile_layer.dart | 15 ++++++--------- .../tile_provider/base_tile_provider.dart | 7 +++---- lib/src/layer/tile_layer/tile_range.dart | 2 +- .../layer/tile_layer/tile_range_calculator.dart | 2 +- .../layer/tile_layer/tile_scale_calculator.dart | 4 ++-- 6 files changed, 16 insertions(+), 20 deletions(-) diff --git a/lib/src/layer/tile_layer/tile_bounds/tile_bounds.dart b/lib/src/layer/tile_layer/tile_bounds/tile_bounds.dart index 238b70a52..819191778 100644 --- a/lib/src/layer/tile_layer/tile_bounds/tile_bounds.dart +++ b/lib/src/layer/tile_layer/tile_bounds/tile_bounds.dart @@ -9,7 +9,7 @@ import 'package:meta/meta.dart'; abstract class TileBounds { /// Reference to the coordinate reference system. final Crs crs; - final int _tileSize; + final double _tileSize; final LatLngBounds? _latLngBounds; /// Constructor that creates an instance of a subclass of [TileBounds]: @@ -18,7 +18,7 @@ abstract class TileBounds { /// [WrappedTileBounds] if the CRS is wrapped. factory TileBounds({ required Crs crs, - required int tileSize, + required double tileSize, LatLngBounds? latLngBounds, }) { if (crs.infinite && latLngBounds == null) { @@ -43,7 +43,7 @@ abstract class TileBounds { /// parameters. bool shouldReplace( Crs crs, - int tileSize, + double tileSize, LatLngBounds? latLngBounds, ) => crs != this.crs || tileSize != _tileSize || latLngBounds != _latLngBounds; diff --git a/lib/src/layer/tile_layer/tile_layer.dart b/lib/src/layer/tile_layer/tile_layer.dart index c81581d07..b6c9f538c 100644 --- a/lib/src/layer/tile_layer/tile_layer.dart +++ b/lib/src/layer/tile_layer/tile_layer.dart @@ -56,13 +56,9 @@ class TileLayer extends StatefulWidget { /// If not `null`, then tiles will pull's WMS protocol requests final WMSTileLayerOptions? wmsOptions; - /// Size in pixels of each tile image - /// - /// Should be a positive power of 2. - /// - /// If increasing past 256(px) (default), adjust [zoomOffset] as necessary, - /// for example 512px: -1. - late final int tileSize; + /// Size for the tile. + /// Default is 256 + late final double tileSize; /// The minimum zoom level down to which this layer will be displayed /// (inclusive) @@ -220,7 +216,7 @@ class TileLayer extends StatefulWidget { super.key, this.urlTemplate, this.fallbackUrl, - int tileSize = 256, + double tileSize = 256, double minZoom = 0, double maxZoom = double.infinity, int minNativeZoom = 0, @@ -323,7 +319,8 @@ class TileLayer extends StatefulWidget { this.zoomOffset = useSimulatedRetina ? (zoomReverse ? zoomOffset - 1.0 : zoomOffset + 1.0) : zoomOffset; - this.tileSize = useSimulatedRetina ? (tileSize / 2).floor() : tileSize; + this.tileSize = + useSimulatedRetina ? (tileSize / 2.0).floorToDouble() : tileSize; } @override diff --git a/lib/src/layer/tile_layer/tile_provider/base_tile_provider.dart b/lib/src/layer/tile_layer/tile_provider/base_tile_provider.dart index f335cb00e..954684aa5 100644 --- a/lib/src/layer/tile_layer/tile_provider/base_tile_provider.dart +++ b/lib/src/layer/tile_layer/tile_provider/base_tile_provider.dart @@ -213,9 +213,8 @@ abstract class TileProvider { /// When creating a specialized [TileProvider], prefer overriding URL /// generation related methods in the following order: /// - /// 1. [TileLayer.additionalOptions] (or [generateReplacementMap] for more - /// advanced usage) - /// 2. [populateTemplatePlaceholders] + /// 1. [populateTemplatePlaceholders] + /// 2. [generateReplacementMap] /// 3. [getTileUrl] and/or [getTileFallbackUrl] /// /// Note to implementors: it is not safe to assume that at least one of @@ -225,7 +224,7 @@ abstract class TileProvider { populateTemplatePlaceholders( options.wmsOptions?.getUrl( coordinates, - options.tileSize, + options.tileSize.toInt(), options.resolvedRetinaMode == RetinaMode.simulation, ) ?? options.urlTemplate ?? diff --git a/lib/src/layer/tile_layer/tile_range.dart b/lib/src/layer/tile_layer/tile_range.dart index 1976df857..0f4c889bf 100644 --- a/lib/src/layer/tile_layer/tile_range.dart +++ b/lib/src/layer/tile_layer/tile_range.dart @@ -41,7 +41,7 @@ class DiscreteTileRange extends TileRange { /// Calculate a [DiscreteTileRange] by using the pixel bounds. factory DiscreteTileRange.fromPixelBounds({ required int zoom, - required int tileSize, + required double tileSize, required Bounds pixelBounds, }) { final Bounds bounds; diff --git a/lib/src/layer/tile_layer/tile_range_calculator.dart b/lib/src/layer/tile_layer/tile_range_calculator.dart index 470434086..87c3e9096 100644 --- a/lib/src/layer/tile_layer/tile_range_calculator.dart +++ b/lib/src/layer/tile_layer/tile_range_calculator.dart @@ -7,7 +7,7 @@ import 'package:meta/meta.dart'; @immutable class TileRangeCalculator { /// The tile size in pixels. - final int tileSize; + final double tileSize; /// Create a new [TileRangeCalculator] instance. const TileRangeCalculator({required this.tileSize}); diff --git a/lib/src/layer/tile_layer/tile_scale_calculator.dart b/lib/src/layer/tile_layer/tile_scale_calculator.dart index f4136ff25..01df1d3ef 100644 --- a/lib/src/layer/tile_layer/tile_scale_calculator.dart +++ b/lib/src/layer/tile_layer/tile_scale_calculator.dart @@ -6,7 +6,7 @@ class TileScaleCalculator { final Crs crs; /// The size in pixel of tiles. - final int tileSize; + final double tileSize; double? _cachedCurrentZoom; final Map _cache = {}; @@ -18,7 +18,7 @@ class TileScaleCalculator { }); /// Returns true to indicate that the TileSizeCache should get replaced. - bool shouldReplace(Crs crs, int tileSize) => + bool shouldReplace(Crs crs, double tileSize) => this.crs != crs || this.tileSize != tileSize; /// Clears the cache if the zoom level does not match the current cached one From 99fa71c505524044c589920c773eda99c70e2a5a Mon Sep 17 00:00:00 2001 From: JaffaKetchup Date: Sun, 17 Nov 2024 21:34:28 +0000 Subject: [PATCH 3/4] Added `TileLayer.tileDimension` Deprecated `TileLayer.tileSize` & made nullable Reflected new name throughout internals and semi-internals --- lib/src/layer/tile_layer/retina_mode.dart | 2 +- lib/src/layer/tile_layer/tile.dart | 12 ++-- .../tile_layer/tile_bounds/tile_bounds.dart | 52 ++++++++------- lib/src/layer/tile_layer/tile_layer.dart | 65 ++++++++++++++----- .../tile_provider/base_tile_provider.dart | 11 +++- lib/src/layer/tile_layer/tile_range.dart | 8 +-- .../tile_layer/tile_range_calculator.dart | 6 +- .../tile_layer/tile_scale_calculator.dart | 26 ++++---- .../tile_layer/wms_tile_layer_options.dart | 10 +-- .../tile_bounds/tile_bounds_at_zoom_test.dart | 2 +- .../tile_bounds/tile_bounds_test.dart | 16 ++--- test/layer/tile_layer/tile_range_test.dart | 36 +++++----- 12 files changed, 143 insertions(+), 103 deletions(-) diff --git a/lib/src/layer/tile_layer/retina_mode.dart b/lib/src/layer/tile_layer/retina_mode.dart index f728ea780..16424588b 100644 --- a/lib/src/layer/tile_layer/retina_mode.dart +++ b/lib/src/layer/tile_layer/retina_mode.dart @@ -30,7 +30,7 @@ part of 'tile_layer.dart'; /// /// --- /// -/// Caution is advised when mixing retina mode with different `tileSize`s, +/// Caution is advised when mixing retina mode with different tile dimensions, /// especially when simulating retina mode. /// /// It is expected that [TileLayer.fallbackUrl] follows the same retina support diff --git a/lib/src/layer/tile_layer/tile.dart b/lib/src/layer/tile_layer/tile.dart index 509419fc1..9eff386c4 100644 --- a/lib/src/layer/tile_layer/tile.dart +++ b/lib/src/layer/tile_layer/tile.dart @@ -14,7 +14,7 @@ class Tile extends StatefulWidget { final TileBuilder? tileBuilder; /// The tile size for the given scale of the map. - final double scaledTileSize; + final double scaledTileDimension; /// Reference to the offset of the top-left corner of the bounding rectangle /// of the [MapCamera]. The origin will not equal the offset of the top-left @@ -39,7 +39,7 @@ class Tile extends StatefulWidget { /// Creates a new instance of [Tile]. const Tile({ super.key, - required this.scaledTileSize, + required this.scaledTileDimension, required this.currentPixelOrigin, required this.tileImage, required this.tileBuilder, @@ -70,12 +70,12 @@ class _TileState extends State { @override Widget build(BuildContext context) { return Positioned( - left: widget.positionCoordinates.x * widget.scaledTileSize - + left: widget.positionCoordinates.x * widget.scaledTileDimension - widget.currentPixelOrigin.x, - top: widget.positionCoordinates.y * widget.scaledTileSize - + top: widget.positionCoordinates.y * widget.scaledTileDimension - widget.currentPixelOrigin.y, - width: widget.scaledTileSize, - height: widget.scaledTileSize, + width: widget.scaledTileDimension, + height: widget.scaledTileDimension, child: widget.tileBuilder?.call(context, _tileImage, widget.tileImage) ?? _tileImage, ); diff --git a/lib/src/layer/tile_layer/tile_bounds/tile_bounds.dart b/lib/src/layer/tile_layer/tile_bounds/tile_bounds.dart index 819191778..f2ef8b1d1 100644 --- a/lib/src/layer/tile_layer/tile_bounds/tile_bounds.dart +++ b/lib/src/layer/tile_layer/tile_bounds/tile_bounds.dart @@ -9,7 +9,7 @@ import 'package:meta/meta.dart'; abstract class TileBounds { /// Reference to the coordinate reference system. final Crs crs; - final double _tileSize; + final int _tileDimension; final LatLngBounds? _latLngBounds; /// Constructor that creates an instance of a subclass of [TileBounds]: @@ -18,21 +18,21 @@ abstract class TileBounds { /// [WrappedTileBounds] if the CRS is wrapped. factory TileBounds({ required Crs crs, - required double tileSize, + required int tileDimension, LatLngBounds? latLngBounds, }) { if (crs.infinite && latLngBounds == null) { - return InfiniteTileBounds._(crs, tileSize, latLngBounds); + return InfiniteTileBounds._(crs, tileDimension, latLngBounds); } else if (crs.wrapLat == null && crs.wrapLng == null) { - return DiscreteTileBounds._(crs, tileSize, latLngBounds); + return DiscreteTileBounds._(crs, tileDimension, latLngBounds); } else { - return WrappedTileBounds._(crs, tileSize, latLngBounds); + return WrappedTileBounds._(crs, tileDimension, latLngBounds); } } const TileBounds._( this.crs, - this._tileSize, + this._tileDimension, this._latLngBounds, ); @@ -43,10 +43,12 @@ abstract class TileBounds { /// parameters. bool shouldReplace( Crs crs, - double tileSize, + int tileDimension, LatLngBounds? latLngBounds, ) => - crs != this.crs || tileSize != _tileSize || latLngBounds != _latLngBounds; + crs != this.crs || + tileDimension != _tileDimension || + latLngBounds != _latLngBounds; } /// [TileBounds] that have no limits. @@ -54,7 +56,7 @@ abstract class TileBounds { class InfiniteTileBounds extends TileBounds { const InfiniteTileBounds._( super.crs, - super._tileSize, + super._tileDimension, super._latLngBounds, ) : super._(); @@ -69,7 +71,7 @@ class DiscreteTileBounds extends TileBounds { DiscreteTileBounds._( super.crs, - super._tileSize, + super._tileDimension, super._latLngBounds, ) : super._(); @@ -97,7 +99,7 @@ class DiscreteTileBounds extends TileBounds { return DiscreteTileBoundsAtZoom( DiscreteTileRange.fromPixelBounds( zoom: zoom, - tileSize: _tileSize, + tileDimension: _tileDimension, pixelBounds: pixelBounds, ), ); @@ -111,7 +113,7 @@ class WrappedTileBounds extends TileBounds { WrappedTileBounds._( super.crs, - super._tileSize, + super._tileDimension, super._latLngBounds, ) : super._(); @@ -136,30 +138,30 @@ class WrappedTileBounds extends TileBounds { (int, int)? wrapX; if (crs.wrapLng case final wrapLng?) { - final wrapXMin = - (crs.latLngToPoint(LatLng(0, wrapLng.$1), zoomDouble).x / _tileSize) - .floor(); - final wrapXMax = - (crs.latLngToPoint(LatLng(0, wrapLng.$2), zoomDouble).x / _tileSize) - .ceil(); + final wrapXMin = (crs.latLngToPoint(LatLng(0, wrapLng.$1), zoomDouble).x / + _tileDimension) + .floor(); + final wrapXMax = (crs.latLngToPoint(LatLng(0, wrapLng.$2), zoomDouble).x / + _tileDimension) + .ceil(); wrapX = (wrapXMin, wrapXMax - 1); } (int, int)? wrapY; if (crs.wrapLat case final wrapLat?) { - final wrapYMin = - (crs.latLngToPoint(LatLng(wrapLat.$1, 0), zoomDouble).y / _tileSize) - .floor(); - final wrapYMax = - (crs.latLngToPoint(LatLng(wrapLat.$2, 0), zoomDouble).y / _tileSize) - .ceil(); + final wrapYMin = (crs.latLngToPoint(LatLng(wrapLat.$1, 0), zoomDouble).y / + _tileDimension) + .floor(); + final wrapYMax = (crs.latLngToPoint(LatLng(wrapLat.$2, 0), zoomDouble).y / + _tileDimension) + .ceil(); wrapY = (wrapYMin, wrapYMax - 1); } return WrappedTileBoundsAtZoom( tileRange: DiscreteTileRange.fromPixelBounds( zoom: zoom, - tileSize: _tileSize, + tileDimension: _tileDimension, pixelBounds: pixelBounds, ), wrappedAxisIsAlwaysInBounds: _latLngBounds == null, diff --git a/lib/src/layer/tile_layer/tile_layer.dart b/lib/src/layer/tile_layer/tile_layer.dart index b6c9f538c..70f99e655 100644 --- a/lib/src/layer/tile_layer/tile_layer.dart +++ b/lib/src/layer/tile_layer/tile_layer.dart @@ -56,9 +56,25 @@ class TileLayer extends StatefulWidget { /// If not `null`, then tiles will pull's WMS protocol requests final WMSTileLayerOptions? wmsOptions; - /// Size for the tile. - /// Default is 256 - late final double tileSize; + /// Size in pixels of each tile image + /// + /// Should be a positive integer power of 2. If not an integer, it may be + /// truncated to the nearest integer. + /// + /// If increasing past 256(px) (default), adjust [zoomOffset] as necessary, + /// for example 512px: -1. + /// + /// Prefer [tileDimension]. If `null` (default), [tileDimension] is used. + @Deprecated('`tileSize` is deprecated. Use `tileDimension` instead.') + late final double? tileSize; + + /// Size in pixels of each tile image + /// + /// Should be a positive power of 2. Defaults to 256px. + /// + /// If increasing past 256(px) (default), adjust [zoomOffset] as necessary, + /// for example 512px: -1. + late final int tileDimension; /// The minimum zoom level down to which this layer will be displayed /// (inclusive) @@ -216,7 +232,9 @@ class TileLayer extends StatefulWidget { super.key, this.urlTemplate, this.fallbackUrl, - double tileSize = 256, + @Deprecated('`tileSize` is deprecated. Use `tileDimension` instead.') + double? tileSize, + int tileDimension = 256, double minZoom = 0, double maxZoom = double.infinity, int minNativeZoom = 0, @@ -319,8 +337,14 @@ class TileLayer extends StatefulWidget { this.zoomOffset = useSimulatedRetina ? (zoomReverse ? zoomOffset - 1.0 : zoomOffset + 1.0) : zoomOffset; - this.tileSize = - useSimulatedRetina ? (tileSize / 2.0).floorToDouble() : tileSize; + // Deprecated assignment + if (tileSize case final tileSize?) { + // ignore: deprecated_member_use_from_same_package + this.tileSize = + useSimulatedRetina ? (tileSize / 2.0).floorToDouble() : tileSize; + } + this.tileDimension = + useSimulatedRetina ? tileDimension ~/ 2 : tileDimension; } @override @@ -346,11 +370,16 @@ class _TileLayerState extends State with TickerProviderStateMixin { StreamSubscription? _resetSub; + // REMOVE once `tileSize` is removed, and replace references with + // `widget.tileDimension` + // ignore: deprecated_member_use_from_same_package + int get _tileDimension => widget.tileSize?.toInt() ?? widget.tileDimension; + @override void initState() { super.initState(); _resetSub = widget.reset?.listen(_resetStreamHandler); - _tileRangeCalculator = TileRangeCalculator(tileSize: widget.tileSize); + _tileRangeCalculator = TileRangeCalculator(tileDimension: _tileDimension); } // This is called on every map movement so we should avoid expensive logic @@ -379,21 +408,21 @@ class _TileLayerState extends State with TickerProviderStateMixin { var reloadTiles = false; if (!_initializedFromMapCamera || _tileBounds.shouldReplace( - camera.crs, widget.tileSize, widget.tileBounds)) { + camera.crs, _tileDimension, widget.tileBounds)) { reloadTiles = true; _tileBounds = TileBounds( crs: camera.crs, - tileSize: widget.tileSize, + tileDimension: _tileDimension, latLngBounds: widget.tileBounds, ); } if (!_initializedFromMapCamera || - _tileScaleCalculator.shouldReplace(camera.crs, widget.tileSize)) { + _tileScaleCalculator.shouldReplace(camera.crs, _tileDimension)) { reloadTiles = true; _tileScaleCalculator = TileScaleCalculator( crs: camera.crs, - tileSize: widget.tileSize, + tileDimension: _tileDimension, ); } @@ -408,23 +437,25 @@ class _TileLayerState extends State with TickerProviderStateMixin { var reloadTiles = false; // There is no caching in TileRangeCalculator so we can just replace it. - _tileRangeCalculator = TileRangeCalculator(tileSize: widget.tileSize); + _tileRangeCalculator = TileRangeCalculator(tileDimension: _tileDimension); if (_tileBounds.shouldReplace( - _tileBounds.crs, widget.tileSize, widget.tileBounds)) { + _tileBounds.crs, _tileDimension, widget.tileBounds)) { _tileBounds = TileBounds( crs: _tileBounds.crs, - tileSize: widget.tileSize, + tileDimension: _tileDimension, latLngBounds: widget.tileBounds, ); reloadTiles = true; } if (_tileScaleCalculator.shouldReplace( - _tileScaleCalculator.crs, widget.tileSize)) { + _tileScaleCalculator.crs, + _tileDimension, + )) { _tileScaleCalculator = TileScaleCalculator( crs: _tileScaleCalculator.crs, - tileSize: widget.tileSize, + tileDimension: widget.tileDimension, ); } @@ -520,7 +551,7 @@ class _TileLayerState extends State with TickerProviderStateMixin { // Must be an ObjectKey, not a ValueKey using the coordinates, in // case we remove and replace the TileImage with a different one. key: ObjectKey(tileRenderer), - scaledTileSize: _tileScaleCalculator.scaledTileSize( + scaledTileDimension: _tileScaleCalculator.scaledTileDimension( map.zoom, tileRenderer.positionCoordinates.z, ), diff --git a/lib/src/layer/tile_layer/tile_provider/base_tile_provider.dart b/lib/src/layer/tile_layer/tile_provider/base_tile_provider.dart index 954684aa5..3dee61062 100644 --- a/lib/src/layer/tile_layer/tile_provider/base_tile_provider.dart +++ b/lib/src/layer/tile_layer/tile_provider/base_tile_provider.dart @@ -199,7 +199,9 @@ abstract class TileProvider { : options.subdomains[ (coordinates.x + coordinates.y) % options.subdomains.length], 'r': options.resolvedRetinaMode == RetinaMode.server ? '@2x' : '', - 'd': options.tileSize.toString(), + // ignore: deprecated_member_use_from_same_package + 'd': options.tileSize?.toInt().toString() ?? + options.tileDimension.toString(), ...options.additionalOptions, }; } @@ -224,12 +226,15 @@ abstract class TileProvider { populateTemplatePlaceholders( options.wmsOptions?.getUrl( coordinates, - options.tileSize.toInt(), + // ignore: deprecated_member_use_from_same_package + options.tileSize?.toInt() ?? options.tileDimension, options.resolvedRetinaMode == RetinaMode.simulation, ) ?? options.urlTemplate ?? (throw ArgumentError( - '`wmsOptions` or `urlTemplate` must be provided to generate a tile URL')), + '`wmsOptions` or `urlTemplate` must be provided to generate ' + 'a tile URL', + )), coordinates, options, ); diff --git a/lib/src/layer/tile_layer/tile_range.dart b/lib/src/layer/tile_layer/tile_range.dart index 0f4c889bf..41eff6ee0 100644 --- a/lib/src/layer/tile_layer/tile_range.dart +++ b/lib/src/layer/tile_layer/tile_range.dart @@ -41,17 +41,17 @@ class DiscreteTileRange extends TileRange { /// Calculate a [DiscreteTileRange] by using the pixel bounds. factory DiscreteTileRange.fromPixelBounds({ required int zoom, - required double tileSize, + required int tileDimension, required Bounds pixelBounds, }) { final Bounds bounds; if (pixelBounds.min == pixelBounds.max) { - final minAndMax = (pixelBounds.min / tileSize).floor(); + final minAndMax = (pixelBounds.min / tileDimension).floor(); bounds = Bounds(minAndMax, minAndMax); } else { bounds = Bounds( - (pixelBounds.min / tileSize).floor(), - (pixelBounds.max / tileSize).ceil() - const Point(1, 1), + (pixelBounds.min / tileDimension).floor(), + (pixelBounds.max / tileDimension).ceil() - const Point(1, 1), ); } diff --git a/lib/src/layer/tile_layer/tile_range_calculator.dart b/lib/src/layer/tile_layer/tile_range_calculator.dart index 87c3e9096..169f11c17 100644 --- a/lib/src/layer/tile_layer/tile_range_calculator.dart +++ b/lib/src/layer/tile_layer/tile_range_calculator.dart @@ -7,10 +7,10 @@ import 'package:meta/meta.dart'; @immutable class TileRangeCalculator { /// The tile size in pixels. - final double tileSize; + final int tileDimension; /// Create a new [TileRangeCalculator] instance. - const TileRangeCalculator({required this.tileSize}); + const TileRangeCalculator({required this.tileDimension}); /// Calculates the visible pixel bounds at the [tileZoom] zoom level when /// viewing the map from the [viewingZoom] centered at the [center]. The @@ -27,7 +27,7 @@ class TileRangeCalculator { }) { return DiscreteTileRange.fromPixelBounds( zoom: tileZoom, - tileSize: tileSize, + tileDimension: tileDimension, pixelBounds: _calculatePixelBounds( camera, center ?? camera.center, diff --git a/lib/src/layer/tile_layer/tile_scale_calculator.dart b/lib/src/layer/tile_layer/tile_scale_calculator.dart index 01df1d3ef..dd1fd2324 100644 --- a/lib/src/layer/tile_layer/tile_scale_calculator.dart +++ b/lib/src/layer/tile_layer/tile_scale_calculator.dart @@ -6,7 +6,7 @@ class TileScaleCalculator { final Crs crs; /// The size in pixel of tiles. - final double tileSize; + final int tileDimension; double? _cachedCurrentZoom; final Map _cache = {}; @@ -14,35 +14,37 @@ class TileScaleCalculator { /// Create a new [TileScaleCalculator] instance. TileScaleCalculator({ required this.crs, - required this.tileSize, + required this.tileDimension, }); - /// Returns true to indicate that the TileSizeCache should get replaced. - bool shouldReplace(Crs crs, double tileSize) => - this.crs != crs || this.tileSize != tileSize; + /// Returns true to indicate that the TileDimensionCache should get replaced. + bool shouldReplace(Crs crs, int tileDimension) => + this.crs != crs || this.tileDimension != tileDimension; /// Clears the cache if the zoom level does not match the current cached one - /// and sets [currentZoom] as the new zoom to cache for. Must be called - /// before calling scaledTileSize with a [currentZoom] different than the - /// last time scaledTileSize was called. + /// and sets [currentZoom] as the new zoom to cache for. + /// + /// Must be called before calling [scaledTileDimension] with a [currentZoom] + /// different than the last time [scaledTileDimension] was called. void clearCacheUnlessZoomMatches(double currentZoom) { if (_cachedCurrentZoom != currentZoom) _cache.clear(); _cachedCurrentZoom = currentZoom; } /// Returns a scale value to transform a Tile coordinate to a Tile position. - double scaledTileSize(double currentZoom, int tileZoom) { + double scaledTileDimension(double currentZoom, int tileZoom) { assert( _cachedCurrentZoom == currentZoom, 'The cachedCurrentZoom value and the provided currentZoom need to be equal', ); return _cache.putIfAbsent( tileZoom, - () => _scaledTileSizeImpl(currentZoom, tileZoom), + () => _scaledTileDimensionImpl(currentZoom, tileZoom), ); } - double _scaledTileSizeImpl(double currentZoom, int tileZoom) { - return tileSize * (crs.scale(currentZoom) / crs.scale(tileZoom.toDouble())); + double _scaledTileDimensionImpl(double currentZoom, int tileZoom) { + return tileDimension * + (crs.scale(currentZoom) / crs.scale(tileZoom.toDouble())); } } diff --git a/lib/src/layer/tile_layer/wms_tile_layer_options.dart b/lib/src/layer/tile_layer/wms_tile_layer_options.dart index ae98d6317..650012e82 100644 --- a/lib/src/layer/tile_layer/wms_tile_layer_options.dart +++ b/lib/src/layer/tile_layer/wms_tile_layer_options.dart @@ -68,9 +68,9 @@ class WMSTileLayerOptions { } /// Build the URL for a tile - String getUrl(TileCoordinates coords, int tileSize, bool retinaMode) { - final nwPoint = coords * tileSize; - final sePoint = nwPoint + Point(tileSize, tileSize); + String getUrl(TileCoordinates coords, int tileDimension, bool retinaMode) { + final nwPoint = coords * tileDimension; + final sePoint = nwPoint + Point(tileDimension, tileDimension); final nwCoords = crs.pointToLatLng(nwPoint, coords.z.toDouble()); final seCoords = crs.pointToLatLng(sePoint, coords.z.toDouble()); final nw = crs.projection.project(nwCoords); @@ -81,8 +81,8 @@ class WMSTileLayerOptions { : [bounds.min.x, bounds.min.y, bounds.max.x, bounds.max.y]; final buffer = StringBuffer(_encodedBaseUrl); - buffer.write('&width=${retinaMode ? tileSize * 2 : tileSize}'); - buffer.write('&height=${retinaMode ? tileSize * 2 : tileSize}'); + buffer.write('&width=${retinaMode ? tileDimension * 2 : tileDimension}'); + buffer.write('&height=${retinaMode ? tileDimension * 2 : tileDimension}'); buffer.write('&bbox=${bbox.join(',')}'); return buffer.toString(); } diff --git a/test/layer/tile_layer/tile_bounds/tile_bounds_at_zoom_test.dart b/test/layer/tile_layer/tile_bounds/tile_bounds_at_zoom_test.dart index eed5e9882..fde35643f 100644 --- a/test/layer/tile_layer/tile_bounds/tile_bounds_at_zoom_test.dart +++ b/test/layer/tile_layer/tile_bounds/tile_bounds_at_zoom_test.dart @@ -12,7 +12,7 @@ void main() { Point(hugeCoordinate.x.toDouble(), hugeCoordinate.y.toDouble()); final tileRangeWithHugeCoordinate = DiscreteTileRange.fromPixelBounds( zoom: 0, - tileSize: 1, + tileDimension: 1, pixelBounds: Bounds(hugePoint, hugePoint), ); diff --git a/test/layer/tile_layer/tile_bounds/tile_bounds_test.dart b/test/layer/tile_layer/tile_bounds/tile_bounds_test.dart index 354a5590e..1df422e43 100644 --- a/test/layer/tile_layer/tile_bounds/tile_bounds_test.dart +++ b/test/layer/tile_layer/tile_bounds/tile_bounds_test.dart @@ -14,7 +14,7 @@ void main() { test('crs is infinite, latLngBounds null', () { final tileBounds = TileBounds( crs: const FakeInfiniteCrs(), - tileSize: 256, + tileDimension: 256, ); expect(tileBounds, isA()); @@ -24,7 +24,7 @@ void main() { test('crs is infinite, latLngBounds provided', () { final tileBounds = TileBounds( crs: const FakeInfiniteCrs(), - tileSize: 256, + tileDimension: 256, latLngBounds: LatLngBounds.fromPoints( [const LatLng(-44, -55), const LatLng(44, 55)], ), @@ -46,7 +46,7 @@ void main() { test('crs is finite non-wrapping', () { final tileBounds = TileBounds( crs: const CrsSimple(), - tileSize: 256, + tileDimension: 256, ); expect(tileBounds, isA()); @@ -65,7 +65,7 @@ void main() { test('crs is finite wrapping', () { final tileBounds = TileBounds( crs: const Epsg3857(), - tileSize: 256, + tileDimension: 256, ); expect(tileBounds, isA()); @@ -90,7 +90,7 @@ void main() { const crs = Epsg3857(); final tileBounds = TileBounds( crs: crs, - tileSize: 256, + tileDimension: 256, latLngBounds: LatLngBounds( const LatLng(0, 0), crs.pointToLatLng(crs.getProjectedBounds(0)!.max, 0), @@ -144,7 +144,7 @@ void main() { final tileBounds = TileBounds( crs: const Epsg3857(), - tileSize: 256, + tileDimension: 256, ); for (final entry in expectedTileCounts.entries) { @@ -176,7 +176,7 @@ void main() { final tileBounds = TileBounds( crs: const Epsg3857(), - tileSize: 256, + tileDimension: 256, ); for (final entry in expectedTileRanges.entries) { @@ -210,7 +210,7 @@ void main() { final tileBounds = TileBounds( crs: const Epsg3857(), - tileSize: 256, + tileDimension: 256, ); for (final entry in expectedTileRanges.entries) { diff --git a/test/layer/tile_layer/tile_range_test.dart b/test/layer/tile_layer/tile_range_test.dart index 62d001557..19a0ec39d 100644 --- a/test/layer/tile_layer/tile_range_test.dart +++ b/test/layer/tile_layer/tile_range_test.dart @@ -10,12 +10,12 @@ void main() { test('behaves as an empty range', () { final tileRange1 = DiscreteTileRange.fromPixelBounds( zoom: 0, - tileSize: 1, + tileDimension: 1, pixelBounds: Bounds(const Point(1, 1), const Point(2, 2)), ); final tileRange2 = DiscreteTileRange.fromPixelBounds( zoom: 0, - tileSize: 1, + tileDimension: 1, pixelBounds: Bounds(const Point(3, 3), const Point(4, 4)), ); final emptyTileRange = tileRange1.intersect(tileRange2); @@ -33,7 +33,7 @@ void main() { test('single tile', () { final tileRange = DiscreteTileRange.fromPixelBounds( zoom: 0, - tileSize: 10, + tileDimension: 10, pixelBounds: Bounds( const Point(25, 25), const Point(25, 25), @@ -47,7 +47,7 @@ void main() { test('lower tile edge', () { final tileRange = DiscreteTileRange.fromPixelBounds( zoom: 0, - tileSize: 10, + tileDimension: 10, pixelBounds: Bounds( const Point(0, 0), const Point(0.1, 0.1), @@ -61,7 +61,7 @@ void main() { test('upper tile edge', () { final tileRange = DiscreteTileRange.fromPixelBounds( zoom: 0, - tileSize: 10, + tileDimension: 10, pixelBounds: Bounds( const Point(0, 0), const Point(9.99, 9.99), @@ -75,7 +75,7 @@ void main() { test('both tile edges', () { final tileRange = DiscreteTileRange.fromPixelBounds( zoom: 0, - tileSize: 10, + tileDimension: 10, pixelBounds: Bounds( const Point(19.99, 19.99), const Point(30.1, 30.1), @@ -99,7 +99,7 @@ void main() { test('expand', () { final tileRange = DiscreteTileRange.fromPixelBounds( zoom: 0, - tileSize: 10, + tileDimension: 10, pixelBounds: Bounds( const Point(25, 25), const Point(25, 25), @@ -126,7 +126,7 @@ void main() { test('no intersection', () { final tileRange1 = DiscreteTileRange.fromPixelBounds( zoom: 0, - tileSize: 10, + tileDimension: 10, pixelBounds: Bounds( const Point(25, 25), const Point(25, 25), @@ -135,7 +135,7 @@ void main() { final tileRange2 = DiscreteTileRange.fromPixelBounds( zoom: 0, - tileSize: 10, + tileDimension: 10, pixelBounds: Bounds( const Point(35, 35), const Point(35, 35), @@ -152,7 +152,7 @@ void main() { test('intersects', () { final tileRange1 = DiscreteTileRange.fromPixelBounds( zoom: 0, - tileSize: 10, + tileDimension: 10, pixelBounds: Bounds( const Point(25, 25), const Point(35, 35), @@ -161,7 +161,7 @@ void main() { final tileRange2 = DiscreteTileRange.fromPixelBounds( zoom: 0, - tileSize: 10, + tileDimension: 10, pixelBounds: Bounds( const Point(35, 35), const Point(45, 45), @@ -180,7 +180,7 @@ void main() { test('range within other range', () { final tileRange1 = DiscreteTileRange.fromPixelBounds( zoom: 0, - tileSize: 10, + tileDimension: 10, pixelBounds: Bounds( const Point(25, 25), const Point(35, 35), @@ -189,7 +189,7 @@ void main() { final tileRange2 = DiscreteTileRange.fromPixelBounds( zoom: 0, - tileSize: 10, + tileDimension: 10, pixelBounds: Bounds( const Point(15, 15), const Point(45, 45), @@ -209,7 +209,7 @@ void main() { test('min/max', () { final tileRange = DiscreteTileRange.fromPixelBounds( zoom: 0, - tileSize: 10, + tileDimension: 10, pixelBounds: Bounds( const Point(35, 35), const Point(45, 45), @@ -224,7 +224,7 @@ void main() { test('one tile', () { final tileRange = DiscreteTileRange.fromPixelBounds( zoom: 0, - tileSize: 10, + tileDimension: 10, pixelBounds: Bounds( const Point(35, 35), const Point(35, 35), @@ -237,7 +237,7 @@ void main() { test('multiple tiles, even number of tiles', () { final tileRange = DiscreteTileRange.fromPixelBounds( zoom: 0, - tileSize: 10, + tileDimension: 10, pixelBounds: Bounds( const Point(35, 35), const Point(45, 45), @@ -250,7 +250,7 @@ void main() { test('multiple tiles, odd number of tiles', () { final tileRange = DiscreteTileRange.fromPixelBounds( zoom: 0, - tileSize: 10, + tileDimension: 10, pixelBounds: Bounds( const Point(35, 35), const Point(55, 55), @@ -264,7 +264,7 @@ void main() { test('contains', () { final tileRange = DiscreteTileRange.fromPixelBounds( zoom: 10, - tileSize: 10, + tileDimension: 10, pixelBounds: Bounds( const Point(35, 35), const Point(35, 35), From 8d52ab40012801686ef6975d098c6d1cdb000dc0 Mon Sep 17 00:00:00 2001 From: JaffaKetchup Date: Sun, 17 Nov 2024 21:38:54 +0000 Subject: [PATCH 4/4] Fixed bug --- lib/src/layer/tile_layer/tile_layer.dart | 11 ++++++----- 1 file changed, 6 insertions(+), 5 deletions(-) diff --git a/lib/src/layer/tile_layer/tile_layer.dart b/lib/src/layer/tile_layer/tile_layer.dart index 70f99e655..3ff6cdbda 100644 --- a/lib/src/layer/tile_layer/tile_layer.dart +++ b/lib/src/layer/tile_layer/tile_layer.dart @@ -338,11 +338,12 @@ class TileLayer extends StatefulWidget { ? (zoomReverse ? zoomOffset - 1.0 : zoomOffset + 1.0) : zoomOffset; // Deprecated assignment - if (tileSize case final tileSize?) { - // ignore: deprecated_member_use_from_same_package - this.tileSize = - useSimulatedRetina ? (tileSize / 2.0).floorToDouble() : tileSize; - } + // ignore: deprecated_member_use_from_same_package + this.tileSize = tileSize == null + ? null + : useSimulatedRetina + ? (tileSize / 2).floorToDouble() + : tileSize; this.tileDimension = useSimulatedRetina ? tileDimension ~/ 2 : tileDimension; }