Places SDK for iOS supports the existing Place Autocomplete. If you are familiar with the existing Place Autocomplete, the new version of Place Autocomplete makes the following changes:
Uses a new pricing model. For pricing information for all APIs, see Pricing for the Places SDK for iOS (New).
To make a request, call the new
GMSPlacesClient fetchAutocompleteSuggestionsFromRequest:
method.Pass to the request:
An instance of the new
GMSAutocompleteRequest
class that defines all request parameters, such as the query and session token.A callback of type
GMSAutocompleteSuggestionsCallback
to handle the response.
The
GMSAutocompleteFilter
class now lets you:- Set the region code used to determine formatting of the results.
- Set the prediction offset, a zero-based Unicode character offset of the query.
The response is defined by the new
GMSAutocompleteSuggestion
class. This class contains an array of instances of the new typeGMSAutocompletePlaceSuggestion
representing the suggestions.The session now concludes with a call to either Place Details (New) or Address Validation. For more information, see Autocomplete (New) and session pricing.
Example request
With Place Autocomplete (New), you make a request and
pass all parameters in the GMSAutocompleteRequest
instance:
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.
}
}
}];