Skip to main content
GuidesAPI ReferenceExamplesFAQ

Assets Map Styles

Estimated reading time: 3 minutes

This example showcases how to build a Flutter app featuring an interactive map with a custom style, seamlessly imported from the assets folder, using the Maps SDK for Flutter.

How it works

The example app demonstrates the following features:

  • Main App Setup : Initializes GemKit and displays a map view with an AppBar.
  • Map Styling : The user taps an icon to apply a style, loaded from a file in the app assets folder.
  • Loading Feedback : Displays a loading message until the style is fully applied.
hello_maphello_map
Map styles loadingApplied map style

Add style to project

In the root directory of your project, create a new folder named assets. Specify the path to the assets folder in the pubspec.yaml file. Modify the file as follows:

  flutter:
uses-material-design: true

assets:
- assets/

Place a .style file inside the assets directory.

UI and Map Integration

class MyHomePage extends StatefulWidget {
const MyHomePage({super.key});


State<MyHomePage> createState() => _MyHomePageState();
}

class _MyHomePageState extends State<MyHomePage> {
late GemMapController _mapController;
bool _isStyleLoaded = false;


void dispose() {
GemKit.release();
super.dispose();
}


Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
backgroundColor: Colors.deepPurple[900],
title: const Text('Assets Map Style', style: TextStyle(color: Colors.white)),
actions: [
if (!_isStyleLoaded)
IconButton(
onPressed: _applyStyle,
icon: Icon(Icons.map, color: Colors.white),
),
],
),
body: GemMap(
key: ValueKey("GemMap"),
onMapCreated: _onMapCreated,
appAuthorization: projectApiToken,
),
);
}

void _onMapCreated(GemMapController controller) async {
_mapController = controller;
}

This code sets up the main screen with a map and a button that triggers the _applyStyle method to load a custom style file.

Loading and Applying Map Styles

This code loads the .style file as bytes, applies it to the map with a smooth transition, and centers the map on specified coordinates.

Future<void> _applyStyle() async {
_showSnackBar(context, message: "The map style is loading.");

await Future<void>.delayed(Duration(milliseconds: 250));
final styleData = await _loadStyle();

_mapController.preferences.setMapStyleByBuffer(styleData, smoothTransition: true);

setState(() {
_isStyleLoaded = true;
});

if (mounted) {
ScaffoldMessenger.of(context).hideCurrentSnackBar();
}
_mapController.centerOnCoordinates(Coordinates(latitude: 45, longitude: 20), zoomLevel: 25);
}

Loading the Style File

This method reads the .style file from assets and returns the data as Uint8List bytes.

Future<Uint8List> _loadStyle() async {
final data = await rootBundle.load('assets/Basic_1_Oldtime-1_21_656.style');
return data.buffer.asUint8List();
}

Flutter Examples

Maps SDK for Flutter Examples can be downloaded or cloned with Git