0

找了一个星期了,官方 文档一点都不清楚。

如那里所述,代码

val searchServiceConnection = SearchServiceManager.createAndBind(context,
    searchServiceConnectionCallback)

应该在应用程序中初始化搜索 API。但在此之后如何使用它还不清楚。

我以正确的方式安装并初始化了 API:

摇篮:

//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

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

2 回答 2

2

我假设您在 AndroidManifest.xml 文件中有正确的 API 密钥。如上所述,您可以通过三个步骤开始使用 TomTom 搜索 API:

  1. 创建 SearchAPI 对象:

    val searchApi = OnlineSearchApi.create(applicationContext)!!
    
  2. 创建搜索查询对象:

    val text = "Berlin"
    val searchQuery = FuzzySearchQueryBuilder.create(text).build()
    
  3. 调用 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()
        }
    })
    
于 2019-10-08T14:20:19.960 回答
0

根据给定的答案,现在已弃用,这是新的等价物:

    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")
                                }
                              }
                            }) 
于 2021-05-01T09:41:59.923 回答