Skip to content

Commit 4ad1c2f

Browse files
authored
chore: adjust OSM warning message & consider User-Agent (#2109)
1 parent 916ba0f commit 4ad1c2f

File tree

2 files changed

+40
-31
lines changed

2 files changed

+40
-31
lines changed

lib/src/layer/tile_layer/tile_layer.dart

Lines changed: 39 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -344,39 +344,50 @@ class TileLayer extends StatefulWidget {
344344
}
345345

346346
class _TileLayerState extends State<TileLayer> with TickerProviderStateMixin {
347-
static const _openStreetMapUrls = {
348-
'tile.openstreetmap.org',
349-
'tile.osm.org',
350-
};
347+
static const _openStreetMapUrls = {'tile.openstreetmap.org', 'tile.osm.org'};
351348
bool get _isOpenStreetMapUrl =>
352349
widget.urlTemplate != null &&
353-
_openStreetMapUrls.any((target) => widget.urlTemplate!.contains(target));
350+
_openStreetMapUrls.any(widget.urlTemplate!.contains);
354351

355-
static const _unblockOpenStreetMapUrlEnv =
352+
static const _unblockOSMEnvVar =
356353
String.fromEnvironment('flutter.flutter_map.unblockOSM');
357-
static bool get _unblockOpenStreetMapUrl => const ListEquality<int>()
358-
.equals(_unblockOpenStreetMapUrlEnv.codeUnits, unblockOSM);
359-
360-
static final _blockOpenStreetMapUrl =
361-
// ignore: dead_code
362-
false && (kReleaseMode || kProfileMode) && !_unblockOpenStreetMapUrl;
354+
static bool get _shouldUnblockOSM => const ListEquality<int>()
355+
.equals(_unblockOSMEnvVar.codeUnits, osmUnblockingString);
356+
357+
late final hasSetGoodUserAgent =
358+
widget.tileProvider.headers['User-Agent'] != 'flutter_map (unknown)';
359+
late final _blockOpenStreetMapUrl =
360+
false /*&&
361+
_isOpenStreetMapUrl &&
362+
!hasSetGoodUserAgent &&
363+
!kDebugMode &&
364+
!_shouldUnblockOSM*/
365+
;
363366
void _warnOpenStreetMapUrl() {
364-
if (!_isOpenStreetMapUrl || !kDebugMode || _unblockOpenStreetMapUrl) return;
365-
Logger(printer: PrettyPrinter(methodCount: 0)).e(
366-
'''\x1B[1m\x1B[3mflutter_map\x1B[0m
367+
if (_isOpenStreetMapUrl && kDebugMode && !_shouldUnblockOSM) {
368+
final uaWarning = hasSetGoodUserAgent
369+
? ''
370+
: '''
371+
372+
When using the OSM tile servers, you must set an HTTP User-Agent which
373+
adequately identifies your application to the servers.
374+
Set `TileLayer.userAgentPackageName` appropriately, or set a UA header manually.
375+
In a future flutter_map release, usage of the OpenStreetMap public tile servers
376+
without an adequate User-Agent may be blocked in release mode without warning.
377+
''';
378+
379+
final warning = '''\x1B[1m\x1B[3mflutter_map\x1B[0m$uaWarning
367380
flutter_map wants to help keep map data available for everyone.
368-
We use the public OpenStreetMap tile servers in our code examples & demo app,
369-
but they are NOT free to use by everyone.
370-
In an upcoming non-major release, requests to 'tile.openstreetmap.org' or
371-
'tile.osm.org' will be blocked by default in release mode.
372-
Please review https://operations.osmfoundation.org/policies/tiles/ to see if
373-
your project is compliant with their Tile Usage Policy.
374-
For more information, see https://docs.fleaflet.dev/tile-servers/using-openstreetmap-direct.
375-
It describes in additional detail why we feel it is important to do this, how
376-
you can unblock the tile servers if your use-case is acceptable, the timeframes
377-
for this new policy, and how we're working to reduce requests without any extra
378-
work from you.''',
379-
);
381+
We use the OpenStreetMap public tile servers in our code examples & demo app,
382+
but they are NOT free to use by everyone. Please review whether OSM's tile
383+
servers are appropriate and the best choice for your app.
384+
See:
385+
* https://operations.osmfoundation.org/policies/tiles (OSM Tile Usage Policy)
386+
* https://docs.fleaflet.dev/osm-warn for more information about this warning''';
387+
388+
Logger(printer: PrettyPrinter(methodCount: 0))
389+
.log(hasSetGoodUserAgent ? Level.info : Level.warning, warning);
390+
}
380391
}
381392

382393
bool _initializedFromMapCamera = false;
@@ -465,8 +476,6 @@ work from you.''',
465476
super.didUpdateWidget(oldWidget);
466477
var reloadTiles = false;
467478

468-
_warnOpenStreetMapUrl();
469-
470479
// There is no caching in TileRangeCalculator so we can just replace it.
471480
_tileRangeCalculator = TileRangeCalculator(tileDimension: _tileDimension);
472481

@@ -544,7 +553,7 @@ work from you.''',
544553

545554
if (_outsideZoomLimits(map.zoom.round())) return const SizedBox.shrink();
546555

547-
if (_isOpenStreetMapUrl && _blockOpenStreetMapUrl) {
556+
if (_blockOpenStreetMapUrl) {
548557
return const SizedBox.shrink();
549558
}
550559

lib/src/layer/tile_layer/unblock_osm.dart

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/// @nodoc
2-
final unblockOSM = [
2+
final osmUnblockingString = [
33
79,
44
117,
55
114,

0 commit comments

Comments
 (0)