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 7 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
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
19 changes: 12 additions & 7 deletions example/lib/local_style.dart
Original file line number Diff line number Diff line change
Expand Up @@ -55,21 +55,26 @@ class LocalStyleState extends State<LocalStyle> {

@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(
floatingActionButton: FloatingActionButton.small(
child: const Icon(Icons.layers_outlined),
onPressed: () async {
if (styleAbsoluteFilePath != null) {
await mapController?.setStyle(styleAbsoluteFilePath!);
}
},
),
body: MapLibreMap(
styleString: styleAbsoluteFilePath,
onMapCreated: _onMapCreated,
initialCameraPosition: const CameraPosition(target: LatLng(0.0, 0.0)),
onStyleLoadedCallback: onStyleLoadedCallback,
));
onMapCreated: _onMapCreated,
initialCameraPosition: const CameraPosition(target: LatLng(0.0, 0.0)),
onStyleLoadedCallback: onStyleLoadedCallback,
));
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Adding the option to change the map to this example could be confusing because this example showcases that the map style can in general be loaded from an local source. Could you create a new page that showcases this functionality please?

}

void onStyleLoadedCallback() {}
Expand Down
7 changes: 6 additions & 1 deletion ios/Classes/MapboxMapController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -929,7 +929,12 @@ class MapboxMapController: NSObject, FlutterPlatformView, MLNMapViewDelegate, Ma
var reply = [String: NSObject]()
reply["filter"] = currentLayerFilter as NSObject
result(reply)


case "style#setStyle":
guard let arguments = methodCall.arguments as? [String: Any] else { return }
guard let style = arguments["style"] as? String else { return }
setStyleString(styleString: style)
result(nil)
josxha marked this conversation as resolved.
Show resolved Hide resolved
default:
result(FlutterMethodNotImplemented)
}
Expand Down
6 changes: 6 additions & 0 deletions lib/src/controller.dart
Original file line number Diff line number Diff line change
Expand Up @@ -1427,6 +1427,12 @@ class MapLibreMapController extends ChangeNotifier {
.toList();
}

/// Method to set style string
/// [styleString] -> It will take json, url, absolute path or asset path
Future<void> setStyle(String styleString) async {
itheamc marked this conversation as resolved.
Show resolved Hide resolved
return await _maplibrePlatform.setStyle(styleString);
}

@override
void dispose() {
super.dispose();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -49,26 +49,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 @@ -77,7 +86,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 @@ -205,6 +216,10 @@ abstract class MapLibrePlatform {

Future<void> setLayerVisibility(String layerId, bool visible);

/// Method to set style string
/// [styleString] -> It will take json, url, absolute path or asset path
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 @@ -833,4 +833,21 @@ class MapLibreMethodChannel extends MapLibrePlatform {
return Future.error(e);
}
}

/// Method to set style string
/// [styleString] -> It will take json, url, absolute path or asset path
///
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);
}
}
}
12 changes: 12 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 @@ -1156,4 +1156,16 @@ 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 {
try {
_map.setStyle(styleString);
} on PlatformException catch (e) {
return Future.error(e);
}
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

When does _map.setStyle throw a PlatformException? (It's not used in the other methods.)

}
}