Batch 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.ResponseInfo;
import com.mapbox.search.SearchEngine;
import com.mapbox.search.SearchEngineSettings;
import com.mapbox.search.SearchMultipleSelectionCallback;
import com.mapbox.search.SearchOptions;
import com.mapbox.search.SearchSelectionCallback;
import com.mapbox.search.common.AsyncOperationTask;
import com.mapbox.search.result.SearchResult;
import com.mapbox.search.result.SearchSuggestion;
import com.mapbox.search.sample.R;
import java.util.List;
public class ForwardGeocodingBatchResolvingJavaExampleActivity extends AppCompatActivity {
private SearchEngine searchEngine;
private AsyncOperationTask searchRequestTask;
private final SearchSelectionCallback searchCallback = new SearchSelectionCallback() {
@Override
public void onSuggestions(@NonNull List<SearchSuggestion> suggestions, @NonNull ResponseInfo responseInfo) {
if (suggestions.isEmpty()) {
Log.i("SearchApiExample", "No suggestions found");
} else {
Log.i("SearchApiExample", "Search suggestions: " + suggestions);
searchRequestTask = searchEngine.select(suggestions, multipleSelection);
}
}
@Override
public void onResult(@NonNull SearchSuggestion suggestion, @NonNull SearchResult result, @NonNull ResponseInfo info) {
Log.i("SearchApiExample", "Search result: " + result);
}
@Override
public void onResults(@NonNull SearchSuggestion suggestion, @NonNull List<SearchResult> results, @NonNull ResponseInfo responseInfo) {
Log.i("SearchApiExample", "Category search results: " + results);
}
@Override
public void onError(@NonNull Exception e) {
Log.i("SearchApiExample", "Search error: ", e);
}
};
private final SearchMultipleSelectionCallback multipleSelection = new SearchMultipleSelectionCallback() {
@Override
public void onResult(@NonNull List<SearchSuggestion> suggestions, @NonNull List<SearchResult> results, @NonNull ResponseInfo responseInfo) {
Log.i("SearchApiExample", "Batch retrieve 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);
searchEngine = SearchEngine.createSearchEngineWithBuiltInDataProviders(
new SearchEngineSettings(getString(R.string.mapbox_access_token))
);
final SearchOptions options = new SearchOptions.Builder()
.build();
searchRequestTask = searchEngine.search("Paris Eiffel Tower", options, searchCallback);
}
@Override
protected void onDestroy() {
searchRequestTask.cancel();
super.onDestroy();
}
}
package com.mapbox.search.sample.api
import android.app.Activity
import android.os.Bundle
import android.util.Log
import com.mapbox.search.ResponseInfo
import com.mapbox.search.SearchEngine
import com.mapbox.search.SearchEngineSettings
import com.mapbox.search.SearchMultipleSelectionCallback
import com.mapbox.search.SearchOptions
import com.mapbox.search.SearchSelectionCallback
import com.mapbox.search.common.AsyncOperationTask
import com.mapbox.search.result.SearchResult
import com.mapbox.search.result.SearchSuggestion
import com.mapbox.search.sample.R
class ForwardGeocodingBatchResolvingKotlinExampleActivity : Activity() {
private lateinit var searchEngine: SearchEngine
private lateinit var searchRequestTask: AsyncOperationTask
private val searchCallback = object : SearchSelectionCallback, SearchMultipleSelectionCallback {
override fun onSuggestions(suggestions: List<SearchSuggestion>, responseInfo: ResponseInfo) {
if (suggestions.isEmpty()) {
Log.i("SearchApiExample", "No suggestions found")
} else {
Log.i("SearchApiExample", "Search suggestions: $suggestions.")
searchRequestTask = searchEngine.select(suggestions, this)
}
}
override fun onResult(
suggestion: SearchSuggestion,
result: SearchResult,
responseInfo: ResponseInfo
) {
Log.i("SearchApiExample", "Search result: $result")
}
override fun onResults(
suggestion: SearchSuggestion,
results: List<SearchResult>,
responseInfo: ResponseInfo
) {
Log.i("SearchApiExample", "Category search results: $results")
}
override fun onResult(
suggestions: List<SearchSuggestion>,
results: List<SearchResult>,
responseInfo: ResponseInfo
) {
Log.i("SearchApiExample", "Batch retrieve 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(
"Paris Eiffel Tower",
SearchOptions(),
searchCallback
)
}
override fun onDestroy() {
searchRequestTask.cancel()
super.onDestroy()
}
}
Was this example helpful?