diff --git a/example/lib/local_style.dart b/example/lib/local_style.dart index 5a3c35309..adeb55185 100644 --- a/example/lib/local_style.dart +++ b/example/lib/local_style.dart @@ -51,12 +51,20 @@ class LocalStyleState extends State { void _onMapCreated(MapLibreMapController controller) { mapController = controller; + + // Adding style to the map with some delay + Future.delayed( + const Duration(milliseconds: 250), + () async { + if (styleAbsoluteFilePath != null) { + await mapController?.setStyle(styleAbsoluteFilePath!); + } + }, + ); } @override Widget build(BuildContext context) { - final styleAbsoluteFilePath = this.styleAbsoluteFilePath; - if (styleAbsoluteFilePath == null) { return const Scaffold( body: Center(child: Text('Creating local style file...')), @@ -64,12 +72,12 @@ class LocalStyleState extends State { } return Scaffold( - body: MapLibreMap( - styleString: styleAbsoluteFilePath, - onMapCreated: _onMapCreated, - initialCameraPosition: const CameraPosition(target: LatLng(0.0, 0.0)), - onStyleLoadedCallback: onStyleLoadedCallback, - )); + body: MapLibreMap( + onMapCreated: _onMapCreated, + initialCameraPosition: const CameraPosition(target: LatLng(0.0, 0.0)), + onStyleLoadedCallback: onStyleLoadedCallback, + ), + ); } void onStyleLoadedCallback() {} diff --git a/maplibre_gl/android/src/main/java/org/maplibre/maplibregl/MapLibreMapController.java b/maplibre_gl/android/src/main/java/org/maplibre/maplibregl/MapLibreMapController.java index 724c39c1c..aa84a98c2 100644 --- a/maplibre_gl/android/src/main/java/org/maplibre/maplibregl/MapLibreMapController.java +++ b/maplibre_gl/android/src/main/java/org/maplibre/maplibregl/MapLibreMapController.java @@ -1588,6 +1588,27 @@ public void onFailure(@NonNull Exception exception) { result.success(reply); break; } + case "style#setStyle": + { + // Getting style json, url, path etc. from the flutter side + String styleString = call.argument("style"); + + // Checking if style is null or not + if (styleString != null) { + // If style is not null setting style + setStyleString(styleString); + result.success(null); + } else { + + // else throwing error + result.error( + "STYLE STRING IS NULL", + "The style string is null.", + null + ); + } + break; + } default: result.notImplemented(); } diff --git a/maplibre_gl/ios/maplibre_gl/Sources/maplibre_gl/MapLibreMapController.swift b/maplibre_gl/ios/maplibre_gl/Sources/maplibre_gl/MapLibreMapController.swift index f525566e0..4a094d412 100644 --- a/maplibre_gl/ios/maplibre_gl/Sources/maplibre_gl/MapLibreMapController.swift +++ b/maplibre_gl/ios/maplibre_gl/Sources/maplibre_gl/MapLibreMapController.swift @@ -929,7 +929,32 @@ class MapLibreMapController: NSObject, FlutterPlatformView, MLNMapViewDelegate, var reply = [String: NSObject]() reply["filter"] = currentLayerFilter as NSObject result(reply) - + + case "style#setStyle": + if let arguments = methodCall.arguments as? [String: Any] { + if let style = arguments["style"] as? String { + setStyleString(styleString: style) + result(nil) + } else { + // Error for missing style key in argument + result( + FlutterError( + code: "invalidStyleString", + message: "Missing style key in arguments", + details: nil + ) + ) + } + } else { + // Error for invalid arguments type + result( + FlutterError( + code: "invalidArgumentsType", + message: "Arguments not of type [String: Any]", + details: nil + ) + ) + } default: result(FlutterMethodNotImplemented) } diff --git a/maplibre_gl/lib/src/controller.dart b/maplibre_gl/lib/src/controller.dart index 71a5e827f..d335e63d7 100644 --- a/maplibre_gl/lib/src/controller.dart +++ b/maplibre_gl/lib/src/controller.dart @@ -1406,6 +1406,20 @@ class MapLibreMapController extends ChangeNotifier { .toList(); } + /// Method to set style string + /// A MapLibre GL style document defining the map's appearance. + /// The style document specification is at [https://maplibre.org/maplibre-style-spec]. + /// A short introduction can be found in the documentation of the [maplibre_gl] library. + /// The [styleString] supports following formats: + /// + /// 1. Passing the URL of the map style. This should be a custom map style served remotely using a URL that start with 'http(s)://' + /// 2. Passing the style as a local asset. Create a JSON file in the `assets` and add a reference in `pubspec.yml`. Set the style string to the relative path for this asset in order to load it into the map. + /// 3. Passing the style as a local file. create an JSON file in app directory (e.g. ApplicationDocumentsDirectory). Set the style string to the absolute path of this JSON file. + /// 4. Passing the raw JSON of the map style. This is only supported on Android. + Future setStyle(String styleString) async { + return _maplibrePlatform.setStyle(styleString); + } + @override void dispose() { super.dispose(); diff --git a/maplibre_gl_platform_interface/lib/src/maplibre_gl_platform_interface.dart b/maplibre_gl_platform_interface/lib/src/maplibre_gl_platform_interface.dart index 9f6bb1a86..cb9539307 100644 --- a/maplibre_gl_platform_interface/lib/src/maplibre_gl_platform_interface.dart +++ b/maplibre_gl_platform_interface/lib/src/maplibre_gl_platform_interface.dart @@ -45,26 +45,35 @@ abstract class MapLibrePlatform { final onUserLocationUpdatedPlatform = ArgumentCallbacks(); Future initPlatform(int id); + Widget buildView( Map creationParams, OnPlatformViewCreatedCallback onPlatformViewCreated, Set>? gestureRecognizers); + Future updateMapOptions(Map optionsUpdate); + Future animateCamera(CameraUpdate cameraUpdate, {Duration? duration}); + Future moveCamera(CameraUpdate cameraUpdate); + Future updateMyLocationTrackingMode( MyLocationTrackingMode myLocationTrackingMode); Future matchMapLanguageWithDeviceDefault(); void resizeWebMap(); + void forceResizeWebMap(); Future updateContentInsets(EdgeInsets insets, bool animated); + Future setMapLanguage(String language); + Future setTelemetryEnabled(bool enabled); Future getTelemetryEnabled(); + Future queryRenderedFeatures( Point point, List layerIds, List? filter); @@ -73,7 +82,9 @@ abstract class MapLibrePlatform { Future querySourceFeatures( String sourceId, String? sourceLayerId, List? filter); + Future invalidateAmbientCache(); + Future requestMyLocationLatLng(); Future getVisibleRegion(); @@ -201,6 +212,18 @@ abstract class MapLibrePlatform { Future setLayerVisibility(String layerId, bool visible); + /// Method to set style string + /// A MapLibre GL style document defining the map's appearance. + /// The style document specification is at [https://maplibre.org/maplibre-style-spec]. + /// A short introduction can be found in the documentation of the [maplibre_gl] library. + /// The [styleString] supports following formats: + /// + /// 1. Passing the URL of the map style. This should be a custom map style served remotely using a URL that start with 'http(s)://' + /// 2. Passing the style as a local asset. Create a JSON file in the `assets` and add a reference in `pubspec.yml`. Set the style string to the relative path for this asset in order to load it into the map. + /// 3. Passing the style as a local file. create an JSON file in app directory (e.g. ApplicationDocumentsDirectory). Set the style string to the absolute path of this JSON file. + /// 4. Passing the raw JSON of the map style. This is only supported on Android. + Future setStyle(String styleString); + @mustCallSuper void dispose() { // clear all callbacks to avoid cyclic refs diff --git a/maplibre_gl_platform_interface/lib/src/method_channel_maplibre_gl.dart b/maplibre_gl_platform_interface/lib/src/method_channel_maplibre_gl.dart index d2b4ccccd..3f1741869 100644 --- a/maplibre_gl_platform_interface/lib/src/method_channel_maplibre_gl.dart +++ b/maplibre_gl_platform_interface/lib/src/method_channel_maplibre_gl.dart @@ -819,4 +819,22 @@ class MapLibreMethodChannel extends MapLibrePlatform { return Future.error(e); } } + + /// Method to set style string + /// + @override + Future setStyle(String styleString) async { + try { + await _channel.invokeMethod( + 'style#setStyle', + { + 'style': styleString, + }, + ); + } on PlatformException catch (e) { + return Future.error(e); + } catch (e) { + return Future.error(e); + } + } } diff --git a/maplibre_gl_web/lib/src/maplibre_web_gl_platform.dart b/maplibre_gl_web/lib/src/maplibre_web_gl_platform.dart index 426f89f57..6a09dd28a 100644 --- a/maplibre_gl_web/lib/src/maplibre_web_gl_platform.dart +++ b/maplibre_gl_web/lib/src/maplibre_web_gl_platform.dart @@ -1143,4 +1143,11 @@ class MapLibreMapController extends MapLibrePlatform Future getSourceIds() async { throw UnimplementedError(); } + + /// Method to set style string + /// [styleString] -> It will take json, url, absolute path or asset path + @override + Future setStyle(String styleString) async { + _map.setStyle(styleString); + } }