|
1 | 1 | import 'dart:io'; |
2 | 2 | import 'package:flutter/widgets.dart'; |
3 | 3 | import 'package:flutter_map/flutter_map.dart'; |
4 | | -import 'package:path/path.dart' as p; |
5 | 4 |
|
6 | | -/// A TileProvider that checks if a tile exists locally before falling back to network. |
7 | | -class FallbackFileTileProvider extends TileProvider { |
8 | | - final String tilesDir; |
9 | | - final NetworkTileProvider _networkProvider; |
10 | | - |
11 | | - FallbackFileTileProvider({required this.tilesDir}) |
12 | | - : _networkProvider = NetworkTileProvider(); |
| 5 | +class FileTileProvider extends TileProvider { |
| 6 | + FileTileProvider(); |
13 | 7 |
|
14 | 8 | @override |
15 | 9 | ImageProvider getImage(TileCoordinates coordinates, TileLayer options) { |
16 | | - final z = coordinates.z; |
17 | | - final x = coordinates.x; |
18 | | - final y = coordinates.y; |
19 | | - |
20 | | - final tilePath = p.join(tilesDir, '$z', '$x', '$y.png'); |
21 | | - final file = File(tilePath); |
| 10 | + // Determine path from urlTemplate which was populated with dir path |
| 11 | + final url = getTileUrl(coordinates, options); |
| 12 | + final file = File(url); |
22 | 13 |
|
23 | | - try { |
24 | | - if (file.existsSync() == true) { |
25 | | - print("Tile path: $tilePath"); |
26 | | - return FileImage(file); |
27 | | - } else { |
28 | | - return _networkProvider.getImage(coordinates, options); |
29 | | - } |
30 | | - } catch (e, s) { |
31 | | - debugPrint("ERROR: $e"); |
32 | | - debugPrint("$s"); |
33 | | - return _networkProvider.getImage(coordinates, options); |
| 14 | + if (file.existsSync()) { |
| 15 | + return FileImage(file); |
| 16 | + } else { |
| 17 | + debugPrint("Tile not found: $url"); |
| 18 | + // Return a transparent image or handle error |
| 19 | + throw Exception("Tile not found: $url"); |
34 | 20 | } |
35 | 21 | } |
36 | 22 | } |
0 commit comments