Place Autocomplete
The Place Autocomplete for iOS is in a private preview. Contact sales team if you are interested in the Place Autocomplete.
The Place Autocomplete returns place suggestions in response to user search queries, such as addresses, businesses and POIs. The Place Autocomplete will help you to implement such functionality in a few lines of code.
Place Autocomplete use cases
- Search by name. This use case allows users quickly search for a place by its name, results can be limited to a region and sorted by proximity.
- Search by coordinates. This use case allows users to search for places by the coordinate and pick appropriate from the returned suggestions.
We have a ready-made sample app that you can try in Xcode with Place Autocomplete and other scenarios. We encourage you to install and try it for yourself.
Learn how to integrate the Place Autocomplete with MapKit and Search UI components in a sample app.
Integration
Before using any of Mapbox's Search products, you need to do some common installation steps. Please follow this guide, then continue with the steps below.
- The main entry-point class is called
PlaceAutocomplete
, you will need to instantiate this class first:
let placeAutocomplete = PlaceAutocomplete(accessToken: mapboxAccessToken)
- Retrieve autocomplete suggestions for the specified query. The function parameters will vary depending on the use case.
- For the Search by name use case you will have to provide a
String
query:
let query = "Starbucks"
placeAutocomplete.suggestions(for: query) { result in ... }
- For the Search by coordinates use case you will have to provide geographic position represented by the
CLLocationCoordinate2D
type:
let searchCoordinates: CLLocationCoordinate2D = prepareSearchCoordinates()
placeAutocomplete.suggestions(for: searchCoordinates) { result in ... }
- Process the Place Autocomplete response. The
suggestions()
function returns either error or suggestions list:
placeAutocomplete.suggestions(for: query) { result in
switch result {
case .success(let suggestions):
self.processSuggestions(suggestions)
case .failure(let error):
debugPrint(error)
}
}
- When a user has selected a suggestion, call
select(suggestion:completion:)
method. This method returns either error orPlaceAutocomplete.Result
struct that provides detailed information about the place. Also,select(suggestion:completion:)
method ends billing session, so it's obligatory to call it every time your users select suggestions.
let selectedSuggestion = pickSuggestion(suggestions)
placeAutocomplete.select(suggestion: selectedSuggestion) { result in
switch result {
case .success(let suggestionResult):
// process result
self.processSelection(suggestionResult)
case .failure(let error):
// process failure
debugPrint(error)
}
}
For more information, see the full API reference for the iOS Search SDK.