Skip to main content
GuidesAPI ReferenceExamples

Free Text Search

Estimated reading time: 3 minutes

In this guide you will learn how to search on an interactive map.

Setup

  1. Get your Magic Lane API key token: if you do not have a token, see the Getting Started guide.
  2. Download the Maps & Navigation SDK for Android archive file.
  3. Download the SendDebugInfo project archive file or clone the project with git.
  4. See the Configure Android Example guide.

Run the example

In Android Studio, from the File menu, select Sync Project with Gradle Files.


  1. An android device should be connected via USB cable.
  2. Press SHIFT+F10 to compile, install and run the example on the android device.
  3. In the search bar at the top, start typing the name of a point of interest, such as a town, airport, etc, and text results will start to appear below as you type.

How it works

You can open the MainActivity.kt file to see how the interactive map is rendered.

How it works

class MainActivity : AppCompatActivity()
{
private var searchService = SearchService()

In MainActivity a SearchService() instance is obtained.

searchInput.setOnQueryTextListener(
object : SearchView.OnQueryTextListener
{
override fun onQueryTextSubmit(query: String?): Boolean
{
searchInput.clearFocus()
return true
}
override fun onQueryTextChange(newText: String?): Boolean
{
applyFilter(newText ?: "")
return true
}
}
)

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.

private fun search(filter: String): Int
{
return SdkCall.execute
{
// Get the current position of the user.
val position = PositionService().getPosition()
val latitude = position?.getLatitude() ?: 0.0
val longitude = position?.getLongitude() ?: 0.0
val coordinates = Coordinates(latitude, longitude)
searchService.preferences.setSearchAddresses(true)
searchService.preferences.setSearchMapPOIs(true)
searchService.searchByFilter(filter, coordinates) onCompleted@{ results, reason, _ ->
val gemError = SdkError.fromInt(reason)
if (gemError == SdkError.Cancel) return@onCompleted
val adapter = CustomAdapter(results)
listView?.adapter = adapter
progressBar?.visibility = View.GONE
if (reason == SdkError.NoError.value && results.isEmpty())
{
Toast.makeText(
this@MainActivity, "No results!", Toast.LENGTH_SHORT
).show()
}
}
} ?: SdkError.Cancel.value
}

The 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) and then it starts the search with searchByFilter and also defines an onCompleted callback which receives the results:
searchService.searchByFilter(filter, coordinates) onCompleted@{

Android Examples

Maps SDK for Android Examples can be downloaded or cloned with Git.