Skip to content

Commit

Permalink
Added tileBounds param to tile_layer to restrict tiles loaded. (#1212)
Browse files Browse the repository at this point in the history
* add option tileBounds which will do a basic latLng test to check its inside, and not use tile if outside
  • Loading branch information
ibrierley authored Apr 11, 2022
1 parent 0910ed6 commit 4d3f197
Showing 1 changed file with 30 additions and 7 deletions.
37 changes: 30 additions & 7 deletions lib/src/layer/tile_layer.dart
Original file line number Diff line number Diff line change
Expand Up @@ -4,18 +4,13 @@ import 'dart:math' as math;
import 'package:collection/collection.dart' show MapEquality;
import 'package:flutter/material.dart';
import 'package:flutter/widgets.dart';
import 'package:flutter_map/flutter_map.dart';
import 'package:flutter_map/src/core/bounds.dart';
import 'package:flutter_map/src/core/point.dart';
import 'package:flutter_map/src/core/util.dart' as util;
import 'package:flutter_map/src/geo/crs/crs.dart';
import 'package:flutter_map/src/layer/tile_builder/tile_builder.dart';
import 'package:flutter_map/src/layer/tile_provider/tile_provider.dart';
import 'package:flutter_map/src/map/map.dart';
import 'package:latlong2/latlong.dart';
import 'package:tuple/tuple.dart';

import 'layer.dart';

typedef TemplateFunction = String Function(
String str, Map<String, String> data);

Expand Down Expand Up @@ -249,6 +244,9 @@ class TileLayerOptions extends LayerOptions {
/// Stream to notify the [TileLayer] that it needs resetting
Stream<Null>? reset;

/// Only load tiles that are within these bounds
LatLngBounds? tileBounds;

TileLayerOptions(
{this.attributionAlignment = Alignment.bottomRight,
this.attributionBuilder,
Expand Down Expand Up @@ -295,7 +293,8 @@ class TileLayerOptions extends LayerOptions {
this.tilesContainerBuilder,
this.evictErrorTileStrategy = EvictErrorTileStrategy.none,
this.fastReplace = false,
this.reset})
this.reset,
this.tileBounds})
: updateInterval =
updateInterval <= 0 ? null : Duration(milliseconds: updateInterval),
tileFadeInDuration = tileFadeInDuration <= 0
Expand Down Expand Up @@ -996,6 +995,14 @@ class _TileLayerState extends State<TileLayer> with TickerProviderStateMixin {
final coords = Coords(i.toDouble(), j.toDouble());
coords.z = _tileZoom!;

if (options.tileBounds != null) {
var tilePxBounds = _pxBoundsToTileRange(
_latLngBoundsToPixelBounds(options.tileBounds!, _tileZoom!));
if (!_areCoordsInsideTileBounds(coords, tilePxBounds)) {
continue;
}
}

if (!_isValidTile(coords)) {
continue;
}
Expand Down Expand Up @@ -1037,6 +1044,22 @@ class _TileLayerState extends State<TileLayer> with TickerProviderStateMixin {
return true;
}

bool _areCoordsInsideTileBounds(Coords coords, Bounds? tileBounds) {
var bounds = tileBounds ?? _globalTileRange;
if ((coords.x < bounds.min.x || coords.x > bounds.max.x) ||
(coords.y < bounds.min.y || coords.y > bounds.max.y)) {
return false;
}
return true;
}

Bounds _latLngBoundsToPixelBounds(LatLngBounds bounds, double thisZoom) {
var swPixel = map.project(bounds.southWest!, thisZoom).floor();
var nePixel = map.project(bounds.northEast!, thisZoom).ceil();
var pxBounds = Bounds(swPixel, nePixel);
return pxBounds;
}

String _tileCoordsToKey(Coords coords) {
return '${coords.x}:${coords.y}:${coords.z}';
}
Expand Down

0 comments on commit 4d3f197

Please sign in to comment.