Free Text Search ¶
Setup ¶
First, get an API key token, see the Getting Started guide.
Download the Maps & Navigation SDK for Android archive file
Download the
Search
project
archive file or clone the project with Git
See the Configure Android Example guide.
Run the example ¶
In Android Studio, from the
File
menu, select
Sync
Project
with
Gradle
Files
|
|||
How it works ¶

You can open the MainActivity.kt file to see how the interactive map is rendered.
1class MainActivity : AppCompatActivity()
2{
3 private var searchService = SearchService()
In
MainActivity
a
SearchService()
instance is obtained.
1searchInput.setOnQueryTextListener(
2 object : SearchView.OnQueryTextListener
3 {
4 override fun onQueryTextSubmit(query: String?): Boolean
5 {
6 searchInput.clearFocus()
7 return true
8 }
9 override fun onQueryTextChange(newText: String?): Boolean
10 {
11 applyFilter(newText ?: "")
12 return true
13 }
14 }
15)
If the user input text to search changes or is submitted, the text is set
in the filter to use for searching by the
applyFilter()
function.
1private fun search(filter: String): Int
2{
3 return SdkCall.execute
4 {
5 // Get the current position of the user.
6 val position = PositionService().getPosition()
7 val latitude = position?.getLatitude() ?: 0.0
8 val longitude = position?.getLongitude() ?: 0.0
9 val coordinates = Coordinates(latitude, longitude)
10 searchService.preferences.setSearchAddresses(true)
11 searchService.preferences.setSearchMapPOIs(true)
12 searchService.searchByFilter(filter, coordinates) onCompleted@{ results, reason, _ ->
13 val gemError = SdkError.fromInt(reason)
14 if (gemError == SdkError.Cancel) return@onCompleted
15 val adapter = CustomAdapter(results)
16 listView?.adapter = adapter
17 progressBar?.visibility = View.GONE
18 if (reason == SdkError.NoError.value && results.isEmpty())
19 {
20 Toast.makeText(
21 this@MainActivity, "No results!", Toast.LENGTH_SHORT
22 ).show()
23 }
24 }
25 } ?: SdkError.Cancel.value
26}
search()
function uses the
SearchService()
instance to first
set preferences to search for both addresses and POIs (points of interest)
searchService.preferences.setSearchAddresses(true)
searchService.preferences.setSearchMapPOIs(true)
searchByFilter
and also defines
an
onCompleted
callback which receives the results:
searchService.searchByFilter(filter,
coordinates)
onCompleted@{