Center on coordinates¶
Setup¶
Prerequisites¶
Run the example¶
Start a terminal/command prompt and go to the center_coordinates
directory,
within the flutter examples directory
Build and run the example:
Build and run¶
Note - the gem_kit
directory containing the Maps SDK for Flutter
should be in the plugins
directory of the example, e.g.
center_coordinates/plugins/gem_kit
- see the environment setup guide above.
Download project dependencies:
flutter upgrade
run the following terminal commands in the project directory,
where the pubspec.yaml
file is located:
flutter clean
flutter pub get
Run the example:
flutter run
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 minSdkVersion
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.center_coordinates"
4 minSdkVersion 21
5 targetSdkVersion flutter.targetSdkVersion
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
flutter build apk --release
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 center_coordinates
in this case.app-release.apk
or app-debug.apk
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'
pod install
in the [ios folder] ./ios/
flutter build ios
to build a Runner.app.flutter run
to build and run on an attached device.<path/to>/ios/Runner.xcworkspace
project in Xcode
and execute and debug from there.How it works¶
In the center_coordinates
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: simulate_route
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 center_coordinates/lib/main.dart
1import 'package:flutter/material.dart';
2import 'package:gem_kit/api/gem_coordinates.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 CenterCoordinatesApp());
10}
11
12class CenterCoordinatesApp extends StatelessWidget {
13 const CenterCoordinatesApp({super.key});
14
15 @override
16 Widget build(BuildContext context) {
17 return MaterialApp(
18 debugShowCheckedModeBanner: false,
19 title: 'Center Coordinates',
20 theme: ThemeData(
21 colorScheme: ColorScheme.fromSeed(seedColor: Colors.deepPurple),
22 useMaterial3: true,
23 ),
24 home: CenterCoordinatesPage(),
25 );
26 }
27}
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 coordinates,
settings and mapviewpreferences packages.
The map is in a widget which is the root of the application.
1class CenterCoordinatesPage extends StatelessWidget {
2 CenterCoordinatesPage({super.key});
3
4 late GemMapController _mapController;
5
6 final token = 'YOUR_API_KEY_TOKEN';
7
8 @override
9 Widget build(BuildContext context) {
10 return Scaffold(
11 appBar: AppBar(
12 backgroundColor: Colors.deepPurple[900],
13 title: const Text('Center Coordinates',
14 style: TextStyle(color: Colors.white)),
15 actions: [
16 IconButton(
17 onPressed: _onCenterCoordinatesButtonPressed,
18 icon: const Icon(
19 Icons.adjust,
20 color: Colors.white,
21 ))
22 ],
23 ),
24 body: GemMap(
25 onMapCreated: _onMapCreatedCallback,
26 ),
27 );
28 }
29
30 // The callback for when map is ready to use
31 _onMapCreatedCallback(GemMapController controller) async {
32 // Save controller for further usage
33 _mapController = controller;
34
35 final settings = await SdkSettings.create(controller.mapId);
36
37 settings.setAppAuthorization(token);
38 }
39
40 _onCenterCoordinatesButtonPressed() {
41 // Predefined coordinates for Rome, Italy
42 final targetCoordinates =
43 Coordinates(latitude: 41.902782, longitude: 12.496366);
44
45 // Create an animation (optional)
46 final animation = GemAnimation(type: EAnimation.AnimationLinear);
47
48 // Use the map controller to center on coordinates
49 _mapController.centerOnCoordinates(targetCoordinates, animation: animation);
50 }
51}
The map is initialized with the map controller and the settings.
The Coordinates()
are defined and a GemAnimation()
flies
the camera to the location specified by the coordinates, using
_mapController.centerOnCoordinates()
Setting the API key¶
The string |