Sugerowane wyszukiwania
Aby uzyskać sugestie wyszukiwania, wywołaj metodę getSearchSuggestions na obiekcie ContextualSearchRuntime. Ta metoda przyjmuje pojedynczy obiekt GetSearchSuggestionsOptions, w którym możesz określić tekst, na podstawie którego chcesz uzyskać sugestie wyszukiwania.
Zwrócony obiekt SearchSuggestions nie zawiera żadnych dostępnych właściwości. Zawiera wszystkie informacje niezbędne do renderowania sugestii wyszukiwania, ale poza tym jest nieprzejrzysty.
Swift używa stylu Swift Concurrency, a Objective-C – funkcji wywołania zwrotnego.
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.
}];
Otrzymywanie sugerowanych wyszukiwań na podstawie lokalizacji
Żądanie getSearchSuggestions obsługuje też sugestie wyszukiwania oparte na lokalizacji. Utwórz obiekt GetSearchSuggestionsOptions z LocationSuggestionsContext.
Aby utworzyć klasę LocationSuggestionsContext, wykonaj te czynności:
- (Wymagane) Ustaw zmienne
latitude,longitudeiradiusMeterstak, aby odpowiadały żądanemu obszarowi. - (Opcjonalnie) Ustaw wartość
startTime, aby odpowiadała żądanemu czasowi, w którym mają być stosowane sugestie wyszukiwania. - (Opcjonalnie) Ustaw listę
geoTypesz żądanymi typami geograficznymi. Podane typy są używane do dostosowywania rankingu i filtrowania wyników zwracanych przez usługę. Obsługiwane wartości znajdziesz w dokumentacji 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.
}];
Wyświetlacz
Aby wyświetlić karty z sugestiami wyszukiwania, użyj tych metod (styl różni się w zależności od tego, czy używasz UIKit czy SwiftUI).
Swift UIKit
Wyświetlaj elementy sugestii wyszukiwania za pomocą klasy 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
Wyświetlaj elementy sugestii wyszukiwania za pomocą klasy 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
Sugestie wyszukiwania można wyświetlać za pomocą elementu SearchSuggestionsView.
SearchSuggestionsView(suggestions: suggestions, options: options)