پیشنهادات جستجو، پیشنهادات جستجو، پیشنهادات جستجو

درخواست پیشنهادات جستجو برای ایجاد نمای رابط کاربری

برای دریافت نمای UI پیشنهادات جستجو، ابتدا باید مولد آن را با مراحل زیر دریافت کنید:

  1. اجازه دهید کلاس Activity هدف شما رابط GetSearchSuggestionsViewGeneratorCallback را پیاده سازی کند یا از کلاس داخلی ناشناس استفاده کند.
  2. متدهای onSuccess(SearchSuggestionsViewGenerator) و onError(String) واسط GetSearchSuggestionsViewGeneratorCallback را لغو کنید.
  3. نمونه کلاس GetSearchSuggestionsViewOptions را با لیستی از زمینه های جستجو بسازید. (اختیاری) این شیء همچنین یک شی SearchSuggestionsViewOptions می گیرد که گزینه هایی را برای سفارشی کردن ظاهر UI پیشنهادات جستجو ارائه می دهد.
  4. تابع getSearchSuggestionsView(GetSearchSuggestionsViewOptions, GetSearchSuggestionsViewGeneratorCallback) در SearchInAppsService فراخوانی کنید.
  5. بعد از اینکه مولد رابط کاربری را دریافت کردید، می‌توانید آن را در ViewModel ذخیره کنید تا زمانی که فعالیت باید دوباره ایجاد شود (مثل زمانی که پیکربندی در حین اجرای برنامه تغییر کرده است) نیازی به درخواست مجدد ژنراتور نداشته باشید.

کد نمونه

جاوا

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 همچنین از پیشنهادات جستجوی مبتنی بر مکان پشتیبانی می کند. با دنبال کردن این مراحل یک شی GetSearchSuggestionsViewOptions با لیستی از اشیاء LocationContext بسازید. سپس، طبق مراحل بخش قبل، GetSearchSuggestionsViewOptions را به تابع getSearchSuggestionsView منتقل کنید.

  1. (الزامی) شی LocationContext را با شی GeographicalRestrictions بسازید. شی GeographicalRestrictions یک شی CircularArea می گیرد که شامل طول و عرض جغرافیایی مرکز منطقه و شعاع منطقه است.
  2. (اختیاری) شی TimeRestrictions را با شی TimeSegment بسازید. شی TimeSegment یک رشته می گیرد که بخش زمانی را در قالب ISO 8601 نشان می دهد.
  3. (اختیاری) شی GeoTypeRestrictions را با انواع جغرافیایی درخواستی بسازید. انواع ارائه شده برای تنظیم رتبه بندی و فیلتر کردن نتایج بازگردانده شده توسط سرویس استفاده می شود. مقادیر پشتیبانی شده عبارتند از: «رستوران»، «کافه»، «بار»، «پارک»، «باغ‌وحش»، «موزه»، «جاذبه»، «خودپرداز»، «بانک»، «آرایشگاه»، «آژانس_املاک»، « مکان_اشتراک_دوچرخه، "آژانس_اجاره_ماشین"، "مرکز_خرید"، "بقالی_فروشی" و "هتل".

کد نمونه

جاوا

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) این شی برای ایجاد نمای رابط کاربری و updateView(View) برای به‌روزرسانی نمای موجود (مخصوصاً برای برنامه‌های Jetpack Compose) استفاده کنید.

برای برنامه‌های Jetpack Compose، باید از AndroidView برای مصرف نمای کلاسیک تولید شده استفاده کنید.

کد نمونه

جاوا

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) },
      )
    }
  }
}

بعدی: نمایش نتایج جستجو