Skip to content

Commit

Permalink
Avoid white space when resizing map on web (#474)
Browse files Browse the repository at this point in the history
* Fixes null reference in fullmap example

* Avoid white space when resizing map on web

When resizing the browser screen Flutter fires several resize events.
These events are properly propagated to the containing div, but not
always by the map. It appears Mapbox misses the last resize event.
Therefore, we fire a timer that will check for mismatching width/height
between mapbox and it's container and trigger an additonal resizing.
  • Loading branch information
jrehwaldt authored Dec 19, 2020
1 parent dad427b commit f3403d1
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 6 deletions.
13 changes: 7 additions & 6 deletions example/lib/full_map.dart
Original file line number Diff line number Diff line change
Expand Up @@ -32,11 +32,12 @@ class FullMapState extends State<FullMap> {
Widget build(BuildContext context) {
return new Scaffold(
body: MapboxMap(
accessToken: MapsDemo.ACCESS_TOKEN,
onMapCreated: _onMapCreated,
initialCameraPosition:
const CameraPosition(target: LatLng(0.0, 0.0)),
)
);
accessToken: MapsDemo.ACCESS_TOKEN,
onMapCreated: _onMapCreated,
initialCameraPosition: const CameraPosition(target: LatLng(0.0, 0.0)),
onStyleLoadedCallback: onStyleLoadedCallback,
));
}

void onStyleLoadedCallback() {}
}
13 changes: 13 additions & 0 deletions mapbox_gl_web/lib/src/mapbox_map_controller.dart
Original file line number Diff line number Diff line change
Expand Up @@ -341,6 +341,19 @@ class MapboxMapController extends MapboxGlPlatform
_map.on('movestart', _onCameraMoveStarted);
_map.on('move', _onCameraMove);
_map.on('moveend', _onCameraIdle);
_map.on('resize', _onMapResize);
}

void _onMapResize(Event e) {
Timer(Duration(microseconds: 10), () {
var container = _map.getContainer();
var canvas = _map.getCanvas();
var widthMismatch = canvas.clientWidth != container.clientWidth;
var heightMismatch = canvas.clientHeight != container.clientHeight;
if (widthMismatch || heightMismatch) {
_map.resize();
}
});
}

void _onMapClick(e) {
Expand Down

0 comments on commit f3403d1

Please sign in to comment.