Skip to content

Map Compass

This example demonstrates how to render a compass icon that displays the heading rotation of an interactive map. The compass indicates the direction where 0 degrees is north, 90 degrees is east, 180 degrees is south, and 270 degrees is west. You will also learn how to rotate the map back to its default north-up orientation.

map_compass - example flutter screenshot

Setup

First, get an API key token, see the Getting Started guide.

Prerequisites

Make sure you completed the Environment Setup - Flutter Examples guide before starting this guide.

Build and run

Go to the map_compass directory within the Flutter examples directory - this is the name of this example project.

Note - the gem_kit directory containing the Maps SDK for Flutter should be in the plugins directory of the example, e.g. example_pathname/plugins/gem_kit - see the environment setup guide above.

Run: flutter pub get

Configure the native parts:

First, verify that the ANDROID_SDK_ROOT environment variable is set to the root path of your android SDK.

In android/build.gradle add the maven block as shown, within the allprojects block, for both debug and release builds:

1    allprojects {
2        repositories {
3            google()
4            mavenCentral()
5            maven {
6               url "${rootDir}/../plugins/gem_kit/android/build"
7            }
8        }
9    }

in android/app/build.gradle within the android block, in the defaultConfig block, the android SDK version minSdk must be set as shown below.

Additionally, for release builds, in android/app/build.gradle, within the android block, add the buildTypes block as shown:

Replace example_pathname with the actual project pathname

 1android {
 2    defaultConfig {
 3        applicationId "com.magiclane.gem_kit.examples.example_pathname"
 4        minSdk 21
 5        targetSdk flutter.targetSdk
 6        versionCode flutterVersionCode.toInteger()
 7        versionName flutterVersionName
 8    }
 9    buildTypes {
10        release {
11            minifyEnabled false
12            shrinkResources false
13
14            // TODO: Add your own signing config for the release build.
15            // Signing with the debug keys for now, so `flutter run --release` works.
16            signingConfig signingConfigs.debug
17        }
18    }
19}

In the ios/Podfile configuration file, at the top, set the minimum ios platform version to 14 like this:

platform :ios, '14.0'

We recommend you to run these commands after you copy the gem_kit into your project: flutter clean flutter pub get and cd ios pod install

Then run the project:

flutter run --debug
or
flutter run --release

How it works

map_compass - example flutter screenshot

The example app demonstrates the following features:

  • Display a map and sync a compass to its rotation.

  • Align north up the map.

Map Initialization

 1import 'package:gem_kit/core.dart';
 2import 'package:gem_kit/map.dart';
 3import 'package:flutter/material.dart';
 4import 'dart:typed_data';
 5
 6Future<void> main() async {
 7  const projectApiToken = String.fromEnvironment('GEM_TOKEN');
 8  await GemKit.initialize(appAuthorization: projectApiToken);
 9  runApp(const MyApp());
10}
11
12class MyApp extends StatelessWidget {
13  const MyApp({super.key});
14
15  @override
16  Widget build(BuildContext context) {
17    return const MaterialApp(
18      debugShowCheckedModeBanner: false,
19      title: 'Map Compass',
20      home: MyHomePage(),
21    );
22  }
23}

The dart material package is imported, along with the required gem_kit packages. The map is integrated into a widget that serves as the root of the application.

Map Display and UI Components

 1class _MyHomePageState extends State<MyHomePage> {
 2  late GemMapController mapController;
 3
 4  double compassAngle = 0;
 5  late Uint8List compassImage;
 6
 7  @override
 8  void initState() {
 9    compassImage = _compassImage();
10    super.initState();
11  }
12
13  @override
14  Widget build(BuildContext context) {
15    return Scaffold(
16      appBar: AppBar(
17        backgroundColor: Colors.deepPurple[900],
18        title: const Text(
19          "Map Compass",
20          style: TextStyle(color: Colors.white),
21        ),
22      ),
23      body: Stack(
24        children: [
25          GemMap(
26            onMapCreated: _onMapCreated,
27          ),
28          Positioned(
29            right: 12,
30            top: 12,
31            child: InkWell(
32              // Align the map north to up.
33              onTap: () => mapController.alignNorthUp(),
34              child: Transform.rotate(
35                angle: -compassAngle * (3.141592653589793 / 180),
36                child: Container(
37                  padding: const EdgeInsets.all(3),
38                  decoration: const BoxDecoration(
39                    shape: BoxShape.circle,
40                    color: Colors.white,
41                  ),
42                  child: SizedBox(
43                    width: 40,
44                    height: 40,
45                    child: Image.memory(
46                      compassImage,
47                      gaplessPlayback: true,
48                    ),
49                  ),
50                ),
51              ),
52            ),
53          )
54        ],
55      ),
56    );
57  }
58
59  void _onMapCreated(GemMapController controller) {
60    mapController = controller;
61    mapController.registerOnMapAngleUpdate(
62      (angle) => setState(() => compassAngle = angle)
63    );
64  }
65
66  Uint8List _compassImage() {
67    final image = SdkSettings.getImageById(
68      id: EngineMisc.compassEnableSensorOFF.id,
69      size: const Size(100, 100)
70    );
71    return image;
72  }
73}

Clean-Up

The dispose() method ensures that resources are properly released when the widget is destroyed.

1@override
2void dispose() {
3  GemKit.release();
4  super.dispose();
5}

This callback function is triggered when the interactive map is initialized and ready for use. The map angle update callback is registered to ensure that the compass icon reflects the map’s current heading.

When the map’s heading angle changes (e.g., due to rotation), the compassAngle variable is updated, causing the compass widget to rotate accordingly. Tapping on the compass icon calls mapController.alignNorthUp(), which reorients the map so that 0 degrees (north) is at the top of the screen.

Flutter Examples

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