Skip to content

Commit

Permalink
fix onStyleLoadedCallback (#418)
Browse files Browse the repository at this point in the history
* fix onStyleLoadedCallback

* fix onStyleLoadedCallback called before onMapCreated

Co-authored-by: Tobrun <tobrun.van.nuland@gmail.com>
  • Loading branch information
andrea689 and tobrun authored Oct 24, 2020
1 parent 8f9838f commit 1f849aa
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 19 deletions.
23 changes: 13 additions & 10 deletions lib/src/controller.dart
Original file line number Diff line number Diff line change
Expand Up @@ -147,18 +147,16 @@ class MapboxMapController extends ChangeNotifier {
});
}

static Future<MapboxMapController> init(
int id, CameraPosition initialCameraPosition,
static MapboxMapController init(int id, CameraPosition initialCameraPosition,
{OnStyleLoadedCallback onStyleLoadedCallback,
OnMapClickCallback onMapClick,
OnUserLocationUpdated onUserLocationUpdated,
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,
Expand All @@ -170,6 +168,11 @@ class MapboxMapController extends ChangeNotifier {
onMapIdle: onMapIdle);
}

static Future<void> initPlatform(int id) async {
assert(id != null);
await MapboxGlPlatform.getInstance(id).initPlatform(id);
}

final OnStyleLoadedCallback onStyleLoadedCallback;

final OnMapClickCallback onMapClick;
Expand Down Expand Up @@ -533,8 +536,8 @@ class MapboxMapController extends ChangeNotifier {
Future<Circle> 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;
Expand Down Expand Up @@ -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<Point> toScreenLocation(LatLng latLng) async{
Future<Point> 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<LatLng> toLatLng(Point screenLocation) async{
Future<LatLng> toLatLng(Point screenLocation) async {
return MapboxGlPlatform.getInstance(_id).toLatLng(screenLocation);
}

Expand Down
26 changes: 17 additions & 9 deletions lib/src/mapbox_map.dart
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -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;

Expand Down Expand Up @@ -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;

Expand Down Expand Up @@ -217,16 +216,25 @@ class _MapboxMapState extends State<MapboxMap> {

Future<void> 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);
Expand Down

0 comments on commit 1f849aa

Please sign in to comment.