Map styles
You can change the look of the map and its objects by applying styles. Styles are the rules of displaying map data that define in which order and in which form to draw it.
Connecting a style to a map
You can connect styles from the Style editor to a map added to a mobile application. The Style editor allows you to configure the rendering of any map components.
To connect a style from the Style editor:
-
Create your map style using the Style editor: see the Creating a style instruction. You can use prepared style templates.
-
Export the style: see the Connecting a style for Flutter SDK instruction.
-
Move the downloaded style to the project with the mobile application.
-
Connect the style using the loadStyle method and specify the style object as the
style
parameter:Future<void> _initializeMapOptions() async { // Asynchronous loading of the map style from file final style = await sdk.StyleBuilder(_sdkContext) .loadStyle(sdk.File('style.2gis')) .value; // Creating MapOptions with the loaded style final mapOptions = sdk.MapOptions(style: style); } // Now MapOptions can be passed to MapWidget sdk.MapWidget( sdkContext: sdkContext, mapOptions: mapOptions, controller: mapWidgetController, );
Switching a theme
Map styles can contain multiple themes (for example, day and night) that you can switch between in the runtime without loading an additional style.
When creating a map
You can specify themes when creating a map using the appearance parameter in MapAppearance
:
final sdk.AutomaticAppearance appearance;
appearance = const sdk.AutomaticAppearance(
sdk.MapTheme.defaultDayTheme(),
sdk.MapTheme.defaultNightTheme(),
);
final options = sdk.MapOptions(appearance:appearance);
After creating a map
You can change the theme after creating a map using the appearance parameter in MapWidgetController
:
final sdk.AutomaticAppearance appearance;
appearance = const sdk.AutomaticAppearance(
sdk.MapTheme.defaultDayTheme(),
sdk.MapTheme.defaultNightTheme(),
);
mapWidgetController.appearance = appearance;
Managing style layers
You can dynamically enable or disable the display of a style layer on the map. To enable a layer (for example, a layer with parking lots):
-
Initialize a global variable:
-
Open the Style editor.
-
In the My styles block, open a card of the required style.
-
In the Layers section, select the layer you want to enable.
-
In the Data (JSON) block, edit the JSON file and add the required global variable (
global
). For example, for the parking lots layer, it is theparkingOn
variable:A global variable can be used in multiple layers, but only within one style.
-
-
Export and connect the style: see the Connecting a style for Flutter SDK instruction.
-
Set the attribute to
true
using the setAttributeValue() method. For example, to enable the parking lots layer:map.attributes.setAttributeValue("parkingOn", sdk.AttributeValue.boolean(true));