Places SDK for iOS 支援現有的 Place Autocomplete。如果您熟悉現有的 Place Autocomplete,新版 Place Autocomplete 會進行以下變更:
使用新的定價模式。如需所有 API 的定價資訊,請參閱「Places SDK for iOS (新版) 的定價」。
如要提出要求,請呼叫新的
GMSPlacesClient fetchAutocompleteSuggestionsFromRequest:
方法。傳遞至要求:
新
GMSAutocompleteRequest
類別的例項,可定義所有要求參數,例如查詢和工作階段權杖。用來處理回應的
GMSAutocompleteSuggestionsCallback
類型回呼。
GMSAutocompleteFilter
類別現在可讓您:- 設定用來決定結果格式的區域代碼。
- 設定預測偏移量,也就是查詢的 Unicode 字元偏移量,以 0 為起點。
回應是由新的
GMSAutocompleteSuggestion
類別定義。這個類別包含代表建議的新類型GMSAutocompletePlaceSuggestion
例項陣列。這個工作階段現在會結束,並呼叫 Place Details (新版) 或 Address Validation。詳情請參閱「自動完成功能 (新版) 和會話定價」。
要求範例
使用 Place Autocomplete (新版) 時,您可以提出要求並在 GMSAutocompleteRequest
例項中傳遞所有參數:
Swift
let token = GMSAutocompleteSessionToken()
let northEastBounds = CLLocationCoordinate2DMake(37.38816277477739, -122.08813770258874)
let southWestBounds = CLLocationCoordinate2DMake(37.39580487866437, -122.07702325966572)
let filter = GMSAutocompleteFilter()
filter.types = [kGMSPlaceTypeRestaurant]
filter.locationBias = GMSPlaceRectangularLocationOption(northEastBounds, southWestBounds)
let request = GMSAutocompleteRequest(query:"Sicilian piz")
request.filter = filter
request.sessionToken = token
GMSPlacesClient.shared().fetchAutocompleteSuggestions(from: request, callback: { results, error in
// Handle response
})
Objective-C
CLLocationCoordinate2D northEast = CLLocationCoordinate2DMake(37.38816277477739, -122.08813770258874);
CLLocationCoordinate2D southWest = CLLocationCoordinate2DMake(37.39580487866437, -122.07702325966572);
GMSAutocompleteFilter *filter = [[GMSAutocompleteFilter alloc] init];
filter.types = @[ kGMSPlaceTypeRestaurant ];
filter.locationBias = GMSPlaceRectangularLocationOption(northEast, southWest);
GMSAutocompleteRequest *request = [[GMSAutocompleteRequest alloc] initWithQuery:@"Sicilian piz"];
request.sessionToken = token;
request.filter = filter;
[[GMSPlacesClient sharedClient] fetchAutocompleteSuggestionsFromRequest:request callback:^(NSArray<GMSAutocompleteSuggestion *> * results, NSError * error){
// Handle response
for (GMSAutocompleteSuggestion *suggestion in results) {
if (suggestion.placeSuggestion) {
// Show place suggestion data.
}
}
}];