Assets Map Styles
This example demonstrates how to load and apply custom map styles from assets. It shows how to fetch a .style file and apply it to the map dynamically.
Overview
The example demonstrates the following features:
- Loading custom map style files from assets
- Applying map styles using
setMapStyleByBuffer - Centering the map on specific coordinates after style application
- Error handling for asset loading
Code Implementation
Initialize GemKit and Create Map
Load and Apply Map Style
Live Demo
Key Features
- Custom Map Styles: Load and apply
.stylefiles from your assets folder. - Dynamic Style Application: Apply styles at runtime without reloading the map.
- Error Handling: Gracefully handle style loading failures with user feedback.
- Map Centering: Automatically center and zoom the map after applying a new style.
Explanation of Key Components
- map.preferences.setMapStyleByBuffer: Applies a custom map style from a binary buffer (Uint8Array).
- fetch API: Used to load the
.stylefile as an ArrayBuffer from the public assets folder. - map.centerOnCoordinates: Centers the map on specified coordinates with a zoom level.
- Uint8Array: Converts the ArrayBuffer to the format expected by the SDK.
Asset Preparation
When preparing custom map styles:
- File Format: Use
.stylefiles compatible with the Maps SDK. - Location: Place style files in the public folder or serve them from a CDN.
- File Size: Keep file sizes reasonable for faster loading.
- Testing: Test styles before deployment to ensure compatibility.
Next Steps
- Map Gestures - Learn about map interaction controls.
- Map Compass - Add compass functionality to your map.
- Custom Position Icon - Customize the position tracker icon.