Search SDK for Android
Beta

Search

This example does not contain a UI. Results will be logged in Android Studio.

MainActivity.java
package com.mapbox.search.demo.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.MapboxSearchSdk;
import com.mapbox.search.SearchEngine;
import com.mapbox.search.SearchOptions;
import com.mapbox.search.SearchRequestTask;
import com.mapbox.search.SearchSelectionCallback;
import com.mapbox.search.result.SearchResult;
import com.mapbox.search.result.SearchSuggestion;
import java.util.List;
public class ForwardGeocodingJavaExampleActivity extends AppCompatActivity {
private SearchEngine searchEngine;
private SearchRequestTask searchRequestTask;
private final SearchSelectionCallback searchCallback = new SearchSelectionCallback() {
@Override
public void onSuggestions(@NonNull List<? extends SearchSuggestion> suggestions) {
if (suggestions.isEmpty()) {
Log.i("SearchApiExample", "No suggestions found");
} else {
Log.i("SearchApiExample", "Search suggestions: " + suggestions + "\nSelecting first...");
searchRequestTask = searchEngine.select(suggestions.get(0), this);
}
}
@Override
public void onResult(@NonNull SearchResult result) {
Log.i("SearchApiExample", "Search result: " + result);
}
@Override
public void onError(@NonNull Exception e) {
Log.i("SearchApiExample", "Search error: ", e);
}
};
@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
searchEngine = MapboxSearchSdk.createSearchEngine();
final SearchOptions options = new SearchOptions.Builder()
.limit(5)
.build();
searchRequestTask = searchEngine.search("Paris Eiffel Tower", options, searchCallback);
}
@Override
protected void onDestroy() {
searchRequestTask.cancel();
super.onDestroy();
}
}
Was this example helpful?