Skip to content

Commit

Permalink
update flutter_map dependency to 6.1.0
Browse files Browse the repository at this point in the history
  • Loading branch information
greensopinion committed Dec 9, 2023
1 parent afff1c5 commit e63982a
Show file tree
Hide file tree
Showing 17 changed files with 101 additions and 82 deletions.
5 changes: 5 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,8 @@
## 7.0.0

* update `flutter_map` dependency to 6.1.0

See [UPGRADING](https://github.com/greensopinion/flutter-vector-map-tiles/blob/main/UPGRADING.md) for details.

## 6.0.2

Expand Down
8 changes: 8 additions & 0 deletions UPGRADING.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,14 @@

This guide provides details for upgrading from older versions of the library.


### From 6.x.x to 7.0.0

Version 7.x updates `flutter_map` dependency to version 6.1. See the [flutter_map v6 migration docs](https://docs.fleaflet.dev/getting-started/migrating-to-v6) for details.

Notably, this libarry no longer provides a background color from the material theme. To retain previous behaviour,
set the following: `MapOptions(backgroundColor: material.Theme.of(context).canvasColor)`

### From 3.2.x to 3.3.0

Version 3.3 changes behaviour: instead of rendering vector tiles directly to a canvas, vector tiles are first rendered to an image. This improves the frame rate and reduces jank, but produces tiles that can be less sharp at some zoom levels due to scaling.
Expand Down
14 changes: 5 additions & 9 deletions example/lib/main.dart
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import 'package:flutter/material.dart';
import 'package:flutter/material.dart' as material show Theme;
import 'package:flutter_map/flutter_map.dart';
import 'package:latlong2/latlong.dart';
import 'package:vector_map_tiles/vector_map_tiles.dart';
Expand Down Expand Up @@ -95,15 +96,10 @@ class _MyHomePageState extends State<MyHomePage> {
Widget _map(Style style) => FlutterMap(
mapController: _controller,
options: MapOptions(
center: style.center ?? const LatLng(49.246292, -123.116226),
zoom: style.zoom ?? 10,
initialCenter: style.center ?? const LatLng(49.246292, -123.116226),
initialZoom: style.zoom ?? 10,
maxZoom: 22,
interactiveFlags: InteractiveFlag.rotate |
InteractiveFlag.drag |
InteractiveFlag.flingAnimation |
InteractiveFlag.pinchMove |
InteractiveFlag.pinchZoom |
InteractiveFlag.doubleTapZoom),
backgroundColor: material.Theme.of(context).canvasColor),
children: [
VectorTileLayer(
tileProviders: style.providers,
Expand All @@ -121,6 +117,6 @@ class _MyHomePageState extends State<MyHomePage> {
stream: _controller.mapEventStream,
builder: (context, snapshot) {
return Text(
'Zoom: ${_controller.zoom.toStringAsFixed(2)} Center: ${_controller.center.latitude.toStringAsFixed(4)},${_controller.center.longitude.toStringAsFixed(4)}');
'Zoom: ${_controller.camera.zoom.toStringAsFixed(2)} Center: ${_controller.camera.center.latitude.toStringAsFixed(4)},${_controller.camera.center.longitude.toStringAsFixed(4)}');
}));
}
12 changes: 10 additions & 2 deletions example/pubspec.lock
Original file line number Diff line number Diff line change
Expand Up @@ -98,10 +98,10 @@ packages:
dependency: "direct main"
description:
name: flutter_map
sha256: "5286f72f87deb132daa1489442d6cc46e986fc105cb727d9ae1b602b35b1d1f3"
sha256: cda8d72135b697f519287258b5294a57ce2f2a5ebf234f0e406aad4dc14c9399
url: "https://pub.dev"
source: hosted
version: "5.0.0"
version: "6.1.0"
flutter_test:
dependency: "direct dev"
description: flutter
Expand Down Expand Up @@ -155,6 +155,14 @@ packages:
url: "https://pub.dev"
source: hosted
version: "1.0.1"
logger:
dependency: transitive
description:
name: logger
sha256: "6bbb9d6f7056729537a4309bda2e74e18e5d9f14302489cc1e93f33b3fe32cac"
url: "https://pub.dev"
source: hosted
version: "2.0.2+1"
matcher:
dependency: transitive
description:
Expand Down
2 changes: 1 addition & 1 deletion example/pubspec.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ dependencies:
#path: ../../vector_tile_renderer
vector_map_tiles:
path: ../
flutter_map: ^5.0.0
flutter_map: ^6.1.0
latlong2: ^0.9.0

dev_dependencies:
Expand Down
4 changes: 2 additions & 2 deletions lib/src/grid/constants.dart
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import 'package:flutter_map/plugin_api.dart';
import 'dart:math';

const _tileSize = 256.0;
const tileSize = CustomPoint<double>(_tileSize, _tileSize);
const tileSize = Point<double>(_tileSize, _tileSize);
62 changes: 27 additions & 35 deletions lib/src/grid/grid_layer.dart
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,8 @@ import 'dart:async';
import 'dart:math';

import 'package:executor_lib/executor_lib.dart';
import 'package:flutter/material.dart' as material show Theme;
import 'package:flutter/widgets.dart';
import 'package:flutter_map/plugin_api.dart';
import 'package:flutter_map/flutter_map.dart';
import 'package:latlong2/latlong.dart';
import '../cache/cache_storage_function.dart';
import 'package:vector_tile_renderer/vector_tile_renderer.dart' hide TileLayer;
Expand All @@ -30,10 +29,10 @@ import 'tile/disposable_state.dart';
import 'tile_widgets.dart';

class VectorTileCompositeLayer extends StatefulWidget {
final FlutterMapState mapState;
final MapCamera mapCamera;
final VectorTileLayerOptions options;

const VectorTileCompositeLayer(this.options, this.mapState, {super.key});
const VectorTileCompositeLayer(this.options, this.mapCamera, {super.key});

@override
State<StatefulWidget> createState() {
Expand Down Expand Up @@ -104,7 +103,7 @@ class _VectorTileCompositeLayerState extends State<VectorTileCompositeLayer>
@override
void didUpdateWidget(covariant VectorTileCompositeLayer oldWidget) {
super.didUpdateWidget(oldWidget);
final newState = widget.mapState.toMapState();
final newState = widget.mapCamera.toMapState();
final previousState = _previousState;
_previousState = newState;
if (widget.options.hasRenderDifferences(oldWidget.options)) {
Expand Down Expand Up @@ -139,17 +138,11 @@ class _VectorTileCompositeLayerState extends State<VectorTileCompositeLayer>
options.tileDelay,
options.concurrency);
_tileProvider = tileProvider;
final hasBackground = theme.layers
.where((layer) => layer.type == ThemeLayerType.background)
.isNotEmpty;
layers.add(TileLayer(
key: Key("${theme.id}_v${theme.version}_VectorTileLayer"),
maxZoom: maxZoom,
maxNativeZoom: maxZoom.ceil(),
evictErrorTileStrategy: EvictErrorTileStrategy.notVisible,
backgroundColor: hasBackground
? material.Theme.of(context).canvasColor
: const Color.fromARGB(0, 0, 0, 0),
tileProvider: tileProvider));
}
if (options.layerMode == VectorTileLayerMode.vector) {
Expand All @@ -168,7 +161,7 @@ class _VectorTileCompositeLayerState extends State<VectorTileCompositeLayer>
tileZoomSubstitutionOffset: 0,
mapZoom: _zoom,
rotation: _rotation),
widget.mapState,
widget.mapCamera,
_mapChanged.stream,
_tileSupplier));
if (backgroundTheme != null) {
Expand All @@ -185,13 +178,13 @@ class _VectorTileCompositeLayerState extends State<VectorTileCompositeLayer>
tileZoomSubstitutionOffset: 4,
mapZoom: _zoom,
rotation: _rotation),
widget.mapState,
widget.mapCamera,
_mapChanged.stream,
_tileSupplier);
layers.insert(0, background);
}
}
return Stack(children: layers);
return MobileLayerTransformer(child: Stack(children: layers));
}

void _createCaches() {
Expand Down Expand Up @@ -225,9 +218,9 @@ class _VectorTileCompositeLayerState extends State<VectorTileCompositeLayer>

double _zoom() => max(
1,
(widget.mapState.zoom + widget.options.tileOffset.zoomOffset)
(widget.mapCamera.zoom + widget.options.tileOffset.zoomOffset)
.floorToDouble());
double _rotation() => widget.mapState.rotationRad;
double _rotation() => widget.mapCamera.rotationRad;
}

class _LayerOptions {
Expand Down Expand Up @@ -259,7 +252,7 @@ class _LayerOptions {

class _VectorTileLayer extends StatefulWidget {
final _LayerOptions options;
final FlutterMapState mapState;
final MapCamera mapState;
final Stream<void> stream;
final TranslatingTileProvider tileProvider;

Expand All @@ -278,7 +271,7 @@ class _VectorTileLayerState extends DisposableState<_VectorTileLayer> {
late TileWidgets _tileWidgets;
late final _ZoomScaler _zoomScaler;

FlutterMapState get _mapState => widget.mapState;
MapCamera get _mapCamera => widget.mapState;

double get _zoom => widget.options.mapZoom();
double get _detailZoom =>
Expand All @@ -289,7 +282,7 @@ class _VectorTileLayerState extends DisposableState<_VectorTileLayer> {
@override
void initState() {
super.initState();
_zoomScaler = _ZoomScaler(_mapState.options.crs);
_zoomScaler = _ZoomScaler(_mapCamera.crs);
_createTileWidgets();
_subscription = widget.stream.listen((event) {
_update();
Expand Down Expand Up @@ -347,19 +340,19 @@ class _VectorTileLayerState extends DisposableState<_VectorTileLayer> {
if (tiles.isEmpty) {
return Container();
}
_zoomScaler.updateMapZoomScale(_mapState.zoom);
_zoomScaler.updateMapZoomScale(_mapCamera.zoom);

final tileWidgets = <Widget>[];
var positioner = GridTilePositioner(
tiles.first.key.z,
TilePositioningState(
_zoomScaler.zoomScale(tiles.first.key.z), _mapState, _zoom));
_zoomScaler.zoomScale(tiles.first.key.z), _mapCamera, _zoom));
for (final tile in tiles) {
if (tile.key.z != positioner.tileZoom) {
positioner = GridTilePositioner(
tile.key.z,
TilePositioningState(
_zoomScaler.zoomScale(tile.key.z), _mapState, _zoom));
_zoomScaler.zoomScale(tile.key.z), _mapCamera, _zoom));
}
tileWidgets.add(positioner.positionTile(tile.key, tile.value));
}
Expand All @@ -383,21 +376,20 @@ class _VectorTileLayerState extends DisposableState<_VectorTileLayer> {
}

Bounds _tiledPixelBounds() {
final zoom = _mapState.zoom;
final scale = _mapState.getZoomScale(zoom, _clampedZoom);
final centerPoint = _mapState.project(_mapState.center, _clampedZoom);
final halfSize = _mapState.size / (scale * 2);
final zoom = _mapCamera.zoom;
final scale = _mapCamera.getZoomScale(zoom, _clampedZoom);
final centerPoint = _mapCamera.project(_mapCamera.center, _clampedZoom);
final halfSize = _mapCamera.size / (scale * 2);

return Bounds(centerPoint - halfSize, centerPoint + halfSize);
}

TileViewport _pixelBoundsToTileViewport(Bounds pixelBounds) {
final zoom = _clampedZoom.toInt();
final a = pixelBounds.min.unscaleBy(tileSize).floor();
final b =
pixelBounds.max.unscaleBy(tileSize).ceil() - const CustomPoint(1, 1);
final topLeft = CustomPoint<int>(a.x.toInt(), a.y.toInt());
final bottomRight = CustomPoint<int>(b.x.toInt(), b.y.toInt());
final b = pixelBounds.max.unscaleBy(tileSize).ceil() - const Point(1, 1);
final topLeft = Point<int>(a.x.toInt(), a.y.toInt());
final bottomRight = Point<int>(b.x.toInt(), b.y.toInt());
return TileViewport(zoom, Bounds<int>(topLeft, bottomRight));
}

Expand Down Expand Up @@ -455,9 +447,9 @@ class _ZoomScaler {
class _MapState {
final double zoom;
final double rotation;
final CustomPoint pixelOrigin;
final Point pixelOrigin;
final LatLng center;
final CustomPoint<double> size;
final Point<double> size;
final LatLngBounds bounds;
final Bounds pixelBounds;

Expand All @@ -480,7 +472,7 @@ class _MapState {
int get hashCode => Object.hash(zoom, center, size);
}

extension _MapStateExtension on FlutterMapState {
_MapState toMapState() =>
_MapState(zoom, rotation, pixelOrigin, center, size, bounds, pixelBounds);
extension _MapStateExtension on MapCamera {
_MapState toMapState() => _MapState(
zoom, rotation, pixelOrigin, center, size, visibleBounds, pixelBounds);
}
16 changes: 9 additions & 7 deletions lib/src/grid/grid_tile_positioner.dart
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import 'dart:math';

import 'package:flutter/widgets.dart';
import 'package:flutter_map/plugin_api.dart';
import 'package:flutter_map/flutter_map.dart';

import '../tile_identity.dart';
import 'constants.dart';
Expand Down Expand Up @@ -87,13 +87,15 @@ class GridTileSizer {

class TilePositioningState {
final double zoomScale;
late final CustomPoint<double> origin;
late final CustomPoint<double> translate;
late final Point<double> origin;
late final Point<double> translate;

TilePositioningState(this.zoomScale, FlutterMapState mapState, double zoom) {
final pixelOrigin =
mapState.getNewPixelOrigin(mapState.center, mapState.zoom).round();
origin = mapState.project(mapState.unproject(pixelOrigin, zoom), zoom);
TilePositioningState(this.zoomScale, MapCamera mapCamera, double zoom) {
final pixelOrigin = mapCamera
.getNewPixelOrigin(mapCamera.center, mapCamera.zoom)
.round()
.toDoublePoint();
origin = mapCamera.project(mapCamera.unproject(pixelOrigin, zoom), zoom);
translate = (origin * zoomScale) - pixelOrigin;
}
}
Expand Down
2 changes: 1 addition & 1 deletion lib/src/raster/future_tile_provider.dart
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import 'package:flutter/painting.dart';
import 'package:flutter_map/plugin_api.dart';
import 'package:flutter_map/flutter_map.dart';

class FutureTileProvider extends TileProvider {
final Future<ImageInfo> Function(
Expand Down
2 changes: 1 addition & 1 deletion lib/src/raster/raster_tile_provider.dart
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import 'package:executor_lib/executor_lib.dart';
import 'package:flutter_map/plugin_api.dart';
import 'package:flutter_map/flutter_map.dart';
import 'package:vector_tile_renderer/vector_tile_renderer.dart';

import '../../vector_map_tiles.dart';
Expand Down
2 changes: 1 addition & 1 deletion lib/src/raster/tile_loader.dart
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import 'dart:ui';

import 'package:executor_lib/executor_lib.dart';
import 'package:flutter/widgets.dart' hide Image;
import 'package:flutter_map/plugin_api.dart';
import 'package:flutter_map/flutter_map.dart';
import 'package:vector_tile_renderer/vector_tile_renderer.dart' hide TileLayer;

import '../../vector_map_tiles.dart';
Expand Down
4 changes: 1 addition & 3 deletions lib/src/tile_identity.dart
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
import 'dart:math';

import 'package:flutter_map/plugin_api.dart';

class TileIdentity extends CustomPoint<int> {
class TileIdentity extends Point<int> {
final int z;

TileIdentity(this.z, super.x, super.y);
Expand Down
8 changes: 4 additions & 4 deletions lib/src/tile_viewport.dart
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import 'dart:math';

import 'package:flutter_map/plugin_api.dart';
import 'package:flutter_map/flutter_map.dart';

import '../vector_map_tiles.dart';

Expand All @@ -14,7 +14,7 @@ class TileViewport {
/// the given tile
bool overlaps(TileIdentity tile) {
if (tile.z == zoom) {
return bounds.contains(CustomPoint(tile.x, tile.y));
return bounds.contains(Point(tile.x, tile.y));
}
final zoomDifference = zoom - tile.z;
final multiplier = pow(2, zoomDifference.abs()).toInt();
Expand All @@ -24,15 +24,15 @@ class TileViewport {
(bounds.topLeft.toDoublePoint() * (1 / multiplier)).floor();
final boundsBottomRight =
(bounds.bottomRight.toDoublePoint() * (1 / multiplier)).ceil();
final tilePoint = CustomPoint(tile.x, tile.y);
final tilePoint = Point(tile.x, tile.y);
return Bounds(boundsTopLeft, boundsBottomRight)
.containsPartialBounds(Bounds(tilePoint, tilePoint));
}
// tile is smaller
final tileZoomTopLeft = bounds.topLeft * multiplier;
if (tile.x >= tileZoomTopLeft.x && tile.y >= tileZoomTopLeft.y) {
final tileZoomBottomRight =
(bounds.bottomRight + const CustomPoint(1, 1)) * multiplier;
(bounds.bottomRight + const Point(1, 1)) * multiplier;
return tile.x < tileZoomBottomRight.x && tile.y < tileZoomBottomRight.y;
}
return false;
Expand Down
Loading

0 comments on commit e63982a

Please sign in to comment.