From 0c19f71500f703ca78e85ef46f5840d3851dae0f Mon Sep 17 00:00:00 2001 From: mvan Date: Wed, 23 Nov 2022 17:29:04 +0100 Subject: [PATCH 1/9] fix: Trimming styleString to simplify the JSON detection --- .../src/main/java/com/mapbox/mapboxgl/MapboxMapController.java | 1 + 1 file changed, 1 insertion(+) diff --git a/android/src/main/java/com/mapbox/mapboxgl/MapboxMapController.java b/android/src/main/java/com/mapbox/mapboxgl/MapboxMapController.java index 2d66a5e3..cb7cb3bd 100644 --- a/android/src/main/java/com/mapbox/mapboxgl/MapboxMapController.java +++ b/android/src/main/java/com/mapbox/mapboxgl/MapboxMapController.java @@ -258,6 +258,7 @@ public boolean onTouch(View v, MotionEvent event) { public void setStyleString(String styleString) { // clear old layer id from the location Component clearLocationComponentLayer(); + styleString = styleString.trim(); // Check if json, url, absolute path or asset path: if (styleString == null || styleString.isEmpty()) { From ac2d4903dd9112ad2b1b6b287f47c30a2f337e77 Mon Sep 17 00:00:00 2001 From: mvan Date: Fri, 17 Feb 2023 10:35:21 +0100 Subject: [PATCH 2/9] Added getSourceIds on android / ios --- .../mapbox/mapboxgl/MapboxMapController.java | 19 +++++++++++++++++++ ios/Classes/MapboxMapController.swift | 11 +++++++++++ .../lib/src/mapbox_gl_platform_interface.dart | 2 ++ .../lib/src/method_channel_mapbox_gl.dart | 11 +++++++++++ .../lib/src/mapbox_web_gl_platform.dart | 5 +++++ 5 files changed, 48 insertions(+) diff --git a/android/src/main/java/com/mapbox/mapboxgl/MapboxMapController.java b/android/src/main/java/com/mapbox/mapboxgl/MapboxMapController.java index 2d66a5e3..338e53df 100644 --- a/android/src/main/java/com/mapbox/mapboxgl/MapboxMapController.java +++ b/android/src/main/java/com/mapbox/mapboxgl/MapboxMapController.java @@ -1376,6 +1376,25 @@ public void onFailure(@NonNull Exception exception) { result.success(reply); break; } + case "style#getSourceIds": + { + if (style == null) { + result.error( + "STYLE IS NULL", + "The style is null. Has onStyleLoaded() already been invoked?", + null); + } + Map reply = new HashMap<>(); + + List sourceIds = new ArrayList<>(); + for (Source source : style.getSources()) { + sourceIds.add(source.getId()); + } + + reply.put("sources", sourceIds); + result.success(reply); + break; + } default: result.notImplemented(); } diff --git a/ios/Classes/MapboxMapController.swift b/ios/Classes/MapboxMapController.swift index f4b2fd84..94efd6a3 100644 --- a/ios/Classes/MapboxMapController.swift +++ b/ios/Classes/MapboxMapController.swift @@ -765,6 +765,17 @@ class MapboxMapController: NSObject, FlutterPlatformView, MGLMapViewDelegate, Ma var reply = [String: NSObject]() reply["layers"] = layerIds as NSObject result(reply) + + case "style#getSrouceIds": + var sourceIds = [String]() + + guard let style = mapView.style else { return } + + style.sources.forEach { source in sourceIds.append(source.identifier) } + + var reply = [String: NSObject]() + reply["sources"] = sourceIds as NSObject + result(reply) case "style#getFilter": guard let arguments = methodCall.arguments as? [String: Any] else { return } diff --git a/maplibre_gl_platform_interface/lib/src/mapbox_gl_platform_interface.dart b/maplibre_gl_platform_interface/lib/src/mapbox_gl_platform_interface.dart index c6d16f63..26ea0270 100644 --- a/maplibre_gl_platform_interface/lib/src/mapbox_gl_platform_interface.dart +++ b/maplibre_gl_platform_interface/lib/src/mapbox_gl_platform_interface.dart @@ -91,6 +91,8 @@ abstract class MapLibreGlPlatform { Future getLayerIds(); + Future getSourceIds(); + Future setFilter(String layerId, dynamic filter); Future getFilter(String layerId); diff --git a/maplibre_gl_platform_interface/lib/src/method_channel_mapbox_gl.dart b/maplibre_gl_platform_interface/lib/src/method_channel_mapbox_gl.dart index 0dae8468..5526301a 100644 --- a/maplibre_gl_platform_interface/lib/src/method_channel_mapbox_gl.dart +++ b/maplibre_gl_platform_interface/lib/src/method_channel_mapbox_gl.dart @@ -753,4 +753,15 @@ class MethodChannelMaplibreGl extends MapLibreGlPlatform { return new Future.error(e); } } + + @override + Future getSourceIds() async { + try { + final Map reply = + await _channel.invokeMethod('style#getSourceIds'); + return reply['sources'].map((it) => it.toString()).toList(); + } on PlatformException catch (e) { + return new Future.error(e); + } + } } diff --git a/maplibre_gl_web/lib/src/mapbox_web_gl_platform.dart b/maplibre_gl_web/lib/src/mapbox_web_gl_platform.dart index 317805a5..ed52e27d 100644 --- a/maplibre_gl_web/lib/src/mapbox_web_gl_platform.dart +++ b/maplibre_gl_web/lib/src/mapbox_web_gl_platform.dart @@ -1045,4 +1045,9 @@ class MaplibreMapController extends MapLibreGlPlatform Future getLayerIds() async { throw UnimplementedError(); } + + @override + Future getSourceIds() async { + throw UnimplementedError(); + } } From df2b54d1087af541e276456f4909d70687ed0dc9 Mon Sep 17 00:00:00 2001 From: mvan Date: Fri, 17 Feb 2023 10:39:59 +0100 Subject: [PATCH 3/9] Added getSourceIds on the controller --- lib/src/controller.dart | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/lib/src/controller.dart b/lib/src/controller.dart index 2ab8ac03..e3f7fa76 100644 --- a/lib/src/controller.dart +++ b/lib/src/controller.dart @@ -1270,6 +1270,10 @@ class MaplibreMapController extends ChangeNotifier { return _mapboxGlPlatform.getLayerIds(); } + Future getSourceIds() { + return _mapboxGlPlatform.getSourceIds(); + } + @override void dispose() { super.dispose(); From be0d599c83e422bf5939fb3bb38eee5205acf277 Mon Sep 17 00:00:00 2001 From: mvan Date: Fri, 17 Feb 2023 11:01:07 +0100 Subject: [PATCH 4/9] Fixed getSourceIds to return a list of strings --- lib/src/controller.dart | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/lib/src/controller.dart b/lib/src/controller.dart index e3f7fa76..ae226ae1 100644 --- a/lib/src/controller.dart +++ b/lib/src/controller.dart @@ -1270,8 +1270,8 @@ class MaplibreMapController extends ChangeNotifier { return _mapboxGlPlatform.getLayerIds(); } - Future getSourceIds() { - return _mapboxGlPlatform.getSourceIds(); + Future> getSourceIds() async { + return (await _mapboxGlPlatform.getSourceIds()).whereType().toList(); } @override From 3ad6b06fb5e62600402f3b6cfd16e2771bac6ca9 Mon Sep 17 00:00:00 2001 From: mvan Date: Fri, 17 Feb 2023 11:11:09 +0100 Subject: [PATCH 5/9] Fixed formatting on getSourceIds --- lib/src/controller.dart | 4 +++- .../lib/src/method_channel_mapbox_gl.dart | 2 +- 2 files changed, 4 insertions(+), 2 deletions(-) diff --git a/lib/src/controller.dart b/lib/src/controller.dart index ae226ae1..7dd09b05 100644 --- a/lib/src/controller.dart +++ b/lib/src/controller.dart @@ -1271,7 +1271,9 @@ class MaplibreMapController extends ChangeNotifier { } Future> getSourceIds() async { - return (await _mapboxGlPlatform.getSourceIds()).whereType().toList(); + return (await _mapboxGlPlatform.getSourceIds()) + .whereType() + .toList(); } @override diff --git a/maplibre_gl_platform_interface/lib/src/method_channel_mapbox_gl.dart b/maplibre_gl_platform_interface/lib/src/method_channel_mapbox_gl.dart index 5526301a..1fef7d5c 100644 --- a/maplibre_gl_platform_interface/lib/src/method_channel_mapbox_gl.dart +++ b/maplibre_gl_platform_interface/lib/src/method_channel_mapbox_gl.dart @@ -758,7 +758,7 @@ class MethodChannelMaplibreGl extends MapLibreGlPlatform { Future getSourceIds() async { try { final Map reply = - await _channel.invokeMethod('style#getSourceIds'); + await _channel.invokeMethod('style#getSourceIds'); return reply['sources'].map((it) => it.toString()).toList(); } on PlatformException catch (e) { return new Future.error(e); From 66cb5deb8e5c7988563deccce632bd4323134e34 Mon Sep 17 00:00:00 2001 From: mvan Date: Thu, 23 Feb 2023 09:59:17 +0100 Subject: [PATCH 6/9] Fixed typo on IOS in the getSourceIds implementation --- ios/Classes/MapboxMapController.swift | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/ios/Classes/MapboxMapController.swift b/ios/Classes/MapboxMapController.swift index 94efd6a3..7b68ac8c 100644 --- a/ios/Classes/MapboxMapController.swift +++ b/ios/Classes/MapboxMapController.swift @@ -766,7 +766,7 @@ class MapboxMapController: NSObject, FlutterPlatformView, MGLMapViewDelegate, Ma reply["layers"] = layerIds as NSObject result(reply) - case "style#getSrouceIds": + case "style#getSourceIds": var sourceIds = [String]() guard let style = mapView.style else { return } From b19383f1d55163b5a8499478372400cbdfda2541 Mon Sep 17 00:00:00 2001 From: mvan Date: Thu, 23 Feb 2023 09:59:47 +0100 Subject: [PATCH 7/9] Added documentation comment on the getSourceIds controller's method --- lib/src/controller.dart | 3 +++ 1 file changed, 3 insertions(+) diff --git a/lib/src/controller.dart b/lib/src/controller.dart index 7dd09b05..ecb34262 100644 --- a/lib/src/controller.dart +++ b/lib/src/controller.dart @@ -1270,6 +1270,9 @@ class MaplibreMapController extends ChangeNotifier { return _mapboxGlPlatform.getLayerIds(); } + /// Retrieve every source ids of the map as a [String] list, including the ones added internally + /// + /// This method is not currently implemented on the web Future> getSourceIds() async { return (await _mapboxGlPlatform.getSourceIds()) .whereType() From 3bfeb2bf05da361fb859be6aa46ba8397dae83c3 Mon Sep 17 00:00:00 2001 From: mvan Date: Fri, 24 Feb 2023 11:07:30 +0100 Subject: [PATCH 8/9] Added get source ids + get layer ids to the example app --- example/lib/get_map_informations.dart | 122 ++++++++++++++++++++++++++ example/lib/main.dart | 2 + 2 files changed, 124 insertions(+) create mode 100644 example/lib/get_map_informations.dart diff --git a/example/lib/get_map_informations.dart b/example/lib/get_map_informations.dart new file mode 100644 index 00000000..e182f281 --- /dev/null +++ b/example/lib/get_map_informations.dart @@ -0,0 +1,122 @@ +import 'package:flutter/material.dart'; +import 'package:maplibre_gl/mapbox_gl.dart'; + +import 'page.dart'; + +class GetMapInfoPage extends ExamplePage { + GetMapInfoPage() : super(const Icon(Icons.info), 'Get map state'); + + @override + Widget build(BuildContext context) { + return GetMapInfoBody(); + } +} + +class GetMapInfoBody extends StatefulWidget { + const GetMapInfoBody(); + + @override + State createState() => _GetMapInfoBodyState(); +} + +class _GetMapInfoBodyState extends State { + MaplibreMapController? controller; + String data = ''; + + void onMapCreated(MaplibreMapController controller) { + setState(() { + this.controller = controller; + print('controller set'); + }); + } + + void displaySources() async { + if (controller == null) { + return; + } + List sources = await controller!.getSourceIds(); + setState(() { + data = 'Sources: ${sources.map((e) => '"$e"').join(', ')}'; + }); + } + + void displayLayers() async { + if (controller == null) { + return; + } + List layers = (await controller!.getLayerIds()).cast(); + setState(() { + data = 'Layers: ${layers.map((e) => '"$e"').join(', ')}'; + }); + } + + @override + Widget build(BuildContext context) { + return Column( + mainAxisAlignment: MainAxisAlignment.spaceEvenly, + crossAxisAlignment: CrossAxisAlignment.stretch, + children: [ + Center( + child: SizedBox( + width: 300.0, + height: 200.0, + child: MaplibreMap( + initialCameraPosition: const CameraPosition( + target: LatLng(-33.852, 151.211), + zoom: 11.0, + ), + onMapCreated: onMapCreated, + compassEnabled: false, + annotationOrder: [], + myLocationEnabled: false, + styleString: '''{ + "version": 8, + "sources": { + "OSM": { + "type": "raster", + "tiles": [ + "https://a.tile.openstreetmap.org/{z}/{x}/{y}.png", + "https://b.tile.openstreetmap.org/{z}/{x}/{y}.png", + "https://c.tile.openstreetmap.org/{z}/{x}/{y}.png" + ], + "tileSize": 256, + "attribution": "© OpenStreetMap contributors", + "maxzoom": 18 + } + }, + "layers": [ + { + "id": "OSM-layer", + "source": "OSM", + "type": "raster" + } + ] + }''', + ), + ), + ), + Center( + child: const Text('© OpenStreetMap contributors'), + ), + Expanded( + child: SingleChildScrollView( + child: Column( + children: [ + SizedBox(height: 30), + Center(child: Text(data)), + SizedBox(height: 30), + ElevatedButton( + onPressed: controller == null ? null : displayLayers, + child: const Text('Get map layers'), + ), + ElevatedButton( + onPressed: controller == null ? null : displaySources, + child: const Text('Get map sources'), + ) + ], + ), + )), + ], + ); + } +} diff --git a/example/lib/main.dart b/example/lib/main.dart index 1b74818a..0b7e217e 100644 --- a/example/lib/main.dart +++ b/example/lib/main.dart @@ -8,6 +8,7 @@ import 'package:flutter/foundation.dart'; import 'package:flutter/material.dart'; import 'package:location/location.dart'; import 'package:device_info_plus/device_info_plus.dart'; +import 'package:maplibre_gl_example/get_map_informations.dart'; import 'package:maplibre_gl_example/given_bounds.dart'; import 'animate_camera.dart'; @@ -52,6 +53,7 @@ final List _allPages = [ ClickAnnotationPage(), Sources(), GivenBoundsPage(), + GetMapInfoPage(), ]; class MapsDemo extends StatefulWidget { From 911eca1f60cf9b369dc4ab170a80fa820d306b93 Mon Sep 17 00:00:00 2001 From: mvan Date: Fri, 24 Feb 2023 11:24:21 +0100 Subject: [PATCH 9/9] Removed useless print --- example/lib/get_map_informations.dart | 1 - 1 file changed, 1 deletion(-) diff --git a/example/lib/get_map_informations.dart b/example/lib/get_map_informations.dart index e182f281..4bcb6f4a 100644 --- a/example/lib/get_map_informations.dart +++ b/example/lib/get_map_informations.dart @@ -26,7 +26,6 @@ class _GetMapInfoBodyState extends State { void onMapCreated(MaplibreMapController controller) { setState(() { this.controller = controller; - print('controller set'); }); }