खोज सुझाव

खोज के सुझावों के यूज़र इंटरफ़ेस (यूआई) व्यू जनरेटर का अनुरोध करना

खोज के सुझावों का यूज़र इंटरफ़ेस (यूआई) व्यू पाने के लिए, आपको पहले इसका जनरेटर पाना होगा. इसके लिए, यह तरीका अपनाएं:

  1. अपनी टारगेट Activity क्लास को GetSearchSuggestionsViewGeneratorCallback इंटरफ़ेस लागू करने दें या अनाम इंटरनल क्लास का इस्तेमाल करें.
  2. GetSearchSuggestionsViewGeneratorCallback इंटरफ़ेस के onSuccess(SearchSuggestionsViewGenerator) और onError(String) मेथड को बदलें.
  3. खोज के कॉन्टेक्स्ट की सूची की मदद से, GetSearchSuggestionsViewOptions क्लास इंस्टेंस बनाएं. (ज़रूरी नहीं) यह ऑब्जेक्ट एक SearchSuggestionsViewOptions ऑब्जेक्ट भी लेता है. इससे, खोज के सुझावों वाले यूज़र इंटरफ़ेस (यूआई) के दिखने के तरीके को पसंद के मुताबिक बनाने के कुछ विकल्प मिलते हैं.
  4. SearchInAppsService के getSearchSuggestionsView(GetSearchSuggestionsViewOptions, GetSearchSuggestionsViewGeneratorCallback) फ़ंक्शन को कॉल करें.
  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 एंट्री पॉइंट, जगह के हिसाब से खोज के सुझावों के साथ भी काम करता है. यह तरीका अपनाकर, LocationContext ऑब्जेक्ट की सूची के साथ GetSearchSuggestionsViewOptions ऑब्जेक्ट बनाएं. इसके बाद, पिछले सेक्शन में दिए गए निर्देशों का पालन करके, GetSearchSuggestionsViewOptions को getSearchSuggestionsView फ़ंक्शन में पास करें.

  1. (ज़रूरी है) GeographicalRestrictions ऑब्जेक्ट की मदद से LocationContext ऑब्जेक्ट बनाएं. GeographicalRestrictions ऑब्जेक्ट, CircularArea ऑब्जेक्ट को पैरामीटर के तौर पर लेता है. इसमें इलाके के केंद्र का अक्षांश और देशांतर, साथ ही इलाके का दायरा शामिल होता है.
  2. (ज़रूरी नहीं) TimeSegment ऑब्जेक्ट की मदद से TimeRestrictions ऑब्जेक्ट बनाएं. TimeSegment ऑब्जेक्ट में एक स्ट्रिंग होती है, जो ISO 8601 फ़ॉर्मैट में समय के सेगमेंट को दिखाती है.
  3. (ज़रूरी नहीं) अनुरोध किए गए भौगोलिक टाइप के साथ 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)
...

खोज के सुझावों का यूज़र इंटरफ़ेस (यूआई) व्यू जोड़ना

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

अगला: खोज के नतीजे दिखाएं