From 1f849aa3b70858ef3b2c777ce39570f85df6cae5 Mon Sep 17 00:00:00 2001 From: Andrea Valenzano Date: Sat, 24 Oct 2020 14:29:25 +0200 Subject: [PATCH] fix onStyleLoadedCallback (#418) * fix onStyleLoadedCallback * fix onStyleLoadedCallback called before onMapCreated Co-authored-by: Tobrun --- lib/src/controller.dart | 23 +++++++++++++---------- lib/src/mapbox_map.dart | 26 +++++++++++++++++--------- 2 files changed, 30 insertions(+), 19 deletions(-) diff --git a/lib/src/controller.dart b/lib/src/controller.dart index 7848eabcc..6b941618e 100644 --- a/lib/src/controller.dart +++ b/lib/src/controller.dart @@ -147,8 +147,7 @@ class MapboxMapController extends ChangeNotifier { }); } - static Future init( - int id, CameraPosition initialCameraPosition, + static MapboxMapController init(int id, CameraPosition initialCameraPosition, {OnStyleLoadedCallback onStyleLoadedCallback, OnMapClickCallback onMapClick, OnUserLocationUpdated onUserLocationUpdated, @@ -156,9 +155,8 @@ class MapboxMapController extends ChangeNotifier { 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, @@ -170,6 +168,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; @@ -533,8 +536,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; @@ -776,17 +779,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 90b842b3f..9e4f8caff 100644 --- a/lib/src/mapbox_map.dart +++ b/lib/src/mapbox_map.dart @@ -38,15 +38,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. @@ -115,7 +114,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; @@ -154,7 +153,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; @@ -217,16 +216,25 @@ class _MapboxMapState extends State { Future onPlatformViewCreated(int id) async { MapboxGlPlatform.addInstance(id, _mapboxGlPlatform); - final MapboxMapController controller = await MapboxMapController.init( - id, widget.initialCameraPosition, - onStyleLoadedCallback: widget.onStyleLoadedCallback, + final MapboxMapController controller = MapboxMapController.init( + id, + widget.initialCameraPosition, + onStyleLoadedCallback: () { + if (_controller.isCompleted) { + widget.onStyleLoadedCallback(); + } else { + _controller.future.then((_) => widget.onStyleLoadedCallback()); + } + }, onMapClick: widget.onMapClick, onUserLocationUpdated: widget.onUserLocationUpdated, onMapLongClick: widget.onMapLongClick, onCameraTrackingDismissed: widget.onCameraTrackingDismissed, onCameraTrackingChanged: widget.onCameraTrackingChanged, onCameraIdle: widget.onCameraIdle, - onMapIdle: widget.onMapIdle); + onMapIdle: widget.onMapIdle + ); + await MapboxMapController.initPlatform(id); _controller.complete(controller); if (widget.onMapCreated != null) { widget.onMapCreated(controller);