Quickstart

VERSION 0.3.34
PUBLIC PREVIEW

The Search module provides a set of tools to add a search experience to your application. With it, you can build an interface for users to perform single-line fuzzy searches for addresses and POIs using the TomTom Search APIs.

Project set up

To use the Search module, you need to configure its credentials and add the module as a dependency:

  1. Get your TomTom API key from the Developer Portal. You can find instructions on how to do that here.
  2. Configure the project as described in the Project setup guide.
  3. Add the Search module dependency in the build.gradle of your application module and synchronize the project.
    1dependencies {
    2 implementation "com.tomtom.sdk:search-client-online:0.3.34"
    3}

After synchronizing the project, initialize the SearchApi object. This object is the entry point to using TomTom’s Search module.

val searchApi = OnlineSearchApi.create(context, "YOUR_API_KEY")

Making search calls

Each search call can be done either synchronously or asynchronously.

Synchronous calls

Synchronous calls are useful when you want to have full control over the threading model used in your application. Note that search calls block execution, so they must be done off the main thread. Otherwise, an exception is thrown.

The response to the request is packed in the Result<SuccessType, FailureType> class. Use the Result.isSuccess() method to check if the call succeeded. If the method returns true, you can get the result of the request using the Result.value() method. If an error occurred, Result.isFailure() returns true instead. More details about the error can be found using Result.failure().

1val query = "TomTom"
2val fuzzySearchOptions = FuzzySearchOptions.Builder(query)
3 .limit(5)
4 .build()
5val result = searchApi.fuzzySearch(fuzzySearchOptions)

Asynchronous

An asynchronous call requires an appropriate callback to be provided as a parameter to the request. See the code for an example of what is required. If the call is successful, the callback’s onSuccess(result: SuccessType) method is triggered with the search result. If an error occurred, it is provided by the callback’s onError(error: FailureType) method.

1val query = "TomTom"
2val fuzzySearchOptions = FuzzySearchOptions.Builder(query)
3 .limit(5)
4 .build()
5searchApi.fuzzySearch(
6 fuzzySearchOptions,
7 object : FuzzySearchCallback {
8 override fun onSuccess(result: FuzzySearchResponse) {
9 /* YOUR CODE GOES HERE */
10 }
11
12 override fun onError(error: FuzzySearchError) {
13 /* YOUR CODE GOES HERE */
14 }
15 }
16)

center