Skip to content

Map Perspective

In this guide you will learn how to render an interactive map and toggle the map between 2D mode (looking vertically down toward the map) and 3D perspective mode (looking at the map at an angle, partially toward the horizon).

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 map_perspective directory, within the flutter examples directory

Build and run the example:

Build and run

map_perspective - 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. map_perspective/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

map_perspective - 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.map_perspective"
 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 map_perspective 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 map_perspective 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: map_perspective
 2version: 1.0.0+1
 3
 4environment:
 5  sdk: '>=3.0.5 <4.0.0'
 6
 7dependencies:
 8  flutter:
 9    sdk: flutter
10  gem_kit:
11    path: plugins/gem_kit
12
13  cupertino_icons: ^1.0.2
14
15# The following section is specific to Flutter packages.
16flutter:
17  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 map_perspective/lib/main.dart

 1import 'package:flutter/cupertino.dart';
 2import 'package:flutter/material.dart';
 3import 'package:gem_kit/api/gem_mapviewpreferences.dart';
 4import 'package:gem_kit/api/gem_sdksettings.dart';
 5import 'package:gem_kit/gem_kit_map_controller.dart';
 6import 'package:gem_kit/widget/gem_kit_map.dart';
 7
 8void main() {
 9  runApp(const PerspectiveMapApp());
10}
11
12class PerspectiveMapApp extends StatelessWidget {
13  const PerspectiveMapApp({super.key});
14
15  @override
16  Widget build(BuildContext context) {
17    return MaterialApp(
18        title: 'Perspective Map',
19        debugShowCheckedModeBanner: false,
20        theme: ThemeData(
21          colorScheme: ColorScheme.fromSeed(seedColor: Colors.deepPurple),
22          useMaterial3: true,
23        ),
24        home: const PerspectiveMapPage());
25  }
26}

The dart material package is imported, as well as 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 and mapviewpreferences packages.

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

 1class PerspectiveMapPage extends StatefulWidget {
 2  const PerspectiveMapPage({super.key});
 3
 4  @override
 5  State<PerspectiveMapPage> createState() => _PerspectiveMapPageState();
 6}
 7
 8class _PerspectiveMapPageState extends State<PerspectiveMapPage> {
 9  late GemMapController _mapController;
10
11  // Map preferences are used to change map perspective
12  late MapViewPreferences _mapPreferences;
13
14  late bool _isInPerspectiveView = false;
15
16  // Tilt angle for perspective view
17  final double _3dViewAngle = 65;
18
19  // Tilt angle for orthogonal/vertical view
20  final double _2dViewAngle = 90;
21
22  final token = 'YOUR_API_KEY_TOKEN';
23
24  @override
25  Widget build(BuildContext context) {
26    return Scaffold(
27      appBar: AppBar(
28        backgroundColor: Colors.deepPurple[900],
29        title: const Text('Perspective Map',
30            style: TextStyle(color: Colors.white)),
31        actions: [
32          IconButton(
33              onPressed: _onChangePersectiveButtonPressed,
34              icon: Icon(
35                _isInPerspectiveView
36                    ? CupertinoIcons.view_2d
37                    : CupertinoIcons.view_3d,
38                color: Colors.white,
39              ))
40        ],
41      ),
42      body: GemMap(
43        onMapCreated: _onMapCreatedCallback,
44      ),
45    );
46  }
47
48  // The callback for when map is ready to use
49  _onMapCreatedCallback(GemMapController controller) async {
50    // Save controller for further usage
51    _mapController = controller;
52
53    _mapPreferences = await controller.preferences();
54
55    final settings = await SdkSettings.create(controller.mapId);
56
57    settings.setAppAuthorization(token);
58  }
59
60  _onChangePersectiveButtonPressed() async {
61    setState(() => _isInPerspectiveView = !_isInPerspectiveView);
62
63    // Based on view type, set the view angle
64    if (_isInPerspectiveView) {
65      _mapPreferences.setTiltAngle(_3dViewAngle);
66    } else {
67      _mapPreferences.setTiltAngle(_2dViewAngle);
68    }
69  }
70}

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

The map view angle is toggled between perspective/tilted (3D) mode and looking vertically downward (2D) mode using
_mapPreferences.setTiltAngle(_3dViewAngle);
and
_mapPreferences.setTiltAngle(_2dViewAngle);
respectively.

Setting the API key

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

map_perspective - example flutter screenshot