Places SDK for iOS 支持现有的地点自动补全。如果您熟悉现有的地点自动补全功能,请注意新版地点自动补全做出了以下更改:
采用了新的定价模式。如需了解所有 API 的价格信息,请参阅 Places SDK for iOS(新版)的价格。
如需发出请求,请调用新的
GMSPlacesClient fetchAutocompleteSuggestionsFromRequest:
方法。传递给请求:
新
GMSAutocompleteRequest
类的实例,用于定义所有请求参数,例如查询和会话令牌。用于处理响应的
GMSAutocompleteSuggestionsCallback
类型的回调。
现在,您可以借助
GMSAutocompleteFilter
类执行以下操作:- 设置用于确定结果格式的地区代码。
- 设置预测偏移量,即查询的 Unicode 字符偏移量(从零开始)。
响应由新的
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.
}
}
}];