اقتراحات البحث

أداة إنشاء عرض واجهة مستخدم طلبات اقتراحات البحث

للحصول على عرض واجهة مستخدِم اقتراحات البحث، عليك أولاً الحصول على أداة إنشائها من خلال تنفيذ الخطوات التالية:

  1. اجعل فئة Activity المستهدَفة تنفِّذ واجهة GetSearchSuggestionsViewGeneratorCallback أو استخدِم الفئة العميقة غير المحدَّدة الهوية.
  2. إلغاء الطريقتَين onSuccess(SearchSuggestionsViewGenerator) وonError(String) في واجهة GetSearchSuggestionsViewGeneratorCallback
  3. أنشئ مثيلًا لفئة GetSearchSuggestionsViewOptions باستخدام قائمة بسياقات البحث. (اختياري) يأخذ هذا العنصر أيضًا عنصر SearchSuggestionsViewOptions الذي يقدّم بعض الخيارات ل تخصيص مظهر واجهة مستخدم اقتراحات البحث.
  4. يمكنك استدعاء الدالة getSearchSuggestionsView(GetSearchSuggestionsViewOptions, GetSearchSuggestionsViewGeneratorCallback) الخاصة بـ SearchInAppsService.
  5. بعد الحصول على أداة إنشاء واجهة المستخدم، يمكنك التفكير في تخزينها في 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 أيضًا اقتراحات البحث استنادًا إلى الموقع الجغرافي. أنشئ عنصرًا من النوع GetSearchSuggestionsViewOptions يتضمّن قائمة بعناصر LocationContext باتّباع الخطوات التالية: بعد ذلك، نقْل السلسلة GetSearchSuggestionsViewOptions إلى الدالة getSearchSuggestionsView، باتّباع الخطوات الواردة في القسم السابق.

  1. (مطلوب) أنشِئ الكائن LocationContext باستخدام الكائن GeographicalRestrictions. يأخذ عنصر GeographicalRestrictions عنصر CircularArea الذي يحتوي على خطي العرض والطول لمركز المنطقة ونصف قطرها.
  2. (اختياري) أنشئ عنصر TimeRestrictions باستخدام عنصر TimeSegment. يأخذ الكائن TimeSegment سلسلة تمثل الشريحة الزمنية بتنسيق ISO 8601.
  3. (اختياري) أنشِئ الكائن GeoTypeRestrictions باستخدام أنواع المواقع الجغرافية المطلوبة. تُستخدَم الأنواع المقدَّمة لتعديل ترتيب النتائج التي تعرضها الخدمة وفلترتها. القيم المسموح بها هي: "مطعم"، "مقهى"، "بار"، "متنزه"، "حديقة حيوان"، "متحف"، "معلم سياحي"، "مصرف آلي"، "بنك"، "صالون حلاقة"، "وكالة_عقارية"، "موقع_مشاركة_دراجات"، "وكالة_تأجير_سيارات"، "مركز_تسوّق"، "متجر_بقالة" و "فندق".

نموذج التعليمات البرمجية

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)
...

إضافة عرض واجهة مستخدم اقتراحات البحث

بالنسبة إلى getSearchSuggestionsView، يكون الناتج النهائي كائن SearchSuggestionsViewGenerator. يمكنك استخدام دالة populateView(Context) لهذا العنصر لإنشاء عرض واجهة المستخدم و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) },
      )
    }
  }
}

التالي: نتائج البحث على الشبكة الإعلانية