Skip to content

Commit

Permalink
Improved map resizing (#1061)
Browse files Browse the repository at this point in the history
* Improved map resizing

* Added functionality to trigger the map resize event from outside the package

* Fixed pipeline
  • Loading branch information
felix-mittermeier committed May 30, 2022
1 parent 734b707 commit b1479f4
Show file tree
Hide file tree
Showing 10 changed files with 87 additions and 18 deletions.
8 changes: 6 additions & 2 deletions .github/workflows/flutter_ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,9 @@ jobs:
- uses: actions/setup-java@v1
with:
java-version: '12.x'
- uses: subosito/flutter-action@v1
- uses: subosito/flutter-action@v2
with:
flutter-version: '2.10.5'
- run: flutter pub get
- name: Build example APK
run: cd example && flutter build apk
Expand All @@ -80,7 +82,9 @@ jobs:
- uses: actions/setup-java@v1
with:
java-version: '12.x'
- uses: subosito/flutter-action@v1
- uses: subosito/flutter-action@v2
with:
flutter-version: '2.10.5'
- run: flutter pub get
- name: build iOS package
run: |
Expand Down
12 changes: 10 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -70,8 +70,16 @@ curl: (22) The requested URL returned error: 401 Unauthorized
Include the JavaScript and CSS files in the `<head>` of your `index.html` file:

```
<script src='https://api.mapbox.com/mapbox-gl-js/v2.7.0/mapbox-gl.js'></script>
<link href='https://api.mapbox.com/mapbox-gl-js/v2.7.0/mapbox-gl.css' rel='stylesheet' />
<script src='https://api.mapbox.com/mapbox-gl-js/v2.8.2/mapbox-gl.js'></script>
<link href='https://api.mapbox.com/mapbox-gl-js/v2.8.2/mapbox-gl.css' rel='stylesheet' />
<style>
.mapboxgl-map {
position: relative;
width: 100%;
height: 100%;
}
</style>
```

*Note: Look for latest version in [Mapbox GL JS documentation](https://docs.mapbox.com/mapbox-gl-js/guides/).*
Expand Down
2 changes: 1 addition & 1 deletion example/web/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
<link rel="shortcut icon" type="image/png" href="/favicon.png"/>

<!-- Mapbox -->
<script src='https://api.mapbox.com/mapbox-gl-js/v2.7.0/mapbox-gl.js'></script>
<script src='https://api.mapbox.com/mapbox-gl-js/v2.8.2/mapbox-gl.js'></script>

<title>example</title>
<link rel="manifest" href="/manifest.json">
Expand Down
15 changes: 15 additions & 0 deletions lib/src/controller.dart
Original file line number Diff line number Diff line change
Expand Up @@ -257,6 +257,21 @@ class MapboxMapController extends ChangeNotifier {
notifyListeners();
}

/// Triggers a resize event for the map on web (ignored on Android or iOS).
///
/// Checks first if a resize is required or if it looks like it is already correctly resized.
/// If it looks good, the resize call will be skipped.
///
/// To force resize map (without any checks) have a look at forceResizeWebMap()
void resizeWebMap() {
_mapboxGlPlatform.resizeWebMap();
}

/// Triggers a hard map resize event on web and does not check if it is required or not.
void forceResizeWebMap() {
_mapboxGlPlatform.forceResizeWebMap();
}

/// Starts an animated change of the map camera position.
///
/// The returned [Future] completes after the change has been started on the
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ import 'package:flutter/gestures.dart';
import 'package:flutter/material.dart';
import 'package:flutter/rendering.dart';
import 'package:flutter/services.dart';
import 'package:meta/meta.dart' show visibleForTesting;

part 'src/annotation.dart';
part 'src/callbacks.dart';
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,9 @@ abstract class MapboxGlPlatform {

Future<void> matchMapLanguageWithDeviceDefault();

void resizeWebMap();
void forceResizeWebMap();

Future<void> updateContentInsets(EdgeInsets insets, bool animated);
Future<void> setMapLanguage(String language);
Future<void> setTelemetryEnabled(bool enabled);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -678,4 +678,10 @@ class MethodChannelMapboxGl extends MapboxGlPlatform {
'geojsonFeature': jsonEncode(geojsonFeature)
});
}

@override
void forceResizeWebMap() {}

@override
void resizeWebMap() {}
}
2 changes: 1 addition & 1 deletion mapbox_gl_web/lib/mapbox_gl_web.dart
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ import 'package:flutter/services.dart';

import 'package:flutter/foundation.dart';
import 'package:flutter/gestures.dart';
import 'package:flutter/material.dart';
import 'package:flutter/material.dart' hide Element;

import 'package:flutter_web_plugins/flutter_web_plugins.dart';
import 'package:mapbox_gl_platform_interface/mapbox_gl_platform_interface.dart';
Expand Down
41 changes: 34 additions & 7 deletions mapbox_gl_web/lib/src/mapbox_web_gl_platform.dart
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ class MapboxWebGlPlatform extends MapboxGlPlatform

String? _navigationControlPosition;
NavigationControl? _navigationControl;
Timer? lastResizeObserverTimer;

@override
Widget buildView(
Expand All @@ -47,8 +48,10 @@ class MapboxWebGlPlatform extends MapboxGlPlatform
ui.platformViewRegistry.registerViewFactory(
'plugins.flutter.io/mapbox_gl_$identifier', (int viewId) {
_mapElement = DivElement()
..style.width = '100%'
..style.height = '100%';
..style.position = 'absolute'
..style.top = '0'
..style.bottom = '0'
..style.width = '100%';
callback(viewId);
return _mapElement;
});
Expand All @@ -59,7 +62,6 @@ class MapboxWebGlPlatform extends MapboxGlPlatform
await _addStylesheetToShadowRoot(_mapElement);
if (_creationParams.containsKey('initialCameraPosition')) {
var camera = _creationParams['initialCameraPosition'];

_dragEnabled = _creationParams['dragEnabled'] ?? true;

if (_creationParams.containsKey('accessToken')) {
Expand All @@ -82,16 +84,31 @@ class MapboxWebGlPlatform extends MapboxGlPlatform
_map.on('movestart', _onCameraMoveStarted);
_map.on('move', _onCameraMove);
_map.on('moveend', _onCameraIdle);
_map.on('resize', _onMapResize);
_map.on('resize', (_) => _onMapResize());
_map.on('styleimagemissing', _loadFromAssets);
if (_dragEnabled) {
_map.on('mouseup', _onMouseUp);
_map.on('mousemove', _onMouseMove);
}

_initResizeObserver();
}
Convert.interpretMapboxMapOptions(_creationParams['options'], this);
}

void _initResizeObserver() {
final resizeObserver = ResizeObserver((entries, observer) {
// The resize observer might be called a lot of times when the user resizes the browser window with the mouse for example.
// Due to the fact that the resize call is quite expensive it should not be called for every triggered event but only the last one, like "onMoveEnd".
// But because there is no event type for the end, there is only the option to spawn timers and cancel the previous ones if they get overwritten by a new event.
lastResizeObserverTimer?.cancel();
lastResizeObserverTimer = Timer(Duration(milliseconds: 50), () {
_onMapResize();
});
});
resizeObserver.observe(document.body as Element);
}

void _loadFromAssets(Event event) async {
final imagePath = event.id;
final ByteData bytes = await rootBundle.load(imagePath);
Expand Down Expand Up @@ -340,12 +357,12 @@ class MapboxWebGlPlatform extends MapboxGlPlatform

void _onStyleLoaded(_) {
_mapReady = true;
_map.resize();
_onMapResize();
onMapStyleLoadedPlatform(null);
}

void _onMapResize(Event e) {
Timer(Duration(microseconds: 10), () {
void _onMapResize() {
Timer(Duration(), () {
var container = _map.getContainer();
var canvas = _map.getCanvas();
var widthMismatch = canvas.clientWidth != container.clientWidth;
Expand Down Expand Up @@ -949,4 +966,14 @@ class MapboxWebGlPlatform extends MapboxGlPlatform
}
}
}

@override
void resizeWebMap() {
_onMapResize();
}

@override
void forceResizeWebMap() {
_map.resize();
}
}
15 changes: 11 additions & 4 deletions pubspec.lock
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ packages:
source: hosted
version: "1.2.0"
collection:
dependency: transitive
dependency: "direct main"
description:
name: collection
url: "https://pub.dartlang.org"
Expand Down Expand Up @@ -59,21 +59,28 @@ packages:
name: mapbox_gl_dart
url: "https://pub.dartlang.org"
source: hosted
version: "0.2.0-nullsafety.0"
version: "0.2.1"
mapbox_gl_platform_interface:
dependency: "direct main"
description:
path: mapbox_gl_platform_interface
relative: true
source: path
version: "0.14.0"
version: "0.16.1"
mapbox_gl_web:
dependency: "direct main"
description:
path: mapbox_gl_web
relative: true
source: path
version: "0.14.0"
version: "0.16.1"
material_color_utilities:
dependency: transitive
description:
name: material_color_utilities
url: "https://pub.dartlang.org"
source: hosted
version: "0.1.3"
meta:
dependency: transitive
description:
Expand Down

0 comments on commit b1479f4

Please sign in to comment.