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

added setStyle method on map controller #431

Closed
wants to merge 2 commits into from
Closed
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
4,150 changes: 2,065 additions & 2,085 deletions android/src/main/java/com/mapbox/mapboxgl/MapboxMapController.java

Large diffs are not rendered by default.

21 changes: 14 additions & 7 deletions example/lib/local_style.dart
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,8 @@ class LocalStyleState extends State<LocalStyle> {

await styleFile.writeAsString(styleJSON);

await Future.delayed(const Duration(milliseconds: 500));

setState(() {
styleAbsoluteFilePath = styleFile.path;
});
Expand All @@ -55,21 +57,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,
));
}

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 @@ -932,7 +932,12 @@ class MapboxMapController: NSObject, FlutterPlatformView, MGLMapViewDelegate, 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)
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 @@ -1426,6 +1426,12 @@ class MaplibreMapController extends ChangeNotifier {
.toList();
}

/// Method to set style string
///
Future<void> setStyle(String styleString) async {
return await _maplibreGlPlatform.setStyle(styleString);
}

@override
void dispose() {
super.dispose();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -205,6 +205,9 @@ abstract class MapLibreGlPlatform {

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

/// Method to set style string
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,20 @@ class MethodChannelMaplibreGl extends MapLibreGlPlatform {
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);
}
}
}
11 changes: 11 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,15 @@ class MaplibreMapController extends MapLibreGlPlatform
Future<List> getSourceIds() async {
throw UnimplementedError();
}

/// Method to set style string
///
@override
Future<void> setStyle(String styleString) async {
try {
_map.setStyle(styleString);
} on PlatformException catch (e) {
return Future.error(e);
}
}
}
6 changes: 4 additions & 2 deletions pubspec.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,10 @@ environment:
dependencies:
flutter:
sdk: flutter
maplibre_gl_platform_interface: ^0.19.0+2
maplibre_gl_web: ^0.19.0+2
maplibre_gl_platform_interface:
path: maplibre_gl_platform_interface
maplibre_gl_web:
path: maplibre_gl_web

dev_dependencies:
flutter_lints: '>=3.0.0 <5.0.0'
Expand Down