Skip to content

Hello Map

In this guide you will learn how to display an interactive map.

Setup

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

Prerequisites

It is required that you complete the Environment Setup - Flutter Examples guide before starting this guide.

Run the example

Start a terminal/command prompt and go to the hello_map directory, within the flutter examples directory

Build and run the example:

Build and run

hello_map - example flutter screenshot

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

Download project dependencies:

example flutter upgrade screenshot

flutter upgrade

example flutter clean screenshot

run the following terminal commands in the project directory, where the pubspec.yaml file is located:

flutter clean

example flutter pub get screenshot

flutter pub get

Run the example:

flutter run

hello_map - example flutter screenshot

If such a question appears, select the chrome browser; in the above example, press 2.

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, without the line numbers, those are for reference:

1allprojects {
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:

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

Then build the apk:

flutter build apk --debug
or
flutter build apk --release
the apk file is located in the build/app/outputs/apk/debug or build/app/outputs/apk/release subdirectory, for debug or release build respectively, within the current project directory, which is hello_map in this case.
The apk file name is app-release.apk or app-debug.apk
You can copy the apk to an android device using adb, for example:
adb push app-release.apk sdcard

And then click on the apk in the file browser on the device to install and run it.

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

platform :ios, '13.0'

Run pod install in the [ios folder] ./ios/
Then go back to the repository root folder and type flutter build ios to build a Runner.app.
Type flutter run to build and run on an attached device.
You can open the <path/to>/ios/Runner.xcworkspace project in Xcode and execute and debug from there.

How it works

In the hello_map project directory, there is a text file named pubspec.yaml which contains project configuration and dependencies. The most important lines from this file are shown here:

 1name: hello_map
 2version: 1.0.0+1
 3
 4environment:
 5  sdk: '>=2.19.6 <3.0.0'
 6
 7dependencies:
 8  flutter:
 9    sdk: flutter
10  gem_kit:
11    path: plugins/gem_kit
12
13# The following section is specific to Flutter packages.
14flutter:
15  uses-material-design: true

The project must have a name and version. The dependencies list the Flutter SDK, and the gem_kit, Maps for Flutter SDK.

The source code is in hello_map/lib/main.dart

 1import 'package:flutter/material.dart';
 2import 'package:gem_kit/gem_kit_map_controller.dart';
 3import 'package:gem_kit/widget/gem_kit_map.dart';
 4import 'package:gem_kit/api/gem_sdksettings.dart';
 5
 6void main() {
 7  runApp(const MyApp());
 8}
 9
10class MyApp extends StatelessWidget {
11  const MyApp({super.key});
12
13  // This widget is the root of your application.
14  @override
15  Widget build(BuildContext context) {
16    return const MaterialApp(
17      debugShowCheckedModeBanner: false,
18      title: 'Hello Map',
19      home: MyHomePage(),
20    );
21  }
22}
23
24class MyHomePage extends StatefulWidget {
25  const MyHomePage({super.key});
26
27  @override
28  State<MyHomePage> createState() => _MyHomePageState();
29}

First, the dart material package is imported, followed by the gem_kit packages for the map controller, which enables user input such as pan and zoom, the map package which draws the map, and the settings package.

The map is in a widget which is the root of the application.

 1class _MyHomePageState extends State<MyHomePage> {
 2  late GemMapController mapController;
 3  late SdkSettings _sdkSettings;
 4  @override
 5  void initState() {
 6    super.initState();
 7  }
 8
 9  Future<void> onMapCreated(GemMapController controller) async {
10    mapController = controller;
11    SdkSettings.create(mapController.mapId).then((value)
12    {
13        _sdkSettings = value;
14        _sdkSettings.setAppAuthorization("YOUR_API_KEY_TOKEN");
15    });
16  }
17
18  @override
19  Widget build(BuildContext context) {
20    return Scaffold(
21      body: Center(
22        child: Stack(
23          children: [
24            GemMap(
25              onMapCreated: onMapCreated,
26            ),
27          ],
28        ),
29      ),
30    );
31  }
32}

The map is initialized with the map controller and the settings.

Setting the API key

The string "YOUR_API_KEY_TOKEN" should be replaced with your actual API key token string.