要求搜尋建議 UI 檢視產生器
如要取得搜尋建議 UI 檢視畫面,請先按照下列步驟取得產生器:
- 讓目標
Activity
類別實作GetSearchSuggestionsViewGeneratorCallback
介面,或使用匿名內部類別。 - 覆寫
GetSearchSuggestionsViewGeneratorCallback
介面的onSuccess(SearchSuggestionsViewGenerator)
和onError(String)
方法。 - 使用搜尋內容清單建構
GetSearchSuggestionsViewOptions
類別例項。(選用) 這個物件也會採用SearchSuggestionsViewOptions
物件,提供一些選項,可自訂搜尋建議 UI 的外觀。 - 呼叫
SearchInAppsService
的getSearchSuggestionsView(GetSearchSuggestionsViewOptions, GetSearchSuggestionsViewGeneratorCallback)
函式。 - 取得 UI 產生器後,您可以考慮將其儲存在 ViewModel 中,這樣就能在需要重新建立活動時 (例如在應用程式執行期間變更設定時) 再次要求產生器。
程式碼範例
Java
package ...;
...
import androidx.appcompat.app.AppCompatActivity;
import com.google.android.libraries.searchinapps.GetSearchSuggestionsViewGeneratorCallback;
import com.google.android.libraries.searchinapps.GetSearchSuggestionsViewOptions;
import com.google.android.libraries.searchinapps.SearchInAppsService;
import com.google.android.libraries.searchinapps.SearchSuggestionsViewGenerator;
import java.util.Arrays;
import java.util.List;
...
public class MainActivity extends AppCompatActivity implements GetSearchSuggestionsViewGeneratorCallback {
private SearchInAppsService service;
@Override
public void onSuccess(SearchSuggestionsViewGenerator generator) {
...
}
@Override
public void onError(String errorMessage) {
...
}
@Override
protected void onCreate(Bundle savedInstanceState) {
...
service = SearchInAppsService.create(this);
// Right now only the first element of this list will be used.
List<String> searchContext = Arrays.asList(new String[]{"This is a test query, for example."});
// Uses the default SearchSuggestionsViewOptions.
service.getSearchSuggestionsView(
new GetSearchSuggestionsViewOptions().setTextContext(searchContext),this);
...
}
@Override
public void onDestroy() {
service.shutDown();
super.onDestroy();
}
}
Jetpack Compose
package ...
...
import android.os.Bundle
import androidx.activity.compose.setContent
import androidx.appcompat.app.AppCompatActivity
import androidx.compose.runtime.Composable
import androidx.compose.runtime.DisposableEffect
import androidx.compose.runtime.mutableStateOf
import androidx.compose.ui.platform.LocalContext
import com.google.android.libraries.searchinapps.GetSearchSuggestionsViewGeneratorCallback
import com.google.android.libraries.searchinapps.GetSearchSuggestionsViewOptions
import com.google.android.libraries.searchinapps.SearchInAppsService
import com.google.android.libraries.searchinapps.SearchSuggestionsViewGenerator
...
class MainActivity : AppCompatActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContent {
SearchSuggestionsUI()
}
}
@Composable
fun SearchSuggestionsUI() {
...
var service by remember {
mutableStateOf<SearchInAppsService?>(
SearchInAppsService.create(LocalContext.current))
}
DisposableEffect(Unit) { onDispose { service?.shutDown() } }
val callback =
object : GetSearchSuggestionsViewGeneratorCallback {
override fun onSuccess(generator: SearchSuggestionsViewGenerator) {
...
}
override fun onError(errorMessage: String) {
...
}
}
var searchContexts: List<String> = listOf<String>("Query")
// Uses the default SearchSuggestionsViewOptions.
var options: GetSearchSuggestionsViewOptions =
GetSearchSuggestionsViewOptions()
.setTextContext(searchContexts)
service?.getSearchSuggestionsView(options, callback)
...
}
}
要求依位置提供搜尋建議
getSearchSuggestionsView
進入點也支援以位置為依據的搜尋建議。請按照下列步驟,使用 LocationContext
物件清單建構 GetSearchSuggestionsViewOptions
物件。接著,按照上一節的步驟,將 GetSearchSuggestionsViewOptions
傳遞至 getSearchSuggestionsView
函式。
- (必要) 使用
GeographicalRestrictions
物件建構LocationContext
物件。GeographicalRestrictions
物件會採用CircularArea
物件,其中包含區域中心的經緯度和半徑。 - (選用) 使用
TimeSegment
物件建構TimeRestrictions
物件。TimeSegment
物件採用代表 ISO 8601 格式時間區隔的字串。 - (選用) 使用要求的地理類型建立
GeoTypeRestrictions
物件。提供的類型可用於調整服務傳回結果的排名和篩選方式。支援的值包括:"restaurant"、"cafe"、"bar"、"park"、"zoo"、"museum"、"attraction"、"atm"、"bank"、"hair_salon"、"real_estate_agency"、"bicycle_sharing_location"、"car_rental_agency"、"shopping_center"、"grocery_store" 和 "hotel"。
程式碼範例
Java
package ...;
...
import com.google.android.libraries.searchinapps.GetSearchSuggestionsViewGeneratorCallback;
import com.google.android.libraries.searchinapps.GetSearchSuggestionsViewOptions;
import com.google.android.libraries.searchinapps.LocationContext;
import com.google.android.libraries.searchinapps.LocationContext.CircularArea;
import com.google.android.libraries.searchinapps.LocationContext.GeoTypeRestrictions;
import com.google.android.libraries.searchinapps.LocationContext.GeographicalRestrictions;
import com.google.android.libraries.searchinapps.LocationContext.LatLng;
import com.google.android.libraries.searchinapps.LocationContext.TimeRestrictions;
import com.google.android.libraries.searchinapps.LocationContext.TimeSegment;
import com.google.android.libraries.searchinapps.SearchInAppsService;
import com.google.android.libraries.searchinapps.SearchSuggestionsViewGenerator;
...
LocationContext locationContext = new LocationContext(
new GeographicalRestrictions(
new CircularArea(
new LatLng(
40.7414728,
-74.0059622),
1000)))
// Optional. If set, the returned suggestions are categories of which
// there are several businesses in the requested area that are open at
// the given time.
.setTimeRestrictions(
new TimeRestrictions(
new TimeSegment("2024-05-18T19:20:30.45+01:00")))
// Optional. If set, the returned suggestions are subset of the
// requested categories.
.setGeoTypeRestrictions(
new GeoTypeRestrictions("restaurant", "parks"));
// Uses the default SearchSuggestionsViewOptions.
service = SearchInAppsService.create(this);
// Uses the default SearchSuggestionsViewOptions.
service.getSearchSuggestionsView(
new GetSearchSuggestionsViewOptions().setLocationContext(Arrays.asList(locationContext)), this);
Jetpack Compose
package ...
...
import com.google.android.libraries.searchinapps.GetSearchSuggestionsViewGeneratorCallback
import com.google.android.libraries.searchinapps.GetSearchSuggestionsViewOptions
import com.google.android.libraries.searchinapps.LocationContext
import com.google.android.libraries.searchinapps.LocationContext.CircularArea
import com.google.android.libraries.searchinapps.LocationContext.GeoTypeRestrictions
import com.google.android.libraries.searchinapps.LocationContext.GeographicalRestrictions
import com.google.android.libraries.searchinapps.LocationContext.LatLng
import com.google.android.libraries.searchinapps.LocationContext.TimeRestrictions
import com.google.android.libraries.searchinapps.LocationContext.TimeSegment
import com.google.android.libraries.searchinapps.SearchInAppsService
import com.google.android.libraries.searchinapps.SearchSuggestionsViewGenerator
...
var locationContext: LocationContext = LocationContext(
GeographicalRestrictions(
CircularArea(
LatLng(40.7414728, -74.0059622),
1000)))
// Optional. If set, there are several businesses in the requested area that
// are open at the given time.
.setTimeRestrictions(
TimeRestrictions(
TimeSegment("2024-05-18T19:20:30.45+01:00")))
// Optional. If set, the returned suggestions are subset of the requested
// categories.
.setGeoTypeRestrictions(
GeoTypeRestrictions("restaurant", "parks"))
// Uses the default SearchSuggestionsViewOptions.
var options: GetSearchSuggestionsViewOptions =
GetSearchSuggestionsViewOptions()
.setLocationContext(listOf<LocationContext>(locationContext))
service.getSearchSuggestionsView(options, callback)
...
新增搜尋建議 UI 檢視畫面
getSearchSuggestionsView
的最終輸出內容為 SearchSuggestionsViewGenerator
物件。您可以使用這個物件的 populateView(Context)
函式產生 UI 檢視畫面,並使用 updateView(View)
更新現有檢視畫面 (特別適用於 Jetpack Compose 應用程式)。
針對 Jetpack Compose 應用程式,您應使用 AndroidView 取用產生的經典檢視畫面。
程式碼範例
Java
package ...;
...
import androidx.appcompat.app.AppCompatActivity;
import com.google.android.libraries.searchinapps.GetSearchSuggestionsViewGeneratorCallback;
import com.google.android.libraries.searchinapps.GetSearchSuggestionsViewOptions;
import com.google.android.libraries.searchinapps.SearchInAppsService;
import com.google.android.libraries.searchinapps.SearchSuggestionsViewGenerator;
import java.util.Arrays;
import java.util.List;
...
public class MainActivity extends AppCompatActivity implements GetSearchSuggestionsViewGeneratorCallback {
private SearchInAppsService service;
@Override
public void onSuccess(SearchSuggestionsViewGenerator generator) {
ViewGroup container = findViewById(R.id.[container_id]);
container.removeAllViews();
container.addView(generator.populateView(this));
}
@Override
public void onError(String errorMessage) {
...
}
@Override
protected void onCreate(Bundle savedInstanceState) {
...
service = SearchInAppsService.create(this);
List<String> searchContext = Arrays.asList(new String[]{"Query"});
// Uses the default SearchSuggestionsViewOptions.
service.getSearchSuggestionsView(
new GetSearchSuggestionsViewOptions().setTextContext(searchContext),this);
...
}
@Override
public void onDestroy() {
service.shutDown();
super.onDestroy();
}
}
Jetpack Compose
package ...
...
import android.os.Bundle
import androidx.activity.compose.setContent
import androidx.appcompat.app.AppCompatActivity
import androidx.compose.runtime.Composable
import androidx.compose.runtime.DisposableEffect
import androidx.compose.runtime.MutableState
import androidx.compose.runtime.mutableStateOf
import androidx.compose.ui.platform.LocalContext
import androidx.compose.ui.viewinterop.AndroidView
import com.google.android.libraries.searchinapps.GetSearchSuggestionsViewGeneratorCallback
import com.google.android.libraries.searchinapps.GetSearchSuggestionsViewOptions
import com.google.android.libraries.searchinapps.SearchInAppsService
import com.google.android.libraries.searchinapps.SearchSuggestionsViewGenerator
...
class MainActivity : AppCompatActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContent { SearchSuggestionsUI() }
}
@Composable
fun SearchSuggestionsUI() {
...
var service by remember {
mutableStateOf<SearchInAppsService?>(
SearchInAppsService.create(LocalContext.current))
}
var viewGenerator = mutableStateOf<SearchSuggestionsViewGenerator?>(null)
DisposableEffect(Unit) { onDispose { service?.shutDown() } }
val callback =
object : GetSearchSuggestionsViewGeneratorCallback {
override fun onSuccess(generator: SearchSuggestionsViewGenerator) {
viewGenerator.value = generator
}
override fun onError(errorMessage: String) {}
}
var searchContexts: List<String> = listOf<String>("Query")
// Uses the default SearchSuggestionsViewOptions.
var options: GetSearchSuggestionsViewOptions =
GetSearchSuggestionsViewOptions().setTextContext(searchContexts)
service?.getSearchSuggestionsView(options, callback)
ChipGroupUI(viewGenerator)
...
}
@Composable
fun ChipGroupUI(viewGenerator: MutableState<SearchSuggestionsViewGenerator?>) {
viewGenerator.value?.let { viewGenerator ->
var context = LocalContext.current
AndroidView(
factory = { context -> viewGenerator.populateView(context) },
update = { view -> viewGenerator.updateView(view, context) },
)
}
}
}