@@ -57,38 +57,43 @@ class MapLibreMapController extends MapLibrePlatform
5757
5858 @override
5959 Future <void > initPlatform (int id) async {
60- if (_creationParams.containsKey ('initialCameraPosition' )) {
61- final camera = _creationParams['initialCameraPosition' ];
62- _dragEnabled = _creationParams['dragEnabled' ] ?? true ;
63-
64- _map = MapLibreMap (
65- MapOptions (
66- container: _mapElement,
67- center: LngLat (camera['target' ][1 ], camera['target' ][0 ]),
68- zoom: camera['zoom' ],
69- bearing: camera['bearing' ],
70- pitch: camera['tilt' ],
71- preserveDrawingBuffer: _creationParams['webPreserveDrawingBuffer' ],
72- attributionControl: false , //avoid duplicate control
73- ),
74- );
75- _map.on ('style.load' , _onStyleLoaded);
76- _map.on ('click' , _onMapClick);
77- // long click not available in web, so it is mapped to double click
78- _map.on ('dblclick' , _onMapLongClick);
79- _map.on ('movestart' , _onCameraMoveStarted);
80- _map.on ('move' , _onCameraMove);
81- _map.on ('moveend' , _onCameraIdle);
82- _map.on ('resize' , (_) => _onMapResize ());
83- _map.on ('styleimagemissing' , _loadFromAssets);
84- if (_dragEnabled) {
85- _map.on ('mouseup' , _onMouseUp);
86- _map.on ('mousemove' , _onMouseMove);
87- }
88-
89- _initResizeObserver ();
60+ final camera =
61+ _creationParams['initialCameraPosition' ] as Map <String , dynamic >? ;
62+ final styleString = _sanitizeStyleObject (_creationParams['styleString' ]);
63+ _dragEnabled = _creationParams['dragEnabled' ] ?? true ;
64+
65+ _map = MapLibreMap (
66+ MapOptions (
67+ container: _mapElement,
68+ center: (camera != null )
69+ ? LngLat (camera['target' ][1 ], camera['target' ][0 ])
70+ : null ,
71+ zoom: camera? ['zoom' ],
72+ bearing: camera? ['bearing' ],
73+ pitch: camera? ['tilt' ],
74+ style: styleString,
75+ preserveDrawingBuffer: _creationParams['webPreserveDrawingBuffer' ],
76+ attributionControl: false , //avoid duplicate control
77+ ),
78+ );
79+ _map.on ('style.load' , _onStyleLoaded);
80+ _map.on ('click' , _onMapClick);
81+ // long click not available in web, so it is mapped to double click
82+ _map.on ('dblclick' , _onMapLongClick);
83+ _map.on ('movestart' , _onCameraMoveStarted);
84+ _map.on ('move' , _onCameraMove);
85+ _map.on ('moveend' , _onCameraIdle);
86+ _map.on ('resize' , (_) => _onMapResize ());
87+ _map.on ('styleimagemissing' , _loadFromAssets);
88+ if (_dragEnabled) {
89+ _map.on ('mouseup' , _onMouseUp);
90+ _map.on ('mousemove' , _onMouseMove);
9091 }
91- Convert .interpretMapLibreMapOptions (_creationParams['options' ], this );
92+
93+ _initResizeObserver ();
94+
95+ final options = _creationParams['options' ] ?? {};
96+ Convert .interpretMapLibreMapOptions (options, this );
9297 }
9398
9499 void _initResizeObserver () {
@@ -855,7 +860,20 @@ class MapLibreMapController extends MapLibrePlatform
855860 }
856861 _interactiveFeatureLayerIds.clear ();
857862
858- _map.setStyle (styleObject, {'diff' : false });
863+ final sanitizedStyle = _sanitizeStyleObject (styleObject);
864+ _map.setStyle (sanitizedStyle, {'diff' : false });
865+ }
866+
867+ /// Sanitizes the style object to ensure it is in the correct format.
868+ /// If the style object is a JSON string, it decodes it into a Map.
869+ /// If it is already a Map or other type, it returns it as is.
870+ dynamic _sanitizeStyleObject (dynamic styleObject) {
871+ if (styleObject is String &&
872+ (styleObject.startsWith ('{' ) || styleObject.startsWith ('[' ))) {
873+ return jsonDecode (styleObject);
874+ } else {
875+ return styleObject;
876+ }
859877 }
860878
861879 @override
0 commit comments