Skip to main content

Search by Text

Search by Text

The Search SDK simplifies the process of implementing text-based fuzzy search for addresses, places, and Points of Interest (POIs).

Using PlaceAutocomplete Class

The PlaceAutocomplete class is instrumental in implementing this feature, allowing you to do so with minimal lines of code. This use case enables users to swiftly search for a location by its name. The results can be confined to a specific region and sorted based on proximity.


We provide a pre-built sample application that you can experiment with in Xcode. This application includes the text search feature among other scenarios. We encourage you to download and explore it firsthand.

example
Full example of Text Search

This guide will teach you how to integrate the Place Autocomplete feature with MapKit and Search UI components using a sample application.

chevron-right
guide
Installation guide

Before utilizing any of Mapbox's Search products, there are some common installation steps that you need to follow. Refer to this guide, and then go ahead with the steps outlined below.

chevron-right
  1. The PlaceAutocomplete class serves as the primary access point for implementing text-based search functionality. To begin, you must first create an instance of this class. You can do this by using the following Swift code:
let placeAutocomplete = PlaceAutocomplete(accessToken: mapboxAccessToken)
  1. To fetch autocomplete suggestions based on a specific query, you'll need to use different function parameters depending on your use case. Here's how you can do it:
  • 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 ... }
  1. After initiating a request for autocomplete suggestions, you'll need to handle the response from the suggestions() method. This function will either return an error or a list of suggestions. Here's how you can process the response:
placeAutocomplete.suggestions(for: query) { result in
switch result {
case .success(let suggestions):
self.processSuggestions(suggestions)

case .failure(let error):
debugPrint(error)
}
}
  1. Once a user has chosen a suggestion, you should call the select(suggestion:completion:) method. This method will return either an error or a PlaceAutocomplete.Result struct, which contains detailed information about the selected place. It's important to note that the select(suggestion:completion:) method also concludes the billing session, so it's mandatory to call this method each time a user selects a suggestion. Here's how you can implement it:
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)
}
}
glossary
API Reference

For more information, see the full API reference for the iOS Search SDK.

chevron-right
Was this page helpful?