Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: added set style method on controller #444

Open
wants to merge 16 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
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
24 changes: 16 additions & 8 deletions 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 @@ -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();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
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 @@ -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<void> setStyle(String styleString) async {
return _maplibrePlatform.setStyle(styleString);
}

@override
void dispose() {
super.dispose();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -45,26 +45,35 @@ abstract class MapLibrePlatform {
final onUserLocationUpdatedPlatform = ArgumentCallbacks<UserLocation>();

Future<void> initPlatform(int id);

Widget buildView(
Map<String, dynamic> creationParams,
OnPlatformViewCreatedCallback onPlatformViewCreated,
Set<Factory<OneSequenceGestureRecognizer>>? gestureRecognizers);

Future<CameraPosition?> updateMapOptions(Map<String, dynamic> optionsUpdate);

Future<bool?> animateCamera(CameraUpdate cameraUpdate, {Duration? duration});

Future<bool?> moveCamera(CameraUpdate cameraUpdate);

Future<void> updateMyLocationTrackingMode(
MyLocationTrackingMode myLocationTrackingMode);

Future<void> matchMapLanguageWithDeviceDefault();

void resizeWebMap();

void forceResizeWebMap();

Future<void> updateContentInsets(EdgeInsets insets, bool animated);

Future<void> setMapLanguage(String language);

Future<void> setTelemetryEnabled(bool enabled);

Future<bool> getTelemetryEnabled();

Future<List> queryRenderedFeatures(
Point<double> point, List<String> layerIds, List<Object>? filter);

Expand All @@ -73,7 +82,9 @@ abstract class MapLibrePlatform {

Future<List> querySourceFeatures(
String sourceId, String? sourceLayerId, List<Object>? filter);

Future invalidateAmbientCache();

Future<LatLng?> requestMyLocationLatLng();

Future<LatLngBounds> getVisibleRegion();
Expand Down Expand Up @@ -201,6 +212,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 @@ -819,4 +819,22 @@ class MapLibreMethodChannel extends MapLibrePlatform {
return Future.error(e);
}
}

/// Method to set style string
///
josxha marked this conversation as resolved.
Show resolved Hide resolved
@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 @@ -1143,4 +1143,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);
}
}