Skip to content

Map perspective

This example demonstrates how to toggle the map view angle between 2D (vertical look-down at the map) and 3D (perspective, tilted map, looking toward the horizon).

map_perspective - 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_perspective directory within the Flutter examples directory, which is the name of this example project.

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. example_pathname/plugins/gem_kit - see the environment setup guide above. Replace example_pathname with the actual example path name, such as address_search

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:

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:

Replace example_pathname with the actual example pathname, such as center_coordinates

 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}

Then run the project:

flutter run --debug
or
flutter run --release

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

platform :ios, '13.0'

Then run the project:

flutter run --debug
or
flutter run --release

How it works

map_perspective - example flutter screenshot

map_perspective - example flutter screenshot

 1import 'package:gem_kit/core.dart';
 2import 'package:gem_kit/map.dart';
 3import 'package:flutter/cupertino.dart';
 4import 'package:flutter/material.dart';
 5
 6Future<void> main() async {
 7  const projectApiToken = String.fromEnvironment('GEM_TOKEN');
 8
 9  await GemKit.initialize(appAuthorization: projectApiToken);
10
11  runApp(const MyApp());
12}
13
14class MyApp extends StatelessWidget {
15  const MyApp({super.key});
16
17  @override
18  Widget build(BuildContext context) {
19    return const MaterialApp(
20      debugShowCheckedModeBanner: false,
21      title: 'Map Perspective',
22      home: MyHomePage(),
23    );
24  }
25}
26
27class MyHomePage extends StatefulWidget {
28  const MyHomePage({super.key});
29
30  @override
31  State<MyHomePage> createState() => _MyHomePageState();
32}
33
34class _MyHomePageState extends State<MyHomePage> {
35  // Map preferences are used to change map perspective
36  late MapViewPreferences _mapPreferences;
37
38  late bool _isInPerspectiveView = false;
39
40  // Tilt angle for perspective view
41  final double _3dViewAngle = 30;
42
43  // Tilt angle for orthogonal/vertical view
44  final double _2dViewAngle = 90;
45
46  @override
47  void dispose() {
48    GemKit.release();
49    super.dispose();
50  }
51
52  // The callback for when map is ready to use
53  void _onMapCreated(GemMapController controller) async {
54    _mapPreferences = controller.preferences;
55  }
56
57  void _onChangePerspectiveButtonPressed() async {
58    setState(() => _isInPerspectiveView = !_isInPerspectiveView);
59
60    // Based on view type, set the view angle
61    if (_isInPerspectiveView) {
62      _mapPreferences.tiltAngle = _3dViewAngle;
63    } else {
64      _mapPreferences.tiltAngle = _2dViewAngle;
65    }
66  }
67}

The dart material package is imported, along with the required gem_kit packages.

The map view angle is set using _mapPreferences.tiltAngle, where the map preferences are obtained from the GemMapController controller that is passed into the _onMapCreated() callback, which is called when the interactive map is initialized and ready to use.

The map view angle is set to 90 degrees (looking vertically downward at the map) for 2D mode.

The map view angle is set to 30 degrees (looking 30 degrees downward from the horizon) for 3D mode.

Flutter Examples

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