Receber pesquisas sugeridas
Para receber sugestões de pesquisa, chame o método getSearchSuggestions
no
ContextualSearchRuntime
. Esse método usa um único
GetSearchSuggestionsOptions,
em que você pode especificar o texto a ser usado para receber sugestões de pesquisa.
O objeto SearchSuggestions
retornado não contém propriedades
acessíveis. Ele contém todas as informações necessárias para renderizar sugestões
de pesquisa, mas é opaco.
O Swift usa o estilo Swift Concurrency, o Objective-C usa uma função de callback.
Swift
getSearchSuggestions(withOptions:)
Task {
let suggestions = try await searchRuntime.getSearchSuggestions(
withOptions: GetSearchSuggestionsOptions(withTextContext:['Sample Query', 'Another query string']))
}
Objective-C
getSearchSuggestionsWithOptions
[searchRuntime
getSearchSuggestionsWithOptions:
[[GetSearchSuggestionsOptions alloc] initWithTextContext: @[@'Sample Query', @'Another query string' ]]
completionHandler:^(SearchSuggestions *_Nullable suggestions,
NSError *_Nullable error) {
// error will be not null if there is an error.
// On success, suggestions will be non null.
}];
Receber pesquisas sugeridas com base na localização
A solicitação getSearchSuggestions
também oferece suporte a sugestões de pesquisa
com base na localização. Crie um objeto GetSearchSuggestionsOptions
com um LocationSuggestionsContext.
Siga estas etapas para criar a classe LocationSuggestionsContext
:
- (Obrigatório) Defina
latitude
,longitude
eradiusMeters
para corresponder à área solicitada. - (Opcional) Defina
startTime
para corresponder ao horário solicitado em que as sugestões de pesquisa devem ser aplicadas. - (Opcional) Defina a lista
geoTypes
com os tipos de região geográfica solicitados. Os tipos fornecidos são usados para ajustar a classificação e a filtragem dos resultados retornados pelo serviço. Os valores aceitos estão documentados em LocationSuggestionsContext.
Swift
getSearchSuggestions(withOptions:)
Task {
let locationContext: LocationSuggestionsContext = {
let context = LocationSuggestionsContext()
context.latitude = 40.748595
context.longitude = -73.992365
context.radiusMeters = 2500
context.startTime = try?
Date.ISO8601FormatStyle.iso8601.parse("2024-03-14T19:20:30.000+02:00")
context.geoTypes = ["restaurant", "cafe"]
return context
}()
let suggestions = try await searchRuntime.getSearchSuggestions(
withOptions: GetSearchSuggestionsOptions(locationContext: [
locationContext
]))
}
Objective-C
getSearchSuggestionsWithOptions
LocationSuggestionsContext *locationContext;
locationContext = [[LocationSuggestionsContext alloc] init];
locationContext.latitude = 40.748595;
locationContext.longitude = -73.992365;
locationContext.radiusMeters = @2500;
[searchRuntime
getSearchSuggestionsWithOptions:
[[GetSearchSuggestionsOptions alloc] initWithLocationContext: locationContext]
completionHandler:^(SearchSuggestions *_Nullable suggestions,
NSError *_Nullable error) {
// error will be not null if there is an error.
// On success, suggestions will be non null.
}];
Display
Para mostrar os ícones de sugestões de pesquisa, use os métodos abaixo. O estilo varia se você estiver usando o UIKit em vez do SwiftUI.
UIKit do Swift
Mostrar ícones de sugestão de pesquisa usando o SearchSuggestionsViewController.
// Create the SearchSuggestionsViewController with specified display options.
var searchSuggestionsController = SearchSuggestionsViewController(options: viewOptions)
...
// Add the view controller to the existing view hierarchy
searchSuggestionsController.addToViewContoller(self)
view.addSubview(searchSuggestionsController.view)
...
// Assign a specific set of search suggestions to this view controller.
searchSuggestionsController.searchSuggestions = suggestions
Objective-C
Mostre os ícones de sugestão de pesquisa usando o SearchSuggestionsViewController.
// Create the SearchSuggestionsController with specified display options.
SearchSuggestionsViewController *_searchSuggestionsController =
[[SearchSuggestionsViewController alloc] initWithOptions:_customizationOptions];
...
// Add the view controller to the existing view hierarchy
[_searchSuggestionsController addToViewController:self];
[self.view addSubview:_searchSuggestionsController.view];
...
// Assign a specific set of search suggestions to this view controller.
_searchSuggestionsController.searchSuggestions = suggestions
SwiftUI
As sugestões de pesquisa podem ser mostradas com uma SearchSuggestionsView.
SearchSuggestionsView(suggestions: suggestions, options: options)