Category search
This example does not contain a UI. Results will be logged in Android Studio.
Note
This example is a part of the Search SDK for Android sample app. You can find the values for all referenced resources in the res
directory. For example, see res/values/strings.xml
for R.string.*
references used in this example. The dependencies can be found here.The examples use View binding.See setup documention if necessary.
package com.mapbox.search.sample.api;
import android.os.Bundle;
import android.util.Log;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;
import com.mapbox.search.CategorySearchOptions;
import com.mapbox.search.ResponseInfo;
import com.mapbox.search.SearchCallback;
import com.mapbox.search.SearchEngine;
import com.mapbox.search.SearchEngineSettings;
import com.mapbox.search.common.AsyncOperationTask;
import com.mapbox.search.result.SearchResult;
import com.mapbox.search.sample.BuildConfig;
import com.mapbox.search.sample.R;
import java.util.List;
public class CategorySearchJavaExampleActivity extends AppCompatActivity {
private AsyncOperationTask searchRequestTask;
private final SearchCallback searchCallback = new SearchCallback() {
@Override
public void onResults(@NonNull List<SearchResult> results, @NonNull ResponseInfo responseInfo) {
if (results.isEmpty()) {
Log.i("SearchApiExample", "No category search results");
} else {
Log.i("SearchApiExample", "Category search results: " + results);
}
}
@Override
public void onError(@NonNull Exception e) {
Log.i("SearchApiExample", "Search error", e);
}
};
@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
final SearchEngine searchEngine = SearchEngine.createSearchEngineWithBuiltInDataProviders(
new SearchEngineSettings(getString(R.string.mapbox_access_token))
);
final CategorySearchOptions options = new CategorySearchOptions.Builder()
.limit(1)
.build();
searchRequestTask = searchEngine.search("cafe", options, searchCallback);
}
@Override
public void onDestroy() {
searchRequestTask.cancel();
super.onDestroy();
}
}
package com.mapbox.search.sample.api
import android.os.Bundle
import android.util.Log
import androidx.appcompat.app.AppCompatActivity
import com.mapbox.search.CategorySearchOptions
import com.mapbox.search.ResponseInfo
import com.mapbox.search.SearchCallback
import com.mapbox.search.SearchEngine
import com.mapbox.search.SearchEngineSettings
import com.mapbox.search.common.AsyncOperationTask
import com.mapbox.search.result.SearchResult
import com.mapbox.search.sample.R
class CategorySearchKotlinExampleActivity : AppCompatActivity() {
private lateinit var searchEngine: SearchEngine
private lateinit var searchRequestTask: AsyncOperationTask
private val searchCallback: SearchCallback = object : SearchCallback {
override fun onResults(results: List<SearchResult>, responseInfo: ResponseInfo) {
if (results.isEmpty()) {
Log.i("SearchApiExample", "No category search results")
} else {
Log.i("SearchApiExample", "Category search results: $results")
}
}
override fun onError(e: Exception) {
Log.i("SearchApiExample", "Search error", e)
}
}
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
searchEngine = SearchEngine.createSearchEngineWithBuiltInDataProviders(
SearchEngineSettings(getString(R.string.mapbox_access_token))
)
searchRequestTask = searchEngine.search(
"cafe",
CategorySearchOptions(limit = 1),
searchCallback
)
}
override fun onDestroy() {
searchRequestTask.cancel()
super.onDestroy()
}
}
この{Type}は役に立ちましたか?