Skip to content

Commit 79609b1

Browse files
authored
feat: warn for OSM URLs (#2074)
1 parent 129e51b commit 79609b1

File tree

4 files changed

+82
-7
lines changed

4 files changed

+82
-7
lines changed

.github/workflows/branch.yml

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -82,7 +82,7 @@ jobs:
8282
channel: "stable"
8383
cache: true
8484
- name: Build Android Application
85-
run: flutter build apk
85+
run: flutter build apk --dart-define=flutter.flutter_map.unblockOSM="${{ secrets.UNBLOCK_OSM }}"
8686
- name: Archive Artifact
8787
uses: actions/upload-artifact@v4
8888
with:
@@ -105,7 +105,7 @@ jobs:
105105
channel: "stable"
106106
cache: true
107107
- name: Build Windows Application
108-
run: flutter build windows
108+
run: flutter build windows --dart-define=flutter.flutter_map.unblockOSM="${{ secrets.UNBLOCK_OSM }}"
109109
- name: Create Windows Application Installer
110110
run: iscc "windowsApplicationInstallerSetup.iss"
111111
working-directory: .
@@ -131,7 +131,7 @@ jobs:
131131
channel: "stable"
132132
cache: true
133133
- name: Build Web Application
134-
run: flutter build web --wasm
134+
run: flutter build web --wasm --dart-define=flutter.flutter_map.unblockOSM="${{ secrets.UNBLOCK_OSM }}"
135135
- name: Archive Artifact
136136
uses: actions/upload-artifact@v4
137137
with:

.github/workflows/master.yml

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@ jobs:
5151
channel: "stable"
5252
cache: true
5353
- name: Build Android Application
54-
run: flutter build apk
54+
run: flutter build apk --dart-define=flutter.flutter_map.unblockOSM="${{ secrets.UNBLOCK_OSM }}"
5555
- name: Archive Artifact
5656
uses: actions/upload-artifact@v4
5757
with:
@@ -76,7 +76,7 @@ jobs:
7676
channel: "stable"
7777
cache: true
7878
- name: Build Windows Application
79-
run: flutter build windows
79+
run: flutter build windows --dart-define=flutter.flutter_map.unblockOSM="${{ secrets.UNBLOCK_OSM }}"
8080
- name: Create Windows Application Installer
8181
run: iscc "windowsApplicationInstallerSetup.iss"
8282
working-directory: .
@@ -104,7 +104,7 @@ jobs:
104104
channel: "stable"
105105
cache: true
106106
- name: Build Web Application
107-
run: flutter build web --wasm
107+
run: flutter build web --wasm --dart-define=flutter.flutter_map.unblockOSM="${{ secrets.UNBLOCK_OSM }}"
108108
- name: Archive Artifact
109109
uses: actions/upload-artifact@v4
110110
with:

lib/src/layer/tile_layer/tile_layer.dart

Lines changed: 48 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import 'dart:async';
22
import 'dart:math';
33

4-
import 'package:collection/collection.dart' show MapEquality;
4+
import 'package:collection/collection.dart' show MapEquality, ListEquality;
55
import 'package:flutter/foundation.dart';
66
import 'package:flutter/material.dart';
77
import 'package:flutter_map/flutter_map.dart';
@@ -12,6 +12,7 @@ import 'package:flutter_map/src/layer/tile_layer/tile_image_manager.dart';
1212
import 'package:flutter_map/src/layer/tile_layer/tile_range.dart';
1313
import 'package:flutter_map/src/layer/tile_layer/tile_range_calculator.dart';
1414
import 'package:flutter_map/src/layer/tile_layer/tile_scale_calculator.dart';
15+
import 'package:flutter_map/src/layer/tile_layer/unblock_osm.dart';
1516
import 'package:flutter_map/src/misc/extensions.dart';
1617
import 'package:http/http.dart';
1718
import 'package:http/retry.dart';
@@ -343,6 +344,41 @@ class TileLayer extends StatefulWidget {
343344
}
344345

345346
class _TileLayerState extends State<TileLayer> with TickerProviderStateMixin {
347+
static const _openStreetMapUrls = {
348+
'tile.openstreetmap.org',
349+
'tile.osm.org',
350+
};
351+
bool get _isOpenStreetMapUrl =>
352+
widget.urlTemplate != null &&
353+
_openStreetMapUrls.any((target) => widget.urlTemplate!.contains(target));
354+
355+
static const _unblockOpenStreetMapUrlEnv =
356+
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;
363+
void _warnOpenStreetMapUrl() {
364+
if (!_isOpenStreetMapUrl || !kDebugMode || _unblockOpenStreetMapUrl) return;
365+
Logger(printer: PrettyPrinter(methodCount: 0)).e(
366+
'''\x1B[1m\x1B[3mflutter_map\x1B[0m
367+
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+
);
380+
}
381+
346382
bool _initializedFromMapCamera = false;
347383

348384
final _tileImageManager = TileImageManager();
@@ -371,6 +407,7 @@ class _TileLayerState extends State<TileLayer> with TickerProviderStateMixin {
371407
super.initState();
372408
_resetSub = widget.reset?.listen(_resetStreamHandler);
373409
_tileRangeCalculator = TileRangeCalculator(tileDimension: _tileDimension);
410+
_warnOpenStreetMapUrl();
374411
}
375412

376413
// This is called on every map movement so we should avoid expensive logic
@@ -428,6 +465,8 @@ class _TileLayerState extends State<TileLayer> with TickerProviderStateMixin {
428465
super.didUpdateWidget(oldWidget);
429466
var reloadTiles = false;
430467

468+
_warnOpenStreetMapUrl();
469+
431470
// There is no caching in TileRangeCalculator so we can just replace it.
432471
_tileRangeCalculator = TileRangeCalculator(tileDimension: _tileDimension);
433472

@@ -505,6 +544,10 @@ class _TileLayerState extends State<TileLayer> with TickerProviderStateMixin {
505544

506545
if (_outsideZoomLimits(map.zoom.round())) return const SizedBox.shrink();
507546

547+
if (_isOpenStreetMapUrl && _blockOpenStreetMapUrl) {
548+
return const SizedBox.shrink();
549+
}
550+
508551
final tileZoom = _clampToNativeZoom(map.zoom);
509552
final tileBoundsAtZoom = _tileBounds.atZoom(tileZoom);
510553
final visibleTileRange = _tileRangeCalculator.calculate(
@@ -667,6 +710,10 @@ class _TileLayerState extends State<TileLayer> with TickerProviderStateMixin {
667710
DiscreteTileRange tileLoadRange, {
668711
required bool pruneAfterLoad,
669712
}) {
713+
if (_isOpenStreetMapUrl && _blockOpenStreetMapUrl) {
714+
return;
715+
}
716+
670717
final tileZoom = tileLoadRange.zoom;
671718
final expandedTileLoadRange = tileLoadRange.expand(widget.panBuffer);
672719

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
/// @nodoc
2+
final unblockOSM = [
3+
79,
4+
117,
5+
114,
6+
32,
7+
116,
8+
105,
9+
108,
10+
101,
11+
32,
12+
115,
13+
101,
14+
114,
15+
118,
16+
101,
17+
114,
18+
115,
19+
32,
20+
97,
21+
114,
22+
101,
23+
32,
24+
110,
25+
111,
26+
116,
27+
46,
28+
];

0 commit comments

Comments
 (0)