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: Set layer visibility #138

Merged
merged 4 commits into from
Jul 31, 2022
Merged
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
21 changes: 21 additions & 0 deletions android/src/main/java/com/mapbox/mapboxgl/MapboxMapController.java
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,7 @@
import com.mapbox.mapboxsdk.style.layers.PropertyValue;
import com.mapbox.mapboxsdk.style.layers.RasterLayer;
import com.mapbox.mapboxsdk.style.layers.SymbolLayer;
import com.mapbox.mapboxsdk.style.layers.PropertyFactory;
import com.mapbox.mapboxsdk.style.sources.GeoJsonSource;
import com.mapbox.mapboxsdk.style.sources.ImageSource;
import io.flutter.plugin.common.BinaryMessenger;
Expand Down Expand Up @@ -1235,6 +1236,26 @@ public void onFailure(@NonNull Exception exception) {
result.success(null);
break;
}
case "layer#setVisibility":
{

if (style == null) {
result.error(
"STYLE IS NULL",
"The style is null. Has onStyleLoaded() already been invoked?",
null);
}
String layerId = call.argument("layerId");
boolean visible = call.argument("visible");

Layer layer = style.getLayer(layerId);

layer.setProperties(PropertyFactory.visibility(visible ? "visible" : "none"));

result.success(null);
break;

}
default:
result.notImplemented();
}
Expand Down
12 changes: 12 additions & 0 deletions example/lib/map_ui.dart
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,7 @@ class MapUiBodyState extends State<MapUiBody> {
bool _zoomGesturesEnabled = true;
bool _myLocationEnabled = true;
bool _telemetryEnabled = true;
bool _countriesVisible = true;
MyLocationTrackingMode _myLocationTrackingMode = MyLocationTrackingMode.None;
List<Object>? _featureQueryFilter;
Fill? _selectedFill;
Expand Down Expand Up @@ -290,6 +291,16 @@ class MapUiBodyState extends State<MapUiBody> {
);
}

Widget _layerVisibilityToggler() {
return TextButton(
child: Text('toggle layer visibility'),
onPressed: () async {
_countriesVisible = !_countriesVisible;
mapController?.setLayerVisibility('countries-fill', _countriesVisible);
},
);
}

_clearFill() {
if (_selectedFill != null) {
mapController!.removeFill(_selectedFill!);
Expand Down Expand Up @@ -410,6 +421,7 @@ class MapUiBodyState extends State<MapUiBody> {
_myLocationToggler(),
_telemetryToggler(),
_visibleRegionGetter(),
_layerVisibilityToggler(),
],
);
}
Expand Down
12 changes: 12 additions & 0 deletions ios/Classes/MapboxMapController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -688,6 +688,18 @@ class MapboxMapController: NSObject, FlutterPlatformView, MGLMapViewDelegate, Ma
guard let geojson = arguments["geojsonFeature"] as? String else { return }
setFeature(sourceId: sourceId, geojsonFeature: geojson)
result(nil)

case "layer#setVisibility":
guard let arguments = methodCall.arguments as? [String: Any] else { return }
guard let layerId = arguments["layerId"] as? String else { return }
guard let visible = arguments["visible"] as? Bool else { return }
guard let layer = mapView.style?.layer(withIdentifier: layerId) else {
result(nil)
return
}
layer.isVisible = visible
result(nil)

default:
result(FlutterMethodNotImplemented)
}
Expand Down
4 changes: 4 additions & 0 deletions lib/src/controller.dart
Original file line number Diff line number Diff line change
Expand Up @@ -1234,6 +1234,10 @@ class MaplibreMapController extends ChangeNotifier {
}
}

Future<void> setLayerVisibility(String layerId, bool visible) async {
return _mapboxGlPlatform.setLayerVisibility(layerId, visible);
}

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

Future<void> addSource(String sourceId, SourceProperties properties);

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

@mustCallSuper
void dispose() {
// clear all callbacks to avoid cyclic refs
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -677,6 +677,14 @@ class MethodChannelMaplibreGl extends MapLibreGlPlatform {
});
}

@override
Future<void> setLayerVisibility(String layerId, bool visible) async {
await _channel.invokeMethod('layer#setVisibility', <String, dynamic>{
'layerId': layerId,
'visible': visible,
});
}

@override
void forceResizeWebMap() {}

Expand Down
5 changes: 5 additions & 0 deletions maplibre_gl_web/lib/src/mapbox_web_gl_platform.dart
Original file line number Diff line number Diff line change
Expand Up @@ -990,4 +990,9 @@ class MaplibreMapController extends MapLibreGlPlatform
void forceResizeWebMap() {
_map.resize();
}

@override
Future<void> setLayerVisibility(String layerId, bool visible) async {
_map.setLayoutProperty(layerId, 'visibility', visible ? 'visible' : 'none');
}
}
4 changes: 2 additions & 2 deletions pubspec.lock
Original file line number Diff line number Diff line change
Expand Up @@ -68,14 +68,14 @@ packages:
path: maplibre_gl_platform_interface
relative: true
source: path
version: "0.15.1"
version: "0.16.0"
maplibre_gl_web:
dependency: "direct main"
description:
path: maplibre_gl_web
relative: true
source: path
version: "0.15.1"
version: "0.16.0"
material_color_utilities:
dependency: transitive
description:
Expand Down