Skip to content

Commit

Permalink
Fix #629 | Reload map if additionalOptions changed for overrideTilesW…
Browse files Browse the repository at this point in the history
…henUrlChanges (#740)

The option `overrideTilesWhenUrlChanges` currently only works if the URL
template changes but not if the options change.

For example:

```dart
TileLayerOptions(
  urlTemplate: 'https://api.mapbox.com/styles/v1/mapbox/{id}/tiles/{z}/{x}/{y}',
  additionalOptions: <String, String>{
    'id': mapboxStyle,
  },
```

If the value of `id` (here the Mapbox layer style, e.g. streets, satellite, etc.)
changes the option `overrideTilesWhenUrlChanges` should override the tiles as well.
  • Loading branch information
Xennis committed Jan 29, 2021
1 parent 739f956 commit 965cfed
Showing 1 changed file with 13 additions and 2 deletions.
15 changes: 13 additions & 2 deletions lib/src/layer/tile_layer.dart
@@ -1,6 +1,7 @@
import 'dart:async';
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/src/core/bounds.dart';
Expand Down Expand Up @@ -203,7 +204,7 @@ class TileLayerOptions extends LayerOptions {
this.maxNativeZoom,
this.zoomReverse = false,
double zoomOffset = 0.0,
this.additionalOptions = const <String, String>{},
Map<String, String> additionalOptions,
this.subdomains = const <String>[],
this.keepBuffer = 2,
this.backgroundColor = const Color(0xFFE0E0E0),
Expand Down Expand Up @@ -252,6 +253,11 @@ class TileLayerOptions extends LayerOptions {
tileSize = wmsOptions == null && retinaMode && maxZoom > 0.0
? (tileSize / 2.0).floorToDouble()
: tileSize,
// copy additionalOptions Map if not null, so we can safely compare old
// and new Map inside didUpdateWidget with MapEquality.
additionalOptions = additionalOptions == null
? const <String, String>{}
: Map.from(additionalOptions),
super(key: key, rebuild: rebuild);
}

Expand Down Expand Up @@ -430,7 +436,12 @@ class _TileLayerState extends State<TileLayer> with TickerProviderStateMixin {
final oldUrl = oldWidget.options.wmsOptions?._encodedBaseUrl ??
oldWidget.options.urlTemplate;
final newUrl = options.wmsOptions?._encodedBaseUrl ?? options.urlTemplate;
if (oldUrl != newUrl) {

final oldOptions = oldWidget.options.additionalOptions;
final newOptions = options.additionalOptions;

if (oldUrl != newUrl ||
!(const MapEquality()).equals(oldOptions, newOptions)) {
if (options.overrideTilesWhenUrlChanges) {
for (var tile in _tiles.values) {
tile.imageProvider = options.tileProvider
Expand Down

0 comments on commit 965cfed

Please sign in to comment.