Text Search¶
Setup¶
Prerequisites¶
Run the example¶
Start a terminal/command prompt and go to the text_search
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.
text_search/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.text_search"
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 text_search
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 text_search
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: text_search
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# 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 text_search/lib/main.dart
1import 'package:flutter/material.dart';
2import 'package:gem_kit/api/gem_landmark.dart';
3import 'package:gem_kit/api/gem_mapviewrendersettings.dart';
4import 'package:gem_kit/api/gem_routingservice.dart';
5import 'package:gem_kit/api/gem_sdksettings.dart';
6import 'package:gem_kit/api/gem_types.dart';
7import 'package:gem_kit/gem_kit_map_controller.dart';
8import 'package:gem_kit/widget/gem_kit_map.dart';
9import '../search_page.dart';
10
11void main() {
12 runApp(const MyApp());
13}
14
15class MyApp extends StatelessWidget {
16 const MyApp({super.key});
17
18 // This widget is the root of your application.
19 @override
20 Widget build(BuildContext context) {
21 return const MaterialApp(
22 debugShowCheckedModeBanner: false,
23 title: 'Search example',
24 home: MyHomePage(),
25 );
26 }
27}
28
29class MyHomePage extends StatefulWidget {
30 const MyHomePage({super.key});
31
32 @override
33 State<MyHomePage> createState() => _MyHomePageState();
34}
First, the dart
material package is imported, followed by the gem_kit
packages such as the map controller, which enables user input such as pan and zoom,
the map package which draws the map, the landmark and routingservice packages
for georeferencing locations on the map, and the settings package.
The map is in a widget which is the root of the application.
Setting the API key¶
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 }
The map is initialized with the map controller and the settings.
The string |
Search¶
1 // Custom method for navigating to search screen
2 _onPressed(BuildContext context) async {
3 // Taking the coordinates at the center of the screen as reference coordinates for search.
4 final x = MediaQuery.of(context).size.width / 2;
5 final y = MediaQuery.of(context).size.height / 2;
6 final mapCoords = await mapController
7 .transformScreenToWgs(XyType(x: x.toInt(), y: y.toInt()));
8
9 // Navigating to search screen. The result will be the selected search result(Landmark)
10 final result = await Navigator.push(
11 context,
12 MaterialPageRoute(
13 builder: (context) => SearchPage(
14 controller: mapController,
15 coordinates: mapCoords!,
16 ),
17 ));
18
19 // Creating a list of landmarks to highlight.
20 LandmarkList landmarkList = await LandmarkList.create(mapController.mapId);
21
22 // Adding the result to the landmark list.
23 landmarkList.push_back(result);
24 final coords = await result.getCoordinates();
25
26 // Activating the highlight
27 mapController.activateHighlight(landmarkList,
28 renderSettings: RenderSettings());
29
30 // Centering the map on the desired coordinates
31 mapController.centerOnCoordinates(coords);
32 }
landmarkList
is created, based on
the search results.result.getCoordinates();
and then the map
is centered on the search result, that is, its coordinates:mapController.centerOnCoordinates(coords);
1 @override
2 Widget build(BuildContext context) {
3 return Scaffold(
4 body: Center(
5 child: GemMap(
6 onMapCreated: onMapCreated,
7 ),
8 ),
9 floatingActionButtonLocation: FloatingActionButtonLocation.endTop,
10 floatingActionButton: FloatingActionButton(
11 backgroundColor: Colors.deepPurple[900],
12 onPressed: () => _onPressed(context),
13 child: const Icon(Icons.search),
14 ),
15 );
16 }
17}
The purple button is added at the top right to go to the text search page.
The text search page source code is in text_search/lib/search_page.dart