简体   繁体   English

启用在 kotlin 中使用 tomtom 搜索 api

[英]enable to use tomtom search api in kotlin

Been searching for a week now, The official documentation are not clear at all.找了一个星期了,官方 文档一点都不清楚。

as mentioned there, the code如那里所述,代码

val searchServiceConnection = SearchServiceManager.createAndBind(context,
    searchServiceConnectionCallback)

should initialize the search API in the application.应该在应用程序中初始化搜索 API。 but it is not clear how to use it after this.但在此之后如何使用它还不清楚。

I installed and initialized the API in the proper way:我以正确的方式安装并初始化了 API:

Gradle: Gradle:

//library required for search
implementation("com.tomtom.online:sdk-search:2.4264")



android {
    compileOptions {
        sourceCompatibility JavaVersion.VERSION_1_8
        targetCompatibility JavaVersion.VERSION_1_8
    }
}

AndroidManifest AndroidManifest

<meta-data
            android:name="OnlineSearch.Key"
            android:value="your-tomtom-key" />

I assume that you have a proper API key inside the AndroidManifest.xml file.我假设您在 AndroidManifest.xml 文件中有一个正确的 API 密钥。 Given above you can start playing with TomTom Search API in three steps:如上所述,您可以通过三个步骤开始使用 TomTom Search API:

  1. Create the SearchAPI object:创建 SearchAPI object:

     val searchApi = OnlineSearchApi.create(applicationContext)!!
  2. Create the Search query object:创建搜索查询 object:

     val text = "Berlin" val searchQuery = FuzzySearchQueryBuilder.create(text).build()
  3. Call Search API and catch the results inside a listener:调用 Search API 并在侦听器中捕获结果:

     searchApi.search(searchQuery, object: FuzzySearchResultListener { override fun onSearchResult(response: FuzzySearchResponse?) { Toast.makeText(applicationContext, "results", Toast.LENGTH_SHORT).show() } override fun onSearchError(error: SearchError?) { Toast.makeText(applicationContext, "error", Toast.LENGTH_SHORT).show() } })

Based on the given answer, that is now deprecated, here is the new equivalent:根据给定的答案,现在已弃用,这是新的等价物:

    private val searchApi = OnlineSearchApi.create(application, TOMTOM_API_KEY)

    val term = "Berlin"
    
    searchApi.search(FuzzySearchSpecification.Builder(term).build(), object : FuzzyOutcomeCallback {
                        override fun onError(error: SearchException) {
                            Log.e(TAG, "onError: ", error)
                        }
            
                        override fun onSuccess(fuzzyOutcome: FuzzyOutcome) {
                            for (fuzzyDetails in fuzzyOutcome.fuzzyDetailsList)
                                fuzzyDetails.apply { // process results (here we just print them)                               
                                  Log.d(TAG, "onSuccess: fuzzyDetails = $fuzzyDetails")
                                }
                              }
                            }) 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM