From 31d3bbb056c89e113ff5959421e5d6ecf7d6b53c Mon Sep 17 00:00:00 2001 From: m0nac0 <58807793+m0nac0@users.noreply.github.com> Date: Fri, 13 May 2022 12:28:30 +0200 Subject: [PATCH 1/5] Cherry-pick upstream#710 (Get rid of MapboxGlPlatform.getInstance) Co-Authored-By: shroff <502644+shroff@users.noreply.github.com> --- example/lib/generated_plugin_registrant.dart | 1 + lib/src/controller.dart | 214 +++++++----------- lib/src/mapbox_map.dart | 8 +- .../lib/src/mapbox_gl_platform_interface.dart | 10 - .../lib/src/method_channel_mapbox_gl.dart | 2 +- pubspec.lock | 11 +- 6 files changed, 97 insertions(+), 149 deletions(-) diff --git a/example/lib/generated_plugin_registrant.dart b/example/lib/generated_plugin_registrant.dart index 19b27afa..bcb8349f 100644 --- a/example/lib/generated_plugin_registrant.dart +++ b/example/lib/generated_plugin_registrant.dart @@ -2,6 +2,7 @@ // Generated file. Do not edit. // +// ignore_for_file: directives_ordering // ignore_for_file: lines_longer_than_80_chars import 'package:location_web/location_web.dart'; diff --git a/lib/src/controller.dart b/lib/src/controller.dart index d4487a63..f8cc416f 100644 --- a/lib/src/controller.dart +++ b/lib/src/controller.dart @@ -34,69 +34,66 @@ typedef void OnMapIdleCallback(); /// Line tap events can be received by adding callbacks to [onLineTapped]. /// Circle tap events can be received by adding callbacks to [onCircleTapped]. class MaplibreMapController extends ChangeNotifier { - MaplibreMapController._(this._id, CameraPosition initialCameraPosition, - {this.onStyleLoadedCallback, + MaplibreMapController( + {required MapLibreGlPlatform mapboxGlPlatform, + required CameraPosition initialCameraPosition, + this.onStyleLoadedCallback, this.onMapClick, this.onMapLongClick, this.onCameraTrackingDismissed, this.onCameraTrackingChanged, this.onMapIdle, this.onUserLocationUpdated, - this.onCameraIdle}) { + this.onCameraIdle}) + : _mapboxGlPlatform = mapboxGlPlatform { _cameraPosition = initialCameraPosition; - MapLibreGlPlatform.getInstance(_id) - .onInfoWindowTappedPlatform - .add((symbolId) { + _mapboxGlPlatform.onInfoWindowTappedPlatform.add((symbolId) { final symbol = _symbols[symbolId]; if (symbol != null) { onInfoWindowTapped(symbol); } }); - MapLibreGlPlatform.getInstance(_id).onSymbolTappedPlatform.add((symbolId) { + _mapboxGlPlatform.onSymbolTappedPlatform.add((symbolId) { final symbol = _symbols[symbolId]; if (symbol != null) { onSymbolTapped(symbol); } }); - MapLibreGlPlatform.getInstance(_id).onLineTappedPlatform.add((lineId) { + _mapboxGlPlatform.onLineTappedPlatform.add((lineId) { final line = _lines[lineId]; if (line != null) { onLineTapped(line); } }); - MapLibreGlPlatform.getInstance(_id).onCircleTappedPlatform.add((circleId) { + _mapboxGlPlatform.onCircleTappedPlatform.add((circleId) { final circle = _circles[circleId]; if (circle != null) { onCircleTapped(circle); } }); - MapLibreGlPlatform.getInstance(_id).onFillTappedPlatform.add((fillId) { + _mapboxGlPlatform.onFillTappedPlatform.add((fillId) { final fill = _fills[fillId]; if (fill != null) { onFillTapped(fill); } }); - MapLibreGlPlatform.getInstance(_id).onCameraMoveStartedPlatform.add((_) { + _mapboxGlPlatform.onCameraMoveStartedPlatform.add((_) { _isCameraMoving = true; notifyListeners(); }); - MapLibreGlPlatform.getInstance(_id) - .onCameraMovePlatform - .add((cameraPosition) { + _mapboxGlPlatform.onCameraMovePlatform.add((cameraPosition) { _cameraPosition = cameraPosition; notifyListeners(); }); - MapLibreGlPlatform.getInstance(_id) - .onCameraIdlePlatform - .add((cameraPosition) { + _mapboxGlPlatform.onCameraIdlePlatform.add((cameraPosition) { _isCameraMoving = false; if (cameraPosition != null) { _cameraPosition = cameraPosition; @@ -107,77 +104,46 @@ class MaplibreMapController extends ChangeNotifier { notifyListeners(); }); - MapLibreGlPlatform.getInstance(_id).onMapStyleLoadedPlatform.add((_) { + _mapboxGlPlatform.onMapStyleLoadedPlatform.add((_) { if (onStyleLoadedCallback != null) { onStyleLoadedCallback!(); } }); - MapLibreGlPlatform.getInstance(_id).onMapClickPlatform.add((dict) { + _mapboxGlPlatform.onMapClickPlatform.add((dict) { if (onMapClick != null) { onMapClick!(dict['point'], dict['latLng']); } }); - MapLibreGlPlatform.getInstance(_id).onMapLongClickPlatform.add((dict) { + _mapboxGlPlatform.onMapLongClickPlatform.add((dict) { if (onMapLongClick != null) { onMapLongClick!(dict['point'], dict['latLng']); } }); - MapLibreGlPlatform.getInstance(_id) - .onCameraTrackingChangedPlatform - .add((mode) { + _mapboxGlPlatform.onCameraTrackingChangedPlatform.add((mode) { if (onCameraTrackingChanged != null) { onCameraTrackingChanged!(mode); } }); - MapLibreGlPlatform.getInstance(_id) - .onCameraTrackingDismissedPlatform - .add((_) { + _mapboxGlPlatform.onCameraTrackingDismissedPlatform.add((_) { if (onCameraTrackingDismissed != null) { onCameraTrackingDismissed!(); } }); - MapLibreGlPlatform.getInstance(_id).onMapIdlePlatform.add((_) { + _mapboxGlPlatform.onMapIdlePlatform.add((_) { if (onMapIdle != null) { onMapIdle!(); } }); - MapLibreGlPlatform.getInstance(_id) - .onUserLocationUpdatedPlatform - .add((location) { + _mapboxGlPlatform.onUserLocationUpdatedPlatform.add((location) { onUserLocationUpdated?.call(location); }); } - static MaplibreMapController init( - int id, CameraPosition initialCameraPosition, - {OnStyleLoadedCallback? onStyleLoadedCallback, - OnMapClickCallback? onMapClick, - OnUserLocationUpdated? onUserLocationUpdated, - OnMapLongClickCallback? onMapLongClick, - OnCameraTrackingDismissedCallback? onCameraTrackingDismissed, - OnCameraTrackingChangedCallback? onCameraTrackingChanged, - OnCameraIdleCallback? onCameraIdle, - OnMapIdleCallback? onMapIdle}) { - return MaplibreMapController._(id, initialCameraPosition, - onStyleLoadedCallback: onStyleLoadedCallback, - onMapClick: onMapClick, - onUserLocationUpdated: onUserLocationUpdated, - onMapLongClick: onMapLongClick, - onCameraTrackingDismissed: onCameraTrackingDismissed, - onCameraTrackingChanged: onCameraTrackingChanged, - onCameraIdle: onCameraIdle, - onMapIdle: onMapIdle); - } - - static Future initPlatform(int id) async { - await MapLibreGlPlatform.getInstance(id).initPlatform(id); - } - final OnStyleLoadedCallback? onStyleLoadedCallback; final OnMapClickCallback? onMapClick; @@ -241,14 +207,14 @@ class MaplibreMapController extends ChangeNotifier { CameraPosition? get cameraPosition => _cameraPosition; CameraPosition? _cameraPosition; - final int _id; //ignore: unused_field + final MapLibreGlPlatform _mapboxGlPlatform; //ignore: unused_field Widget buildView( Map creationParams, OnPlatformViewCreatedCallback onPlatformViewCreated, Set> gestureRecognizers) { - return MapLibreGlPlatform.getInstance(_id) - .buildView(creationParams, onPlatformViewCreated, gestureRecognizers); + return _mapboxGlPlatform.buildView( + creationParams, onPlatformViewCreated, gestureRecognizers); } /// Updates configuration options of the map user interface. @@ -258,8 +224,7 @@ class MaplibreMapController extends ChangeNotifier { /// /// The returned [Future] completes after listeners have been notified. Future _updateMapOptions(Map optionsUpdate) async { - _cameraPosition = await MapLibreGlPlatform.getInstance(_id) - .updateMapOptions(optionsUpdate); + _cameraPosition = await _mapboxGlPlatform.updateMapOptions(optionsUpdate); notifyListeners(); } @@ -270,7 +235,7 @@ class MaplibreMapController extends ChangeNotifier { /// It returns true if the camera was successfully moved and false if the movement was canceled. /// Note: this currently always returns immediately with a value of null on iOS Future animateCamera(CameraUpdate cameraUpdate) async { - return MapLibreGlPlatform.getInstance(_id).animateCamera(cameraUpdate); + return _mapboxGlPlatform.animateCamera(cameraUpdate); } /// Instantaneously re-position the camera. @@ -281,7 +246,7 @@ class MaplibreMapController extends ChangeNotifier { /// It returns true if the camera was successfully moved and false if the movement was canceled. /// Note: this currently always returns immediately with a value of null on iOS Future moveCamera(CameraUpdate cameraUpdate) async { - return MapLibreGlPlatform.getInstance(_id).moveCamera(cameraUpdate); + return _mapboxGlPlatform.moveCamera(cameraUpdate); } /// Updates user location tracking mode. @@ -290,7 +255,7 @@ class MaplibreMapController extends ChangeNotifier { /// platform side. Future updateMyLocationTrackingMode( MyLocationTrackingMode myLocationTrackingMode) async { - return MapLibreGlPlatform.getInstance(_id) + return _mapboxGlPlatform .updateMyLocationTrackingMode(myLocationTrackingMode); } @@ -299,8 +264,7 @@ class MaplibreMapController extends ChangeNotifier { /// The returned [Future] completes after the change has been made on the /// platform side. Future matchMapLanguageWithDeviceDefault() async { - return MapLibreGlPlatform.getInstance(_id) - .matchMapLanguageWithDeviceDefault(); + return _mapboxGlPlatform.matchMapLanguageWithDeviceDefault(); } /// Updates the distance from the edges of the map view’s frame to the edges @@ -316,8 +280,7 @@ class MaplibreMapController extends ChangeNotifier { /// platform side. Future updateContentInsets(EdgeInsets insets, [bool animated = false]) async { - return MapLibreGlPlatform.getInstance(_id) - .updateContentInsets(insets, animated); + return _mapboxGlPlatform.updateContentInsets(insets, animated); } /// Updates the language of the map labels to match the specified language. @@ -327,7 +290,7 @@ class MaplibreMapController extends ChangeNotifier { /// The returned [Future] completes after the change has been made on the /// platform side. Future setMapLanguage(String language) async { - return MapLibreGlPlatform.getInstance(_id).setMapLanguage(language); + return _mapboxGlPlatform.setMapLanguage(language); } /// Enables or disables the collection of anonymized telemetry data. @@ -335,7 +298,7 @@ class MaplibreMapController extends ChangeNotifier { /// The returned [Future] completes after the change has been made on the /// platform side. Future setTelemetryEnabled(bool enabled) async { - return MapLibreGlPlatform.getInstance(_id).setTelemetryEnabled(enabled); + return _mapboxGlPlatform.setTelemetryEnabled(enabled); } /// Retrieves whether collection of anonymized telemetry data is enabled. @@ -343,7 +306,7 @@ class MaplibreMapController extends ChangeNotifier { /// The returned [Future] completes after the query has been made on the /// platform side. Future getTelemetryEnabled() async { - return MapLibreGlPlatform.getInstance(_id).getTelemetryEnabled(); + return _mapboxGlPlatform.getTelemetryEnabled(); } /// Adds a symbol to the map, configured using the specified custom [options]. @@ -373,8 +336,7 @@ class MaplibreMapController extends ChangeNotifier { final List effectiveOptions = options.map((o) => SymbolOptions.defaultOptions.copyWith(o)).toList(); - final symbols = await MapLibreGlPlatform.getInstance(_id) - .addSymbols(effectiveOptions, data); + final symbols = await _mapboxGlPlatform.addSymbols(effectiveOptions, data); symbols.forEach((s) => _symbols[s.id] = s); notifyListeners(); return symbols; @@ -389,7 +351,8 @@ class MaplibreMapController extends ChangeNotifier { /// The returned [Future] completes once listeners have been notified. Future updateSymbol(Symbol symbol, SymbolOptions changes) async { assert(_symbols[symbol.id] == symbol); - await MapLibreGlPlatform.getInstance(_id).updateSymbol(symbol, changes); + + await _mapboxGlPlatform.updateSymbol(symbol, changes); symbol.options = symbol.options.copyWith(changes); notifyListeners(); } @@ -399,8 +362,7 @@ class MaplibreMapController extends ChangeNotifier { /// In that case this method provides the symbol's actual position, and `symbol.options.geometry` the last programmatically set position. Future getSymbolLatLng(Symbol symbol) async { assert(_symbols[symbol.id] == symbol); - final symbolLatLng = - await MapLibreGlPlatform.getInstance(_id).getSymbolLatLng(symbol); + final symbolLatLng = await _mapboxGlPlatform.getSymbolLatLng(symbol); notifyListeners(); return symbolLatLng; } @@ -440,7 +402,7 @@ class MaplibreMapController extends ChangeNotifier { /// /// The returned [Future] completes once listeners have been notified. Future clearSymbols() async { - await MapLibreGlPlatform.getInstance(_id).removeSymbols(_symbols.keys); + await _mapboxGlPlatform.removeSymbols(_symbols.keys); _symbols.clear(); notifyListeners(); } @@ -451,7 +413,7 @@ class MaplibreMapController extends ChangeNotifier { /// The returned [Future] completes once the symbol has been removed from /// [_symbols]. Future _removeSymbols(Iterable ids) async { - await MapLibreGlPlatform.getInstance(_id).removeSymbols(ids); + await _mapboxGlPlatform.removeSymbols(ids); _symbols.removeWhere((k, s) => ids.contains(k)); } @@ -465,8 +427,7 @@ class MaplibreMapController extends ChangeNotifier { Future addLine(LineOptions options, [Map? data]) async { final LineOptions effectiveOptions = LineOptions.defaultOptions.copyWith(options); - final line = await MapLibreGlPlatform.getInstance(_id) - .addLine(effectiveOptions, data); + final line = await _mapboxGlPlatform.addLine(effectiveOptions, data); _lines[line.id] = line; notifyListeners(); return line; @@ -481,8 +442,7 @@ class MaplibreMapController extends ChangeNotifier { /// been notified. Future> addLines(List options, [List? data]) async { - final lines = - await MapLibreGlPlatform.getInstance(_id).addLines(options, data); + final lines = await _mapboxGlPlatform.addLines(options, data); lines.forEach((l) => _lines[l.id] = l); notifyListeners(); return lines; @@ -497,7 +457,7 @@ class MaplibreMapController extends ChangeNotifier { /// The returned [Future] completes once listeners have been notified. Future updateLine(Line line, LineOptions changes) async { assert(_lines[line.id] == line); - await MapLibreGlPlatform.getInstance(_id).updateLine(line, changes); + await _mapboxGlPlatform.updateLine(line, changes); line.options = line.options.copyWith(changes); notifyListeners(); } @@ -507,8 +467,7 @@ class MaplibreMapController extends ChangeNotifier { /// In that case this method provides the line's actual position, and `line.options.geometry` the last programmatically set position. Future> getLineLatLngs(Line line) async { assert(_lines[line.id] == line); - final lineLatLngs = - await MapLibreGlPlatform.getInstance(_id).getLineLatLngs(line); + final lineLatLngs = await _mapboxGlPlatform.getLineLatLngs(line); notifyListeners(); return lineLatLngs; } @@ -523,7 +482,7 @@ class MaplibreMapController extends ChangeNotifier { Future removeLine(Line line) async { assert(_lines[line.id] == line); - await MapLibreGlPlatform.getInstance(_id).removeLine(line.id); + await _mapboxGlPlatform.removeLine(line.id); _lines.remove(line.id); notifyListeners(); } @@ -539,7 +498,7 @@ class MaplibreMapController extends ChangeNotifier { final ids = lines.where((l) => _lines[l.id] == l).map((l) => l.id); assert(lines.length == ids.length); - await MapLibreGlPlatform.getInstance(_id).removeLines(ids); + await _mapboxGlPlatform.removeLines(ids); ids.forEach((id) => _lines.remove(id)); notifyListeners(); } @@ -552,7 +511,7 @@ class MaplibreMapController extends ChangeNotifier { /// The returned [Future] completes once listeners have been notified. Future clearLines() async { final List lineIds = List.from(_lines.keys); - await MapLibreGlPlatform.getInstance(_id).removeLines(lineIds); + await _mapboxGlPlatform.removeLines(lineIds); _lines.clear(); notifyListeners(); } @@ -567,8 +526,7 @@ class MaplibreMapController extends ChangeNotifier { Future addCircle(CircleOptions options, [Map? data]) async { final CircleOptions effectiveOptions = CircleOptions.defaultOptions.copyWith(options); - final circle = await MapLibreGlPlatform.getInstance(_id) - .addCircle(effectiveOptions, data); + final circle = await _mapboxGlPlatform.addCircle(effectiveOptions, data); _circles[circle.id] = circle; notifyListeners(); return circle; @@ -584,8 +542,7 @@ class MaplibreMapController extends ChangeNotifier { /// been notified. Future> addCircles(List options, [List? data]) async { - final circles = - await MapLibreGlPlatform.getInstance(_id).addCircles(options, data); + final circles = await _mapboxGlPlatform.addCircles(options, data); circles.forEach((c) => _circles[c.id] = c); notifyListeners(); return circles; @@ -600,7 +557,8 @@ class MaplibreMapController extends ChangeNotifier { /// The returned [Future] completes once listeners have been notified. Future updateCircle(Circle circle, CircleOptions changes) async { assert(_circles[circle.id] == circle); - await MapLibreGlPlatform.getInstance(_id).updateCircle(circle, changes); + await _mapboxGlPlatform.updateCircle(circle, changes); + circle.options = circle.options.copyWith(changes); notifyListeners(); } @@ -609,8 +567,7 @@ class MaplibreMapController extends ChangeNotifier { /// In that case this method provides the circle's actual position, and `circle.options.geometry` the last programmatically set position. Future getCircleLatLng(Circle circle) async { assert(_circles[circle.id] == circle); - final circleLatLng = - await MapLibreGlPlatform.getInstance(_id).getCircleLatLng(circle); + final circleLatLng = await _mapboxGlPlatform.getCircleLatLng(circle); notifyListeners(); return circleLatLng; } @@ -625,7 +582,7 @@ class MaplibreMapController extends ChangeNotifier { Future removeCircle(Circle circle) async { assert(_circles[circle.id] == circle); - await MapLibreGlPlatform.getInstance(_id).removeCircle(circle.id); + await _mapboxGlPlatform.removeCircle(circle.id); _circles.remove(circle.id); notifyListeners(); @@ -642,7 +599,7 @@ class MaplibreMapController extends ChangeNotifier { final ids = circles.where((c) => _circles[c.id] == c).map((c) => c.id); assert(circles.length == ids.length); - await MapLibreGlPlatform.getInstance(_id).removeCircles(ids); + await _mapboxGlPlatform.removeCircles(ids); ids.forEach((id) => _circles.remove(id)); notifyListeners(); } @@ -654,7 +611,7 @@ class MaplibreMapController extends ChangeNotifier { /// /// The returned [Future] completes once listeners have been notified. Future clearCircles() async { - await MapLibreGlPlatform.getInstance(_id).removeCircles(_circles.keys); + await _mapboxGlPlatform.removeCircles(_circles.keys); _circles.clear(); notifyListeners(); @@ -670,8 +627,7 @@ class MaplibreMapController extends ChangeNotifier { Future addFill(FillOptions options, [Map? data]) async { final FillOptions effectiveOptions = FillOptions.defaultOptions.copyWith(options); - final fill = await MapLibreGlPlatform.getInstance(_id) - .addFill(effectiveOptions, data); + final fill = await _mapboxGlPlatform.addFill(effectiveOptions, data); _fills[fill.id] = fill; notifyListeners(); return fill; @@ -687,8 +643,7 @@ class MaplibreMapController extends ChangeNotifier { /// been notified. Future> addFills(List options, [List? data]) async { - final circles = - await MapLibreGlPlatform.getInstance(_id).addFills(options, data); + final circles = await _mapboxGlPlatform.addFills(options, data); circles.forEach((f) => _fills[f.id] = f); notifyListeners(); return circles; @@ -703,7 +658,7 @@ class MaplibreMapController extends ChangeNotifier { /// The returned [Future] completes once listeners have been notified. Future updateFill(Fill fill, FillOptions changes) async { assert(_fills[fill.id] == fill); - await MapLibreGlPlatform.getInstance(_id).updateFill(fill, changes); + await _mapboxGlPlatform.updateFill(fill, changes); fill.options = fill.options.copyWith(changes); notifyListeners(); } @@ -715,7 +670,8 @@ class MaplibreMapController extends ChangeNotifier { /// /// The returned [Future] completes once listeners have been notified. Future clearFills() async { - await MapLibreGlPlatform.getInstance(_id).removeFills(_fills.keys); + await _mapboxGlPlatform.removeFills(_fills.keys); + _fills.clear(); notifyListeners(); } @@ -729,7 +685,7 @@ class MaplibreMapController extends ChangeNotifier { /// The returned [Future] completes once listeners have been notified. Future removeFill(Fill fill) async { assert(_fills[fill.id] == fill); - await MapLibreGlPlatform.getInstance(_id).removeFill(fill.id); + await _mapboxGlPlatform.removeFill(fill.id); _fills.remove(fill.id); notifyListeners(); @@ -746,37 +702,36 @@ class MaplibreMapController extends ChangeNotifier { final ids = fills.where((f) => _fills[f.id] == f).map((f) => f.id); assert(fills.length == ids.length); - await MapLibreGlPlatform.getInstance(_id).removeFills(ids); + await _mapboxGlPlatform.removeFills(ids); ids.forEach((id) => _fills.remove(id)); notifyListeners(); } Future queryRenderedFeatures( Point point, List layerIds, List? filter) async { - return MapLibreGlPlatform.getInstance(_id) - .queryRenderedFeatures(point, layerIds, filter); + return _mapboxGlPlatform.queryRenderedFeatures(point, layerIds, filter); } Future queryRenderedFeaturesInRect( Rect rect, List layerIds, String? filter) async { - return MapLibreGlPlatform.getInstance(_id) - .queryRenderedFeaturesInRect(rect, layerIds, filter); + return _mapboxGlPlatform.queryRenderedFeaturesInRect( + rect, layerIds, filter); } Future invalidateAmbientCache() async { - return MapLibreGlPlatform.getInstance(_id).invalidateAmbientCache(); + return _mapboxGlPlatform.invalidateAmbientCache(); } /// Get last my location /// /// Return last latlng, nullable Future requestMyLocationLatLng() async { - return MapLibreGlPlatform.getInstance(_id).requestMyLocationLatLng(); + return _mapboxGlPlatform.requestMyLocationLatLng(); } /// This method returns the boundaries of the region currently displayed in the map. Future getVisibleRegion() async { - return MapLibreGlPlatform.getInstance(_id).getVisibleRegion(); + return _mapboxGlPlatform.getVisibleRegion(); } /// Adds an image to the style currently displayed in the map, so that it can later be referred to by the provided name. @@ -815,59 +770,55 @@ class MaplibreMapController extends ChangeNotifier { /// } /// ``` Future addImage(String name, Uint8List bytes, [bool sdf = false]) { - return MapLibreGlPlatform.getInstance(_id).addImage(name, bytes, sdf); + return _mapboxGlPlatform.addImage(name, bytes, sdf); } /// For more information on what this does, see https://docs.mapbox.com/help/troubleshooting/optimize-map-label-placement/#label-collision Future setSymbolIconAllowOverlap(bool enable) async { - await MapLibreGlPlatform.getInstance(_id).setSymbolIconAllowOverlap(enable); + await _mapboxGlPlatform.setSymbolIconAllowOverlap(enable); } /// For more information on what this does, see https://docs.mapbox.com/help/troubleshooting/optimize-map-label-placement/#label-collision Future setSymbolIconIgnorePlacement(bool enable) async { - await MapLibreGlPlatform.getInstance(_id) - .setSymbolIconIgnorePlacement(enable); + await _mapboxGlPlatform.setSymbolIconIgnorePlacement(enable); } /// For more information on what this does, see https://docs.mapbox.com/help/troubleshooting/optimize-map-label-placement/#label-collision Future setSymbolTextAllowOverlap(bool enable) async { - await MapLibreGlPlatform.getInstance(_id).setSymbolTextAllowOverlap(enable); + await _mapboxGlPlatform.setSymbolTextAllowOverlap(enable); } /// For more information on what this does, see https://docs.mapbox.com/help/troubleshooting/optimize-map-label-placement/#label-collision Future setSymbolTextIgnorePlacement(bool enable) async { - await MapLibreGlPlatform.getInstance(_id) - .setSymbolTextIgnorePlacement(enable); + await _mapboxGlPlatform.setSymbolTextIgnorePlacement(enable); } /// Adds an image source to the style currently displayed in the map, so that it can later be referred to by the provided id. Future addImageSource( String imageSourceId, Uint8List bytes, LatLngQuad coordinates) { - return MapLibreGlPlatform.getInstance(_id) - .addImageSource(imageSourceId, bytes, coordinates); + return _mapboxGlPlatform.addImageSource(imageSourceId, bytes, coordinates); } /// Removes previously added image source by id Future removeImageSource(String imageSourceId) { - return MapLibreGlPlatform.getInstance(_id).removeImageSource(imageSourceId); + return _mapboxGlPlatform.removeImageSource(imageSourceId); } /// Adds a Mapbox style layer to the map's style at render time. Future addLayer(String imageLayerId, String imageSourceId) { - return MapLibreGlPlatform.getInstance(_id) - .addLayer(imageLayerId, imageSourceId); + return _mapboxGlPlatform.addLayer(imageLayerId, imageSourceId); } /// Adds a Mapbox style layer below the layer provided with belowLayerId to the map's style at render time, Future addLayerBelow( String imageLayerId, String imageSourceId, String belowLayerId) { - return MapLibreGlPlatform.getInstance(_id) - .addLayerBelow(imageLayerId, imageSourceId, belowLayerId); + return _mapboxGlPlatform.addLayerBelow( + imageLayerId, imageSourceId, belowLayerId); } /// Removes a Mapbox style layer Future removeLayer(String imageLayerId) { - return MapLibreGlPlatform.getInstance(_id).removeLayer(imageLayerId); + return _mapboxGlPlatform.removeLayer(imageLayerId); } /// 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) @@ -877,22 +828,21 @@ class MaplibreMapController extends ChangeNotifier { /// /// Returns null if [latLng] is not currently visible on the map. Future toScreenLocation(LatLng latLng) async { - return MapLibreGlPlatform.getInstance(_id).toScreenLocation(latLng); + return _mapboxGlPlatform.toScreenLocation(latLng); } Future> toScreenLocationBatch(Iterable latLngs) async { - return MapLibreGlPlatform.getInstance(_id).toScreenLocationBatch(latLngs); + return _mapboxGlPlatform.toScreenLocationBatch(latLngs); } /// 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 { - return MapLibreGlPlatform.getInstance(_id).toLatLng(screenLocation); + return _mapboxGlPlatform.toLatLng(screenLocation); } /// Returns the distance spanned by one pixel at the specified [latitude] and current zoom level. /// The distance between pixels decreases as the latitude approaches the poles. This relationship parallels the relationship between longitudinal coordinates at different latitudes. Future getMetersPerPixelAtLatitude(double latitude) async { - return MapLibreGlPlatform.getInstance(_id) - .getMetersPerPixelAtLatitude(latitude); + return _mapboxGlPlatform.getMetersPerPixelAtLatitude(latitude); } } diff --git a/lib/src/mapbox_map.dart b/lib/src/mapbox_map.dart index 3ef3bee3..35d08e98 100644 --- a/lib/src/mapbox_map.dart +++ b/lib/src/mapbox_map.dart @@ -246,9 +246,9 @@ class _MaplibreMapState extends State { } Future onPlatformViewCreated(int id) async { - MapLibreGlPlatform.addInstance(id, _mapboxGlPlatform); - final MaplibreMapController controller = MaplibreMapController.init( - id, widget.initialCameraPosition, onStyleLoadedCallback: () { + + final MaplibreMapController controller = MaplibreMapController( + mapboxGlPlatform: _mapboxGlPlatform, initialCameraPosition: widget.initialCameraPosition, onStyleLoadedCallback: () { if (_controller.isCompleted) { widget.onStyleLoadedCallback!(); } else { @@ -262,7 +262,7 @@ class _MaplibreMapState extends State { onCameraTrackingChanged: widget.onCameraTrackingChanged, onCameraIdle: widget.onCameraIdle, onMapIdle: widget.onMapIdle); - await MaplibreMapController.initPlatform(id); + await _mapboxGlPlatform.initPlatform(id); _controller.complete(controller); if (widget.onMapCreated != null) { widget.onMapCreated!(controller); diff --git a/maplibre_gl_platform_interface/lib/src/mapbox_gl_platform_interface.dart b/maplibre_gl_platform_interface/lib/src/mapbox_gl_platform_interface.dart index d655b33e..63651caf 100644 --- a/maplibre_gl_platform_interface/lib/src/mapbox_gl_platform_interface.dart +++ b/maplibre_gl_platform_interface/lib/src/mapbox_gl_platform_interface.dart @@ -15,16 +15,6 @@ abstract class MapLibreGlPlatform { static MapLibreGlPlatform Function() createInstance = () => MethodChannelMaplibreGl(); - static Map _instances = {}; - - static void addInstance(int id, MapLibreGlPlatform platform) { - _instances[id] = platform; - } - - static MapLibreGlPlatform getInstance(int id) { - return _instances[id]!; - } - final onInfoWindowTappedPlatform = ArgumentCallbacks(); final onSymbolTappedPlatform = ArgumentCallbacks(); diff --git a/maplibre_gl_platform_interface/lib/src/method_channel_mapbox_gl.dart b/maplibre_gl_platform_interface/lib/src/method_channel_mapbox_gl.dart index e4934cab..1955077c 100644 --- a/maplibre_gl_platform_interface/lib/src/method_channel_mapbox_gl.dart +++ b/maplibre_gl_platform_interface/lib/src/method_channel_mapbox_gl.dart @@ -114,8 +114,8 @@ class MethodChannelMaplibreGl extends MapLibreGlPlatform { @override Future initPlatform(int id) async { _channel = MethodChannel('plugins.flutter.io/mapbox_maps_$id'); - await _channel.invokeMethod('map#waitForMap'); _channel.setMethodCallHandler(_handleMethodCall); + await _channel.invokeMethod('map#waitForMap'); } @override diff --git a/pubspec.lock b/pubspec.lock index 0696a6bd..98940ee5 100644 --- a/pubspec.lock +++ b/pubspec.lock @@ -66,14 +66,21 @@ packages: path: maplibre_gl_platform_interface relative: true source: path - version: "0.14.0" + version: "0.15.0" maplibre_gl_web: dependency: "direct main" description: path: maplibre_gl_web relative: true source: path - version: "0.14.0" + version: "0.15.0" + 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: From feb4060c6f41924bec72ca3e088437b597289c8c Mon Sep 17 00:00:00 2001 From: m0nac0 <58807793+m0nac0@users.noreply.github.com> Date: Fri, 13 May 2022 12:31:14 +0200 Subject: [PATCH 2/5] fix formatting --- lib/src/mapbox_map.dart | 17 +++++++++-------- 1 file changed, 9 insertions(+), 8 deletions(-) diff --git a/lib/src/mapbox_map.dart b/lib/src/mapbox_map.dart index 35d08e98..0968077f 100644 --- a/lib/src/mapbox_map.dart +++ b/lib/src/mapbox_map.dart @@ -246,15 +246,16 @@ class _MaplibreMapState extends State { } Future onPlatformViewCreated(int id) async { - final MaplibreMapController controller = MaplibreMapController( - mapboxGlPlatform: _mapboxGlPlatform, initialCameraPosition: widget.initialCameraPosition, onStyleLoadedCallback: () { - if (_controller.isCompleted) { - widget.onStyleLoadedCallback!(); - } else { - _controller.future.then((_) => widget.onStyleLoadedCallback!()); - } - }, + mapboxGlPlatform: _mapboxGlPlatform, + initialCameraPosition: widget.initialCameraPosition, + onStyleLoadedCallback: () { + if (_controller.isCompleted) { + widget.onStyleLoadedCallback!(); + } else { + _controller.future.then((_) => widget.onStyleLoadedCallback!()); + } + }, onMapClick: widget.onMapClick, onUserLocationUpdated: widget.onUserLocationUpdated, onMapLongClick: widget.onMapLongClick, From c45164498153d2ffcaecfe81ae143df88b0e9341 Mon Sep 17 00:00:00 2001 From: m0nac0 <58807793+m0nac0@users.noreply.github.com> Date: Fri, 13 May 2022 13:18:40 +0200 Subject: [PATCH 3/5] Cherry-pick upstream#852 (Android embedding fixes - migrate to maven) https://github.com/flutter-mapbox-gl/maps/pull/852 plus a temporary fix for the platform dependency (compile time error) Co-Authored-By: Felix Horvat <24698503+felix-ht@users.noreply.github.com> --- android/build.gradle | 3 +-- example/android/app/src/main/AndroidManifest.xml | 2 +- .../mapboxglexample/EmbeddingV1Activity.java | 16 ---------------- example/android/build.gradle | 4 ++-- example/pubspec.yaml | 2 ++ pubspec.lock | 7 +++++++ 6 files changed, 13 insertions(+), 21 deletions(-) delete mode 100644 example/android/app/src/main/java/com/mapbox/mapboxglexample/EmbeddingV1Activity.java diff --git a/android/build.gradle b/android/build.gradle index 2584c716..f3e34827 100644 --- a/android/build.gradle +++ b/android/build.gradle @@ -4,7 +4,7 @@ version '1.0-SNAPSHOT' buildscript { repositories { google() - jcenter() + mavenCentral() } dependencies { @@ -15,7 +15,6 @@ buildscript { rootProject.allprojects { repositories { google() - jcenter() mavenCentral() } } diff --git a/example/android/app/src/main/AndroidManifest.xml b/example/android/app/src/main/AndroidManifest.xml index 32dd1347..865c8648 100644 --- a/example/android/app/src/main/AndroidManifest.xml +++ b/example/android/app/src/main/AndroidManifest.xml @@ -5,7 +5,7 @@ diff --git a/example/android/app/src/main/java/com/mapbox/mapboxglexample/EmbeddingV1Activity.java b/example/android/app/src/main/java/com/mapbox/mapboxglexample/EmbeddingV1Activity.java deleted file mode 100644 index 790679e4..00000000 --- a/example/android/app/src/main/java/com/mapbox/mapboxglexample/EmbeddingV1Activity.java +++ /dev/null @@ -1,16 +0,0 @@ -package com.mapbox.mapboxglexample; - -import android.os.Bundle; - -import com.mapbox.mapboxgl.MapboxMapsPlugin; - -import io.flutter.app.FlutterActivity; - -public class EmbeddingV1Activity extends FlutterActivity { - - @Override - protected void onCreate(Bundle savedInstanceState) { - super.onCreate(savedInstanceState); - MapboxMapsPlugin.registerWith(registrarFor("com.mapbox.mapboxgl.MapboxMapsPlugin")); - } -} \ No newline at end of file diff --git a/example/android/build.gradle b/example/android/build.gradle index 041ce013..d66a442e 100644 --- a/example/android/build.gradle +++ b/example/android/build.gradle @@ -1,7 +1,7 @@ buildscript { repositories { google() - jcenter() + mavenCentral() } dependencies { @@ -12,7 +12,7 @@ buildscript { allprojects { repositories { google() - jcenter() + mavenCentral() } } diff --git a/example/pubspec.yaml b/example/pubspec.yaml index b47f014e..256f7b24 100644 --- a/example/pubspec.yaml +++ b/example/pubspec.yaml @@ -17,6 +17,8 @@ dependencies: path_provider: ^2.0.0 http: ^0.13.0 collection: ^1.0.0 + # temporary fix: + platform: ^3.1.0 dependency_overrides: maplibre_gl_platform_interface: diff --git a/pubspec.lock b/pubspec.lock index 98940ee5..774fc336 100644 --- a/pubspec.lock +++ b/pubspec.lock @@ -102,6 +102,13 @@ packages: url: "https://pub.dartlang.org" source: hosted version: "4.1.0" + platform: + dependency: "direct main" + description: + name: platform + url: "https://pub.dartlang.org" + source: hosted + version: "3.1.0" sky_engine: dependency: transitive description: flutter From b74ed9a4a4168c28ba6ac658661355c441157b19 Mon Sep 17 00:00:00 2001 From: m0nac0 <58807793+m0nac0@users.noreply.github.com> Date: Fri, 13 May 2022 13:30:22 +0200 Subject: [PATCH 4/5] Update flutter version --- .github/workflows/flutter_ci.yml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.github/workflows/flutter_ci.yml b/.github/workflows/flutter_ci.yml index 24f40a2e..d1462d85 100644 --- a/.github/workflows/flutter_ci.yml +++ b/.github/workflows/flutter_ci.yml @@ -4,7 +4,7 @@ on: [push, pull_request, workflow_dispatch] env: FLUTTER_CHANNEL: 'stable' - FLUTTER_VERSION: '2.5.3' + FLUTTER_VERSION: '2.10.5' jobs: format: From ceefae4fb314a5a69d793af0c9e99e6a60c4230a Mon Sep 17 00:00:00 2001 From: m0nac0 <58807793+m0nac0@users.noreply.github.com> Date: Fri, 13 May 2022 17:12:36 +0200 Subject: [PATCH 5/5] cherry-pick upstream#904 (fix android build issues) Co-Authored-By: Felix Horvat <24698503+felix-ht@users.noreply.github.com> --- android/build.gradle | 4 +++- example/android/app/build.gradle | 5 +++-- example/android/app/src/main/AndroidManifest.xml | 1 + 3 files changed, 7 insertions(+), 3 deletions(-) diff --git a/android/build.gradle b/android/build.gradle index f3e34827..eb98adf9 100644 --- a/android/build.gradle +++ b/android/build.gradle @@ -22,7 +22,9 @@ rootProject.allprojects { apply plugin: 'com.android.library' android { - compileSdkVersion 29 + + compileSdkVersion 31 + ndkVersion "20.1.5948944" defaultConfig { minSdkVersion 20 diff --git a/example/android/app/build.gradle b/example/android/app/build.gradle index 6d6819d3..2c6f16a9 100644 --- a/example/android/app/build.gradle +++ b/example/android/app/build.gradle @@ -25,7 +25,8 @@ apply plugin: 'com.android.application' apply from: "$flutterRoot/packages/flutter_tools/gradle/flutter.gradle" android { - compileSdkVersion 29 + compileSdkVersion 31 + ndkVersion "20.1.5948944" lintOptions { disable 'InvalidPackage' @@ -35,7 +36,7 @@ android { // TODO: Specify your own unique Application ID (https://developer.android.com/studio/build/application-id.html). applicationId "com.mapbox.mapboxglexample" minSdkVersion 20 - targetSdkVersion 28 + targetSdkVersion 31 versionCode flutterVersionCode.toInteger() versionName flutterVersionName testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner" diff --git a/example/android/app/src/main/AndroidManifest.xml b/example/android/app/src/main/AndroidManifest.xml index 865c8648..a8a23b43 100644 --- a/example/android/app/src/main/AndroidManifest.xml +++ b/example/android/app/src/main/AndroidManifest.xml @@ -11,6 +11,7 @@