From b28e8c6194da448cf7c26514e1331e401010f905 Mon Sep 17 00:00:00 2001 From: shroff Date: Sun, 24 Oct 2021 22:38:48 +0530 Subject: [PATCH] Get rid of MapboxGlPlatform.getInstance (#710) Keeping a reference to this in a static map is unnecessary and causes a memory leak. --- example/lib/generated_plugin_registrant.dart | 1 + lib/src/controller.dart | 228 +++++++----------- lib/src/mapbox_map.dart | 19 +- .../lib/src/mapbox_gl_platform_interface.dart | 10 - .../lib/src/method_channel_mapbox_gl.dart | 2 +- 5 files changed, 95 insertions(+), 165 deletions(-) diff --git a/example/lib/generated_plugin_registrant.dart b/example/lib/generated_plugin_registrant.dart index 9338cdbe8..1bf72ac09 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 95e816809..c91402d01 100644 --- a/lib/src/controller.dart +++ b/lib/src/controller.dart @@ -36,70 +36,67 @@ 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 MapboxMapController extends ChangeNotifier { - MapboxMapController._(this._id, CameraPosition initialCameraPosition, - {this.onStyleLoadedCallback, - this.onMapClick, - this.onMapLongClick, - this.onAttributionClick, - this.onCameraTrackingDismissed, - this.onCameraTrackingChanged, - this.onMapIdle, - this.onUserLocationUpdated, - this.onCameraIdle}) { + MapboxMapController({ + required MapboxGlPlatform mapboxGlPlatform, + required CameraPosition initialCameraPosition, + this.onStyleLoadedCallback, + this.onMapClick, + this.onMapLongClick, + this.onAttributionClick, + this.onCameraTrackingDismissed, + this.onCameraTrackingChanged, + this.onMapIdle, + this.onUserLocationUpdated, + this.onCameraIdle, + }) : _mapboxGlPlatform = mapboxGlPlatform { _cameraPosition = initialCameraPosition; - MapboxGlPlatform.getInstance(_id) - .onInfoWindowTappedPlatform - .add((symbolId) { + _mapboxGlPlatform.onInfoWindowTappedPlatform.add((symbolId) { final symbol = _symbols[symbolId]; if (symbol != null) { onInfoWindowTapped(symbol); } }); - MapboxGlPlatform.getInstance(_id).onSymbolTappedPlatform.add((symbolId) { + _mapboxGlPlatform.onSymbolTappedPlatform.add((symbolId) { final symbol = _symbols[symbolId]; if (symbol != null) { onSymbolTapped(symbol); } }); - MapboxGlPlatform.getInstance(_id).onLineTappedPlatform.add((lineId) { + _mapboxGlPlatform.onLineTappedPlatform.add((lineId) { final line = _lines[lineId]; if (line != null) { onLineTapped(line); } }); - MapboxGlPlatform.getInstance(_id).onCircleTappedPlatform.add((circleId) { + _mapboxGlPlatform.onCircleTappedPlatform.add((circleId) { final circle = _circles[circleId]; if (circle != null) { onCircleTapped(circle); } }); - MapboxGlPlatform.getInstance(_id).onFillTappedPlatform.add((fillId) { + _mapboxGlPlatform.onFillTappedPlatform.add((fillId) { final fill = _fills[fillId]; if (fill != null) { onFillTapped(fill); } }); - MapboxGlPlatform.getInstance(_id).onCameraMoveStartedPlatform.add((_) { + _mapboxGlPlatform.onCameraMoveStartedPlatform.add((_) { _isCameraMoving = true; notifyListeners(); }); - MapboxGlPlatform.getInstance(_id) - .onCameraMovePlatform - .add((cameraPosition) { + _mapboxGlPlatform.onCameraMovePlatform.add((cameraPosition) { _cameraPosition = cameraPosition; notifyListeners(); }); - MapboxGlPlatform.getInstance(_id) - .onCameraIdlePlatform - .add((cameraPosition) { + _mapboxGlPlatform.onCameraIdlePlatform.add((cameraPosition) { _isCameraMoving = false; if (cameraPosition != null) { _cameraPosition = cameraPosition; @@ -110,84 +107,52 @@ class MapboxMapController extends ChangeNotifier { notifyListeners(); }); - MapboxGlPlatform.getInstance(_id).onMapStyleLoadedPlatform.add((_) { + _mapboxGlPlatform.onMapStyleLoadedPlatform.add((_) { if (onStyleLoadedCallback != null) { onStyleLoadedCallback!(); } }); - MapboxGlPlatform.getInstance(_id).onMapClickPlatform.add((dict) { + _mapboxGlPlatform.onMapClickPlatform.add((dict) { if (onMapClick != null) { onMapClick!(dict['point'], dict['latLng']); } }); - MapboxGlPlatform.getInstance(_id).onMapLongClickPlatform.add((dict) { + _mapboxGlPlatform.onMapLongClickPlatform.add((dict) { if (onMapLongClick != null) { onMapLongClick!(dict['point'], dict['latLng']); } }); - MapboxGlPlatform.getInstance(_id).onAttributionClickPlatform.add((_) { + _mapboxGlPlatform.onAttributionClickPlatform.add((_) { if (onAttributionClick != null) { onAttributionClick!(); } }); - MapboxGlPlatform.getInstance(_id) - .onCameraTrackingChangedPlatform - .add((mode) { + _mapboxGlPlatform.onCameraTrackingChangedPlatform.add((mode) { if (onCameraTrackingChanged != null) { onCameraTrackingChanged!(mode); } }); - MapboxGlPlatform.getInstance(_id) - .onCameraTrackingDismissedPlatform - .add((_) { + _mapboxGlPlatform.onCameraTrackingDismissedPlatform.add((_) { if (onCameraTrackingDismissed != null) { onCameraTrackingDismissed!(); } }); - MapboxGlPlatform.getInstance(_id).onMapIdlePlatform.add((_) { + _mapboxGlPlatform.onMapIdlePlatform.add((_) { if (onMapIdle != null) { onMapIdle!(); } }); - MapboxGlPlatform.getInstance(_id) - .onUserLocationUpdatedPlatform - .add((location) { + _mapboxGlPlatform.onUserLocationUpdatedPlatform.add((location) { onUserLocationUpdated?.call(location); }); } - static MapboxMapController init(int id, CameraPosition initialCameraPosition, - {OnStyleLoadedCallback? onStyleLoadedCallback, - OnMapClickCallback? onMapClick, - OnUserLocationUpdated? onUserLocationUpdated, - OnMapLongClickCallback? onMapLongClick, - OnAttributionClickCallback? onAttributionClick, - OnCameraTrackingDismissedCallback? onCameraTrackingDismissed, - OnCameraTrackingChangedCallback? onCameraTrackingChanged, - OnCameraIdleCallback? onCameraIdle, - OnMapIdleCallback? onMapIdle}) { - return MapboxMapController._(id, initialCameraPosition, - onStyleLoadedCallback: onStyleLoadedCallback, - onMapClick: onMapClick, - onUserLocationUpdated: onUserLocationUpdated, - onMapLongClick: onMapLongClick, - onAttributionClick: onAttributionClick, - onCameraTrackingDismissed: onCameraTrackingDismissed, - onCameraTrackingChanged: onCameraTrackingChanged, - onCameraIdle: onCameraIdle, - onMapIdle: onMapIdle); - } - - static Future initPlatform(int id) async { - await MapboxGlPlatform.getInstance(id).initPlatform(id); - } - final OnStyleLoadedCallback? onStyleLoadedCallback; final OnMapClickCallback? onMapClick; @@ -252,14 +217,14 @@ class MapboxMapController extends ChangeNotifier { CameraPosition? get cameraPosition => _cameraPosition; CameraPosition? _cameraPosition; - final int _id; //ignore: unused_field + final MapboxGlPlatform _mapboxGlPlatform; //ignore: unused_field Widget buildView( Map creationParams, OnPlatformViewCreatedCallback onPlatformViewCreated, Set> gestureRecognizers) { - return MapboxGlPlatform.getInstance(_id) - .buildView(creationParams, onPlatformViewCreated, gestureRecognizers); + return _mapboxGlPlatform.buildView( + creationParams, onPlatformViewCreated, gestureRecognizers); } /// Updates configuration options of the map user interface. @@ -269,8 +234,7 @@ class MapboxMapController extends ChangeNotifier { /// /// The returned [Future] completes after listeners have been notified. Future _updateMapOptions(Map optionsUpdate) async { - _cameraPosition = - await MapboxGlPlatform.getInstance(_id).updateMapOptions(optionsUpdate); + _cameraPosition = await _mapboxGlPlatform.updateMapOptions(optionsUpdate); notifyListeners(); } @@ -281,7 +245,7 @@ class MapboxMapController 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 MapboxGlPlatform.getInstance(_id).animateCamera(cameraUpdate); + return _mapboxGlPlatform.animateCamera(cameraUpdate); } /// Instantaneously re-position the camera. @@ -292,7 +256,7 @@ class MapboxMapController 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 MapboxGlPlatform.getInstance(_id).moveCamera(cameraUpdate); + return _mapboxGlPlatform.moveCamera(cameraUpdate); } /// Updates user location tracking mode. @@ -301,7 +265,7 @@ class MapboxMapController extends ChangeNotifier { /// platform side. Future updateMyLocationTrackingMode( MyLocationTrackingMode myLocationTrackingMode) async { - return MapboxGlPlatform.getInstance(_id) + return _mapboxGlPlatform .updateMyLocationTrackingMode(myLocationTrackingMode); } @@ -310,8 +274,7 @@ class MapboxMapController extends ChangeNotifier { /// The returned [Future] completes after the change has been made on the /// platform side. Future matchMapLanguageWithDeviceDefault() async { - return MapboxGlPlatform.getInstance(_id) - .matchMapLanguageWithDeviceDefault(); + return _mapboxGlPlatform.matchMapLanguageWithDeviceDefault(); } /// Updates the distance from the edges of the map view’s frame to the edges @@ -327,8 +290,7 @@ class MapboxMapController extends ChangeNotifier { /// platform side. Future updateContentInsets(EdgeInsets insets, [bool animated = false]) async { - return MapboxGlPlatform.getInstance(_id) - .updateContentInsets(insets, animated); + return _mapboxGlPlatform.updateContentInsets(insets, animated); } /// Updates the language of the map labels to match the specified language. @@ -337,7 +299,7 @@ class MapboxMapController extends ChangeNotifier { /// The returned [Future] completes after the change has been made on the /// platform side. Future setMapLanguage(String language) async { - return MapboxGlPlatform.getInstance(_id).setMapLanguage(language); + return _mapboxGlPlatform.setMapLanguage(language); } /// Enables or disables the collection of anonymized telemetry data. @@ -345,7 +307,7 @@ class MapboxMapController extends ChangeNotifier { /// The returned [Future] completes after the change has been made on the /// platform side. Future setTelemetryEnabled(bool enabled) async { - return MapboxGlPlatform.getInstance(_id).setTelemetryEnabled(enabled); + return _mapboxGlPlatform.setTelemetryEnabled(enabled); } /// Retrieves whether collection of anonymized telemetry data is enabled. @@ -353,7 +315,7 @@ class MapboxMapController extends ChangeNotifier { /// The returned [Future] completes after the query has been made on the /// platform side. Future getTelemetryEnabled() async { - return MapboxGlPlatform.getInstance(_id).getTelemetryEnabled(); + return _mapboxGlPlatform.getTelemetryEnabled(); } /// Adds a symbol to the map, configured using the specified custom [options]. @@ -383,8 +345,7 @@ class MapboxMapController extends ChangeNotifier { final List effectiveOptions = options.map((o) => SymbolOptions.defaultOptions.copyWith(o)).toList(); - final symbols = await MapboxGlPlatform.getInstance(_id) - .addSymbols(effectiveOptions, data); + final symbols = await _mapboxGlPlatform.addSymbols(effectiveOptions, data); symbols.forEach((s) => _symbols[s.id] = s); notifyListeners(); return symbols; @@ -400,7 +361,7 @@ class MapboxMapController extends ChangeNotifier { Future updateSymbol(Symbol symbol, SymbolOptions changes) async { assert(_symbols[symbol.id] == symbol); - await MapboxGlPlatform.getInstance(_id).updateSymbol(symbol, changes); + await _mapboxGlPlatform.updateSymbol(symbol, changes); symbol.options = symbol.options.copyWith(changes); notifyListeners(); } @@ -410,8 +371,7 @@ class MapboxMapController 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 MapboxGlPlatform.getInstance(_id).getSymbolLatLng(symbol); + final symbolLatLng = await _mapboxGlPlatform.getSymbolLatLng(symbol); notifyListeners(); return symbolLatLng; } @@ -451,7 +411,7 @@ class MapboxMapController extends ChangeNotifier { /// /// The returned [Future] completes once listeners have been notified. Future clearSymbols() async { - await MapboxGlPlatform.getInstance(_id).removeSymbols(_symbols.keys); + await _mapboxGlPlatform.removeSymbols(_symbols.keys); _symbols.clear(); notifyListeners(); } @@ -462,7 +422,7 @@ class MapboxMapController extends ChangeNotifier { /// The returned [Future] completes once the symbol has been removed from /// [_symbols]. Future _removeSymbols(Iterable ids) async { - await MapboxGlPlatform.getInstance(_id).removeSymbols(ids); + await _mapboxGlPlatform.removeSymbols(ids); _symbols.removeWhere((k, s) => ids.contains(k)); } @@ -476,8 +436,7 @@ class MapboxMapController extends ChangeNotifier { Future addLine(LineOptions options, [Map? data]) async { final LineOptions effectiveOptions = LineOptions.defaultOptions.copyWith(options); - final line = - await MapboxGlPlatform.getInstance(_id).addLine(effectiveOptions, data); + final line = await _mapboxGlPlatform.addLine(effectiveOptions, data); _lines[line.id] = line; notifyListeners(); return line; @@ -492,8 +451,7 @@ class MapboxMapController extends ChangeNotifier { /// been notified. Future> addLines(List options, [List? data]) async { - final lines = - await MapboxGlPlatform.getInstance(_id).addLines(options, data); + final lines = await _mapboxGlPlatform.addLines(options, data); lines.forEach((l) => _lines[l.id] = l); notifyListeners(); return lines; @@ -508,7 +466,7 @@ class MapboxMapController extends ChangeNotifier { /// The returned [Future] completes once listeners have been notified. Future updateLine(Line line, LineOptions changes) async { assert(_lines[line.id] == line); - await MapboxGlPlatform.getInstance(_id).updateLine(line, changes); + await _mapboxGlPlatform.updateLine(line, changes); line.options = line.options.copyWith(changes); notifyListeners(); } @@ -518,8 +476,7 @@ class MapboxMapController 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 MapboxGlPlatform.getInstance(_id).getLineLatLngs(line); + final lineLatLngs = await _mapboxGlPlatform.getLineLatLngs(line); notifyListeners(); return lineLatLngs; } @@ -534,7 +491,7 @@ class MapboxMapController extends ChangeNotifier { Future removeLine(Line line) async { assert(_lines[line.id] == line); - await MapboxGlPlatform.getInstance(_id).removeLine(line.id); + await _mapboxGlPlatform.removeLine(line.id); _lines.remove(line.id); notifyListeners(); } @@ -550,7 +507,7 @@ class MapboxMapController extends ChangeNotifier { final ids = lines.where((l) => _lines[l.id] == l).map((l) => l.id); assert(lines.length == ids.length); - await MapboxGlPlatform.getInstance(_id).removeLines(ids); + await _mapboxGlPlatform.removeLines(ids); ids.forEach((id) => _lines.remove(id)); notifyListeners(); } @@ -563,7 +520,7 @@ class MapboxMapController extends ChangeNotifier { /// The returned [Future] completes once listeners have been notified. Future clearLines() async { final List lineIds = List.from(_lines.keys); - await MapboxGlPlatform.getInstance(_id).removeLines(lineIds); + await _mapboxGlPlatform.removeLines(lineIds); _lines.clear(); notifyListeners(); } @@ -578,8 +535,7 @@ 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.addCircle(effectiveOptions, data); _circles[circle.id] = circle; notifyListeners(); return circle; @@ -595,8 +551,7 @@ class MapboxMapController extends ChangeNotifier { /// been notified. Future> addCircles(List options, [List? data]) async { - final circles = - await MapboxGlPlatform.getInstance(_id).addCircles(options, data); + final circles = await _mapboxGlPlatform.addCircles(options, data); circles.forEach((c) => _circles[c.id] = c); notifyListeners(); return circles; @@ -611,7 +566,7 @@ class MapboxMapController extends ChangeNotifier { /// The returned [Future] completes once listeners have been notified. Future updateCircle(Circle circle, CircleOptions changes) async { assert(_circles[circle.id] == circle); - await MapboxGlPlatform.getInstance(_id).updateCircle(circle, changes); + await _mapboxGlPlatform.updateCircle(circle, changes); circle.options = circle.options.copyWith(changes); notifyListeners(); } @@ -621,8 +576,7 @@ class MapboxMapController 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 MapboxGlPlatform.getInstance(_id).getCircleLatLng(circle); + final circleLatLng = await _mapboxGlPlatform.getCircleLatLng(circle); notifyListeners(); return circleLatLng; } @@ -637,7 +591,7 @@ class MapboxMapController extends ChangeNotifier { Future removeCircle(Circle circle) async { assert(_circles[circle.id] == circle); - await MapboxGlPlatform.getInstance(_id).removeCircle(circle.id); + await _mapboxGlPlatform.removeCircle(circle.id); _circles.remove(circle.id); notifyListeners(); @@ -654,7 +608,7 @@ class MapboxMapController extends ChangeNotifier { final ids = circles.where((c) => _circles[c.id] == c).map((c) => c.id); assert(circles.length == ids.length); - await MapboxGlPlatform.getInstance(_id).removeCircles(ids); + await _mapboxGlPlatform.removeCircles(ids); ids.forEach((id) => _circles.remove(id)); notifyListeners(); } @@ -666,7 +620,7 @@ class MapboxMapController extends ChangeNotifier { /// /// The returned [Future] completes once listeners have been notified. Future clearCircles() async { - await MapboxGlPlatform.getInstance(_id).removeCircles(_circles.keys); + await _mapboxGlPlatform.removeCircles(_circles.keys); _circles.clear(); notifyListeners(); @@ -682,8 +636,7 @@ class MapboxMapController extends ChangeNotifier { Future addFill(FillOptions options, [Map? data]) async { final FillOptions effectiveOptions = FillOptions.defaultOptions.copyWith(options); - final fill = - await MapboxGlPlatform.getInstance(_id).addFill(effectiveOptions, data); + final fill = await _mapboxGlPlatform.addFill(effectiveOptions, data); _fills[fill.id] = fill; notifyListeners(); return fill; @@ -699,8 +652,7 @@ class MapboxMapController extends ChangeNotifier { /// been notified. Future> addFills(List options, [List? data]) async { - final circles = - await MapboxGlPlatform.getInstance(_id).addFills(options, data); + final circles = await _mapboxGlPlatform.addFills(options, data); circles.forEach((f) => _fills[f.id] = f); notifyListeners(); return circles; @@ -715,7 +667,7 @@ class MapboxMapController extends ChangeNotifier { /// The returned [Future] completes once listeners have been notified. Future updateFill(Fill fill, FillOptions changes) async { assert(_fills[fill.id] == fill); - await MapboxGlPlatform.getInstance(_id).updateFill(fill, changes); + await _mapboxGlPlatform.updateFill(fill, changes); fill.options = fill.options.copyWith(changes); notifyListeners(); } @@ -727,7 +679,7 @@ class MapboxMapController extends ChangeNotifier { /// /// The returned [Future] completes once listeners have been notified. Future clearFills() async { - await MapboxGlPlatform.getInstance(_id).removeFills(_fills.keys); + await _mapboxGlPlatform.removeFills(_fills.keys); _fills.clear(); notifyListeners(); @@ -742,7 +694,7 @@ class MapboxMapController extends ChangeNotifier { /// The returned [Future] completes once listeners have been notified. Future removeFill(Fill fill) async { assert(_fills[fill.id] == fill); - await MapboxGlPlatform.getInstance(_id).removeFill(fill.id); + await _mapboxGlPlatform.removeFill(fill.id); _fills.remove(fill.id); notifyListeners(); @@ -759,37 +711,36 @@ class MapboxMapController extends ChangeNotifier { final ids = fills.where((f) => _fills[f.id] == f).map((f) => f.id); assert(fills.length == ids.length); - await MapboxGlPlatform.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 MapboxGlPlatform.getInstance(_id) - .queryRenderedFeatures(point, layerIds, filter); + return _mapboxGlPlatform.queryRenderedFeatures(point, layerIds, filter); } Future queryRenderedFeaturesInRect( Rect rect, List layerIds, String? filter) async { - return MapboxGlPlatform.getInstance(_id) - .queryRenderedFeaturesInRect(rect, layerIds, filter); + return _mapboxGlPlatform.queryRenderedFeaturesInRect( + rect, layerIds, filter); } Future invalidateAmbientCache() async { - return MapboxGlPlatform.getInstance(_id).invalidateAmbientCache(); + return _mapboxGlPlatform.invalidateAmbientCache(); } /// Get last my location /// /// Return last latlng, nullable Future requestMyLocationLatLng() async { - return MapboxGlPlatform.getInstance(_id).requestMyLocationLatLng(); + return _mapboxGlPlatform.requestMyLocationLatLng(); } /// This method returns the boundaries of the region currently displayed in the map. Future getVisibleRegion() async { - return MapboxGlPlatform.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. @@ -828,59 +779,55 @@ class MapboxMapController extends ChangeNotifier { /// } /// ``` Future addImage(String name, Uint8List bytes, [bool sdf = false]) { - return MapboxGlPlatform.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 MapboxGlPlatform.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 MapboxGlPlatform.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 MapboxGlPlatform.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 MapboxGlPlatform.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 MapboxGlPlatform.getInstance(_id) - .addImageSource(imageSourceId, bytes, coordinates); + return _mapboxGlPlatform.addImageSource(imageSourceId, bytes, coordinates); } /// Removes previously added image source by id Future removeImageSource(String imageSourceId) { - return MapboxGlPlatform.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 MapboxGlPlatform.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 MapboxGlPlatform.getInstance(_id) - .addLayerBelow(imageLayerId, imageSourceId, belowLayerId); + return _mapboxGlPlatform.addLayerBelow( + imageLayerId, imageSourceId, belowLayerId); } /// Removes a Mapbox style layer Future removeLayer(String imageLayerId) { - return MapboxGlPlatform.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) @@ -890,22 +837,21 @@ class MapboxMapController extends ChangeNotifier { /// /// Returns null if [latLng] is not currently visible on the map. Future toScreenLocation(LatLng latLng) async { - return MapboxGlPlatform.getInstance(_id).toScreenLocation(latLng); + return _mapboxGlPlatform.toScreenLocation(latLng); } Future> toScreenLocationBatch(Iterable latLngs) async { - return MapboxGlPlatform.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 MapboxGlPlatform.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 MapboxGlPlatform.getInstance(_id) - .getMetersPerPixelAtLatitude(latitude); + return _mapboxGlPlatform.getMetersPerPixelAtLatitude(latitude); } } diff --git a/lib/src/mapbox_map.dart b/lib/src/mapbox_map.dart index d2d38a9a4..f0acf2c90 100644 --- a/lib/src/mapbox_map.dart +++ b/lib/src/mapbox_map.dart @@ -251,22 +251,15 @@ class _MapboxMapState extends State { } Future onPlatformViewCreated(int id) async { - MapboxGlPlatform.addInstance(id, _mapboxGlPlatform); - final MapboxMapController controller = MapboxMapController.init( - id, - widget.initialCameraPosition, + final MapboxMapController controller = MapboxMapController( + mapboxGlPlatform: _mapboxGlPlatform, + initialCameraPosition: widget.initialCameraPosition, onStyleLoadedCallback: () { - if (_controller.isCompleted) { + _controller.future.then((_) { if (widget.onStyleLoadedCallback != null) { widget.onStyleLoadedCallback!(); } - } else { - _controller.future.then((_) { - if (widget.onStyleLoadedCallback != null) { - widget.onStyleLoadedCallback!(); - } - }); - } + }); }, onMapClick: widget.onMapClick, onUserLocationUpdated: widget.onUserLocationUpdated, @@ -277,7 +270,7 @@ class _MapboxMapState extends State { onCameraIdle: widget.onCameraIdle, onMapIdle: widget.onMapIdle, ); - await MapboxMapController.initPlatform(id); + await _mapboxGlPlatform.initPlatform(id); _controller.complete(controller); if (widget.onMapCreated != null) { widget.onMapCreated!(controller); diff --git a/mapbox_gl_platform_interface/lib/src/mapbox_gl_platform_interface.dart b/mapbox_gl_platform_interface/lib/src/mapbox_gl_platform_interface.dart index f28e37531..42bd76a95 100644 --- a/mapbox_gl_platform_interface/lib/src/mapbox_gl_platform_interface.dart +++ b/mapbox_gl_platform_interface/lib/src/mapbox_gl_platform_interface.dart @@ -14,16 +14,6 @@ abstract class MapboxGlPlatform { static MapboxGlPlatform Function() createInstance = () => MethodChannelMapboxGl(); - static Map _instances = {}; - - static void addInstance(int id, MapboxGlPlatform platform) { - _instances[id] = platform; - } - - static MapboxGlPlatform getInstance(int id) { - return _instances[id]!; - } - final onInfoWindowTappedPlatform = ArgumentCallbacks(); final onSymbolTappedPlatform = ArgumentCallbacks(); diff --git a/mapbox_gl_platform_interface/lib/src/method_channel_mapbox_gl.dart b/mapbox_gl_platform_interface/lib/src/method_channel_mapbox_gl.dart index d6ec09343..5cc04f8dd 100644 --- a/mapbox_gl_platform_interface/lib/src/method_channel_mapbox_gl.dart +++ b/mapbox_gl_platform_interface/lib/src/method_channel_mapbox_gl.dart @@ -117,8 +117,8 @@ class MethodChannelMapboxGl extends MapboxGlPlatform { @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