Text search¶
Setup¶
Prerequisites¶
Build and run¶
Go to the text_search
directory,
within the flutter examples directory - that is the name of this example project.
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:
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:
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
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
flutter run --release
How it works¶
1import 'package:gem_kit/core.dart';
2import 'package:gem_kit/landmark_store.dart';
3import 'package:gem_kit/map.dart';
4import 'search_page.dart';
5import 'package:flutter/material.dart';
Note that the source file search_page.dart
also includes:
1import 'package:gem_kit/search.dart';
2import 'dart:async';
The dart
material package is imported, as well as the
required gem_kit
packages.
1void onMapCreated(GemMapController controller) {
2 _mapController = controller;
3}
This callback function is called when the interactive map is initialized and ready to use.
1// Custom method for navigating to search screen
2void _onSearchButtonPressed(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 = _mapController.transformScreenToWgs(XyType(x: x.toInt(), y: y.toInt()));
7
8// Navigating to search screen. The result will be the selected search result(Landmark)
9 final result = await Navigator.of(context).push(MaterialPageRoute<dynamic>(
10 builder: (context) => SearchPage(coordinates: mapCoords!),
11 ));
12
13 if (result is Landmark) {
14 // Retrieves the LandmarkStore with the given name.
15 var historyStore = LandmarkStoreService.getLandmarkStoreByName("History");
16
17 // If there is no LandmarkStore with this name, then create it.
18 historyStore ??= LandmarkStoreService.createLandmarkStore("History");
19
20 // Add the landmark to the store.
21 historyStore.addLandmark(result);
22
23 // Activating the highlight
24 _mapController.activateHighlight([result], renderSettings: RenderSettings());
25
26 // Centering the map on the desired coordinates
27 _mapController.centerOnCoordinates(result.coordinates);
28 }
29}
This is the method to navigate to the search screen, defined in the SearchPage()
widget,
in search_page.dart
1void _onSearchSubmitted(String text) {
2 SearchPreferences preferences = SearchPreferences(maxMatches: 40, allowFuzzyResults: true);
3
4 search(text, widget.coordinates, preferences: preferences);
5}
As text is typed in the text field by the user, the _onSearchSubmitted()
function is
called, which creates a preferences instance and then the search()
function is called.
The coordinates of the current location displayed on the map widget.coordinates
are used for comparison, to compute the distance to the positions of the search results.
1// Search method. Text and coordinates parameters are mandatory, preferences are optional.
2Future<void> search(String text, Coordinates coordinates, {SearchPreferences? preferences}) async {
3 Completer<List<Landmark>> completer = Completer<List<Landmark>>();
4
5// Calling the search method from the sdk.
6// (err, results) - is a callback function that calls when the computing is done.
7// err is an error code, results is a list of landmarks
8 SearchService.search(text, coordinates, preferences: preferences, (err, results) async {
9 // If there is an error or there aren't any results, the method will return an empty list.
10 if (err != GemError.success || results == null) {
11 completer.complete([]);
12 return;
13 }
14 if (!completer.isCompleted) completer.complete(results);
15 });
16 final result = await completer.future;
17 setState(() {
18 landmarks = result;
19 });
20}
The search()
function calls the SearchService.search()
, obtains a list of landmarks
in results
, which are then displayed as a text list. Tapping on a result causes the map
to be centered on the position of that search result.
This is done in the method that calls SearchPage()
above.
_mapController.activateHighlight([result], renderSettings: RenderSettings());
_mapController.centerOnCoordinates(result.coordinates);