Places SDK for iOS 支持现有的 地点自动补全。 如果您熟悉 地点自动补全 - 新版 地点自动补全 进行以下更改:
使用新的价格模式。如需了解所有 API 的价格信息,请参阅 Places SDK for iOS(新)的价格。
要发出请求,请调用新的
GMSPlacesClient fetchAutocompleteSuggestionsFromRequest:
方法。传递给请求:
新的
GMSAutocompleteRequest
类,用于定义所有请求参数,例如查询和 会话令牌类型的回调
GMSAutocompleteSuggestionsCallback
处理响应。
通过
GMSAutocompleteFilter
类现在可让您:- 设置用于确定结果格式的区域代码。
- 设置预测偏移量,即 查询。
响应由新的
GMSAutocompleteSuggestion
类。此类包含新类型的实例的数组GMSAutocompletePlaceSuggestion
表示建议的参数。该会话现在会调用 地点详情(新)或 地址验证。对于 请参阅 自动补全(新)和会话定价。
示例请求
利用“地点自动补全(新)”功能,您只需发出请求并
传递 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.
}
}
}];