Skip to content
Merged
Show file tree
Hide file tree
Changes from 19 commits
Commits
Show all changes
26 commits
Select commit Hold shift + click to select a range
f2a2c61
Added setStyle method on MaplibreMapController for updating style wit…
itheamc Jun 2, 2024
b6d0b42
Merge branch 'main' into main
josxha Jun 2, 2024
525d121
Merge branch 'maplibre:main' into main
itheamc Jun 3, 2024
0bafec2
Merge branch 'maplibre:main' into main
itheamc Jun 4, 2024
74d00af
Merge branch 'maplibre:main' into main
itheamc Jun 5, 2024
9e3a0cd
Merge remote-tracking branch 'origin/main'
itheamc Jun 6, 2024
ad0ae91
replaced _maplibreGlPlatform to _maplibrePlatform on controller setSt…
itheamc Jun 6, 2024
c46c61e
Merge remote-tracking branch 'origin/main' into feat-added-set-style-…
itheamc Jun 7, 2024
2bc6df6
added information regarding the style string format
itheamc Jun 7, 2024
788a8a1
auto applied style on map created
itheamc Jun 7, 2024
67f257f
synced with main branch
itheamc Jun 17, 2024
925ba67
returning error if style is missing or invalid args
itheamc Jun 24, 2024
5b5e639
synced with main branch
itheamc Jun 24, 2024
64e11c6
minor changes
itheamc Jun 25, 2024
106fb36
Synced with main branch
itheamc Jun 25, 2024
9635a95
Merge branch 'main' into feat-added-set-style-method-on-controller
josxha Oct 3, 2024
b45880c
sync this branch with main
itheamc Jul 4, 2025
8c3f0f4
resolve merge conflict
itheamc Jul 4, 2025
1f4aa18
Merge branch 'main' into feat-added-set-style-method-on-controller
gabbopalma Sep 30, 2025
dc9afc6
Update maplibre_gl_web/lib/src/maplibre_web_gl_platform.dart
gabbopalma Sep 30, 2025
39dc57c
Update maplibre_gl_platform_interface/lib/src/maplibre_gl_platform_in…
gabbopalma Sep 30, 2025
e7a3d0a
Merge branch 'release-0.23.0' into feat-added-set-style-method-on-con…
gabbopalma Sep 30, 2025
60f0fda
chore: Enhance setStyle in MapLibreMapController by preventing race c…
gabbopalma Sep 30, 2025
640c72d
chore: Remove LocalStylePage example and added MultyStyleSwitch. Upda…
gabbopalma Sep 30, 2025
b3b261c
chore: runned dart format
gabbopalma Sep 30, 2025
f424296
chore: update iOS platform version to 13.0 in Podfile and AppFramewor…
gabbopalma Sep 30, 2025
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -1621,6 +1621,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();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -542,7 +542,7 @@ class MapLibreMapController: NSObject, FlutterPlatformView, MLNMapViewDelegate,
maximumZoomLevel: maxzoom,
properties: properties
)

switch addResult {
case .success: result(nil)
case let .failure(error): result(error.flutterError)
Expand Down Expand Up @@ -967,6 +967,31 @@ class MapLibreMapController: NSObject, FlutterPlatformView, MLNMapViewDelegate,
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)
}
Expand Down Expand Up @@ -1226,7 +1251,7 @@ class MapLibreMapController: NSObject, FlutterPlatformView, MLNMapViewDelegate,
}
}
}

private func validateBeforeLayerAdd(
sourceId: String,
layerId: String
Expand Down Expand Up @@ -1590,7 +1615,7 @@ class MapLibreMapController: NSObject, FlutterPlatformView, MLNMapViewDelegate,
}

func addSource(sourceId: String, properties: [String: Any]) -> Result<Void, MethodCallError> {
guard let style = mapView.style else {
guard let style = mapView.style else {
return .failure(.styleNotFound)
}
guard style.source(withIdentifier: sourceId) == nil else {
Expand Down Expand Up @@ -1640,7 +1665,7 @@ class MapLibreMapController: NSObject, FlutterPlatformView, MLNMapViewDelegate,
return .failure(.invalidSourceType(
details: "Source '\(sourceId)' does not support type '\(type)'."
))

}

func mapViewDidBecomeIdle(_: MLNMapView) {
Expand Down Expand Up @@ -1675,7 +1700,7 @@ class MapLibreMapController: NSObject, FlutterPlatformView, MLNMapViewDelegate,

func addSourceGeojson(sourceId: String, geojson: String) -> Result<Void, MethodCallError> {
do{
guard let style = mapView.style else {
guard let style = mapView.style else {
return .failure(.styleNotFound)
}
guard style.source(withIdentifier: sourceId) == nil else {
Expand All @@ -1696,7 +1721,7 @@ class MapLibreMapController: NSObject, FlutterPlatformView, MLNMapViewDelegate,
}

func setSource(sourceId: String, geojson: String) -> Result<Void, MethodCallError> {
guard let style = mapView.style else {
guard let style = mapView.style else {
return .failure(.styleNotFound)
}

Expand All @@ -1716,10 +1741,10 @@ class MapLibreMapController: NSObject, FlutterPlatformView, MLNMapViewDelegate,
}

}


func setFeature(sourceId: String, geojsonFeature: String) -> Result<Void, MethodCallError> {
guard let style = mapView.style else {
guard let style = mapView.style else {
return .failure(.styleNotFound)
}
do {
Expand Down
14 changes: 14 additions & 0 deletions maplibre_gl/lib/src/controller.dart
Original file line number Diff line number Diff line change
Expand Up @@ -1437,6 +1437,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<void> setStyle(String styleString) async {
return _maplibrePlatform.setStyle(styleString);
}

@override
void dispose() {
super.dispose();
Expand Down
24 changes: 16 additions & 8 deletions maplibre_gl_example/lib/local_style.dart
Original file line number Diff line number Diff line change
Expand Up @@ -51,25 +51,33 @@ class LocalStyleState extends State<LocalStyle> {

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...')),
);
}

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() {}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -202,6 +202,18 @@ abstract class MapLibrePlatform {

Future<void> 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<void> setStyle(String styleString);

@mustCallSuper
void dispose() {
// clear all callbacks to avoid cyclic refs
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -831,4 +831,22 @@ class MapLibreMethodChannel extends MapLibrePlatform {
return Future.error(e);
}
}

/// Method to set style string
///
@override
Future<void> setStyle(String styleString) async {
try {
await _channel.invokeMethod(
'style#setStyle',
<String, dynamic>{
'style': styleString,
},
);
} on PlatformException catch (e) {
return Future.error(e);
} catch (e) {
return Future.error(e);
}
}
}
7 changes: 7 additions & 0 deletions maplibre_gl_web/lib/src/maplibre_web_gl_platform.dart
Original file line number Diff line number Diff line change
Expand Up @@ -1150,4 +1150,11 @@ class MapLibreMapController extends MapLibrePlatform
Future<List> getSourceIds() async {
throw UnimplementedError();
}

/// Method to set style string
/// [styleString] -> It will take json, url, absolute path or asset path
@override
Future<void> setStyle(String styleString) async {
_map.setStyle(styleString);
}
}