Visualizzare le ricerche suggerite
Per ottenere suggerimenti di ricerca, chiama il metodo getSearchSuggestions su
ContextualSearchRuntime. Questo metodo accetta un singolo
GetSearchSuggestionsOptions
in cui puoi specificare il testo da utilizzare per ottenere i suggerimenti di ricerca.
L'oggetto SearchSuggestions restituito non contiene proprietà accessibili. Contiene tutte le informazioni necessarie per il rendering dei suggerimenti di ricerca, ma per il resto è opaco.
Swift utilizza lo stile Swift Concurrency, Objective-C utilizza una funzione di 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.
}];
Ricevere ricerche suggerite basate sulla posizione
La richiesta getSearchSuggestions supporta anche i suggerimenti di ricerca basati sulla posizione. Crea un oggetto GetSearchSuggestionsOptions con un
LocationSuggestionsContext.
Per creare la classe LocationSuggestionsContext:
- (Obbligatorio) Imposta
latitude,longitudeeradiusMetersin modo che corrispondano all'area richiesta. - (Facoltativo) Imposta
startTimein modo che corrisponda all'ora richiesta in cui devono essere applicati i suggerimenti di ricerca. - (Facoltativo) Imposta l'elenco
geoTypescon i tipi di dati geografici richiesti. I tipi forniti vengono utilizzati per modificare la classificazione e il filtraggio dei risultati restituiti dal servizio. I valori supportati sono documentati in 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
Per visualizzare i chip dei suggerimenti di ricerca, utilizza i seguenti metodi (lo stile varia se utilizzi UIKit anziché SwiftUI).
Swift UIKit
Visualizza i chip dei suggerimenti di ricerca utilizzando 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
Visualizza i chip dei suggerimenti di ricerca utilizzando 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
I suggerimenti di ricerca possono essere visualizzati con un SearchSuggestionsView
SearchSuggestionsView(suggestions: suggestions, options: options)