From e8be03b02134dcddc033e5960690d233d05a7307 Mon Sep 17 00:00:00 2001 From: Andrea Valenzano Date: Tue, 22 Sep 2020 17:35:48 +0200 Subject: [PATCH 1/2] fix onStyleLoadedCallback --- lib/src/controller.dart | 41 +++++++++++++++++++++-------------------- lib/src/mapbox_map.dart | 12 ++++++------ 2 files changed, 27 insertions(+), 26 deletions(-) diff --git a/lib/src/controller.dart b/lib/src/controller.dart index 13e226e50..53705319a 100644 --- a/lib/src/controller.dart +++ b/lib/src/controller.dart @@ -134,17 +134,15 @@ class MapboxMapController extends ChangeNotifier { }); } - static Future init( - int id, CameraPosition initialCameraPosition, + static MapboxMapController init(int id, CameraPosition initialCameraPosition, {OnStyleLoadedCallback onStyleLoadedCallback, OnMapClickCallback onMapClick, OnMapLongClickCallback onMapLongClick, OnCameraTrackingDismissedCallback onCameraTrackingDismissed, OnCameraTrackingChangedCallback onCameraTrackingChanged, OnCameraIdleCallback onCameraIdle, - OnMapIdleCallback onMapIdle}) async { + OnMapIdleCallback onMapIdle}) { assert(id != null); - await MapboxGlPlatform.getInstance(id).initPlatform(id); return MapboxMapController._(id, initialCameraPosition, onStyleLoadedCallback: onStyleLoadedCallback, onMapClick: onMapClick, @@ -155,6 +153,11 @@ class MapboxMapController extends ChangeNotifier { onMapIdle: onMapIdle); } + static Future initPlatform(int id) async { + assert(id != null); + await MapboxGlPlatform.getInstance(id).initPlatform(id); + } + final OnStyleLoadedCallback onStyleLoadedCallback; final OnMapClickCallback onMapClick; @@ -322,17 +325,17 @@ class MapboxMapController extends ChangeNotifier { /// been notified. Future addSymbol(SymbolOptions options, [Map data]) async { List result = await addSymbols([options], [data]); - + return result.first; } + Future> addSymbols(List options, + [List data]) async { + final List effectiveOptions = + options.map((o) => SymbolOptions.defaultOptions.copyWith(o)).toList(); - Future> addSymbols(List options, [List data]) async { - final List effectiveOptions = options.map( - (o) => SymbolOptions.defaultOptions.copyWith(o) - ).toList(); - - final symbols = await MapboxGlPlatform.getInstance(_id).addSymbols(effectiveOptions, data); + final symbols = await MapboxGlPlatform.getInstance(_id) + .addSymbols(effectiveOptions, data); symbols.forEach((s) => _symbols[s.id] = s); notifyListeners(); return symbols; @@ -385,7 +388,7 @@ class MapboxMapController extends ChangeNotifier { symbols.forEach((s) { assert(_symbols[s.id] == s); }); - + await _removeSymbols(symbols.map((s) => s.id)); notifyListeners(); } @@ -507,8 +510,8 @@ class MapboxMapController extends ChangeNotifier { Future addCircle(CircleOptions options, [Map data]) async { final CircleOptions effectiveOptions = CircleOptions.defaultOptions.copyWith(options); - final circle = - await MapboxGlPlatform.getInstance(_id).addCircle(effectiveOptions, data); + final circle = await MapboxGlPlatform.getInstance(_id) + .addCircle(effectiveOptions, data); _circles[circle.id] = circle; notifyListeners(); return circle; @@ -672,19 +675,17 @@ class MapboxMapController extends ChangeNotifier { } /// Returns the point on the screen that corresponds to a geographical coordinate ([latLng]). The screen location is in screen pixels (not display pixels) relative to the top left of the map (not of the whole screen) - /// + /// /// Note: The resulting x and y coordinates are rounded to [int] on web, on other platforms they may differ very slightly (in the range of about 10^-10) from the actual nearest screen coordinate. /// You therefore might want to round them appropriately, depending on your use case. - /// + /// /// Returns null if [latLng] is not currently visible on the map. - Future toScreenLocation(LatLng latLng) async{ + Future toScreenLocation(LatLng latLng) async { return MapboxGlPlatform.getInstance(_id).toScreenLocation(latLng); } /// Returns the geographic location (as [LatLng]) that corresponds to a point on the screen. The screen location is specified in screen pixels (not display pixels) relative to the top left of the map (not the top left of the whole screen). - Future toLatLng(Point screenLocation) async{ + Future toLatLng(Point screenLocation) async { return MapboxGlPlatform.getInstance(_id).toLatLng(screenLocation); } - - } diff --git a/lib/src/mapbox_map.dart b/lib/src/mapbox_map.dart index fc2c8f9e7..bbc99eea9 100644 --- a/lib/src/mapbox_map.dart +++ b/lib/src/mapbox_map.dart @@ -37,15 +37,14 @@ class MapboxMap extends StatefulWidget { this.onMapIdle, }) : assert(initialCameraPosition != null); - /// If you want to use Mapbox hosted styles and map tiles, you need to provide a Mapbox access token. /// Obtain a free access token on [your Mapbox account page](https://www.mapbox.com/account/access-tokens/). /// The reccommended way is to use this parameter to set your access token, an alternative way to add your access tokens through external files is described in the plugin's wiki on Github. - /// + /// /// Note: You should not use this parameter AND set the access token through external files at the same time, and you should use the same token throughout the entire app. final String accessToken; - /// Please note: you should only add annotations (e.g. symbols or circles) after `onStyleLoadedCallback` has been called. + /// Please note: you should only add annotations (e.g. symbols or circles) after `onStyleLoadedCallback` has been called. final MapCreatedCallback onMapCreated; /// Called when the map style has been successfully loaded and the annotation managers have been enabled. @@ -114,7 +113,7 @@ class MapboxMap extends StatefulWidget { /// when the map tries to turn on the My Location layer. final bool myLocationEnabled; - /// The mode used to let the map's camera follow the device's physical location. + /// The mode used to let the map's camera follow the device's physical location. /// `myLocationEnabled` needs to be true for values other than `MyLocationTrackingMode.None` to work. final MyLocationTrackingMode myLocationTrackingMode; @@ -149,7 +148,7 @@ class MapboxMap extends StatefulWidget { /// Called when the map's camera no longer follows the physical device location, e.g. because the user moved the map final OnCameraTrackingDismissedCallback onCameraTrackingDismissed; - + /// Called when the location tracking mode changes final OnCameraTrackingChangedCallback onCameraTrackingChanged; @@ -212,7 +211,7 @@ class _MapboxMapState extends State { Future onPlatformViewCreated(int id) async { MapboxGlPlatform.addInstance(id, _mapboxGlPlatform); - final MapboxMapController controller = await MapboxMapController.init( + final MapboxMapController controller = MapboxMapController.init( id, widget.initialCameraPosition, onStyleLoadedCallback: widget.onStyleLoadedCallback, onMapClick: widget.onMapClick, @@ -221,6 +220,7 @@ class _MapboxMapState extends State { onCameraTrackingChanged: widget.onCameraTrackingChanged, onCameraIdle: widget.onCameraIdle, onMapIdle: widget.onMapIdle); + await MapboxMapController.initPlatform(id); _controller.complete(controller); if (widget.onMapCreated != null) { widget.onMapCreated(controller); From cd4a48ccbb981d3fc2530dc40ee855f09d0831dd Mon Sep 17 00:00:00 2001 From: Andrea Valenzano Date: Wed, 23 Sep 2020 12:46:13 +0200 Subject: [PATCH 2/2] fix onStyleLoadedCallback called before onMapCreated --- lib/src/mapbox_map.dart | 24 ++++++++++++++++-------- 1 file changed, 16 insertions(+), 8 deletions(-) diff --git a/lib/src/mapbox_map.dart b/lib/src/mapbox_map.dart index bbc99eea9..76e38b572 100644 --- a/lib/src/mapbox_map.dart +++ b/lib/src/mapbox_map.dart @@ -212,14 +212,22 @@ class _MapboxMapState extends State { Future onPlatformViewCreated(int id) async { MapboxGlPlatform.addInstance(id, _mapboxGlPlatform); final MapboxMapController controller = MapboxMapController.init( - id, widget.initialCameraPosition, - onStyleLoadedCallback: widget.onStyleLoadedCallback, - onMapClick: widget.onMapClick, - onMapLongClick: widget.onMapLongClick, - onCameraTrackingDismissed: widget.onCameraTrackingDismissed, - onCameraTrackingChanged: widget.onCameraTrackingChanged, - onCameraIdle: widget.onCameraIdle, - onMapIdle: widget.onMapIdle); + id, + widget.initialCameraPosition, + onStyleLoadedCallback: () { + if (_controller.isCompleted) { + widget.onStyleLoadedCallback(); + } else { + _controller.future.then((_) => widget.onStyleLoadedCallback()); + } + }, + onMapClick: widget.onMapClick, + onMapLongClick: widget.onMapLongClick, + onCameraTrackingDismissed: widget.onCameraTrackingDismissed, + onCameraTrackingChanged: widget.onCameraTrackingChanged, + onCameraIdle: widget.onCameraIdle, + onMapIdle: widget.onMapIdle, + ); await MapboxMapController.initPlatform(id); _controller.complete(controller); if (widget.onMapCreated != null) {