Search for a location by name or address
This example does not contain a UI. Results will be printed to the Xcode console.
import UIKit
import MapboxSearch
class SimpleListSearchViewController: MapsViewController {
let searchEngine = SearchEngine()
// let searchEngine = SearchEngine(accessToken: "<#You can pass access token manually#>")
override func viewDidLoad() {
super.viewDidLoad()
searchEngine.delegate = self
}
override func viewDidAppear(_ animated: Bool) {
super.viewDidAppear(animated)
searchEngine.query = "Mapbox" /// You also can call `searchEngine.search(query: "Mapbox")`
}
}
extension SimpleListSearchViewController: SearchEngineDelegate {
func suggestionsUpdated(suggestions: [SearchSuggestion], searchEngine: SearchEngine) {
print("Number of search results: \(searchEngine.suggestions.count)")
/// Simulate user selection with random algorithm
guard let randomSuggestion: SearchSuggestion = searchEngine.suggestions.randomElement() else {
print("No available suggestions to select")
return
}
/// Callback to SearchEngine with chosen `SearchSuggestion`
searchEngine.select(suggestion: randomSuggestion)
/// We may expect `resolvedResult(result:)` to be called next
/// or the new round of `resultsUpdated(searchEngine:)` in case if randomSuggestion represents category suggestion (like a 'bar' or 'cafe')
}
func resultResolved(result: SearchResult, searchEngine: SearchEngine) {
/// WooHoo, we retrieved the resolved `SearchResult`
print("Resolved result: coordinate: \(result.coordinate), address: \(result.address?.formattedAddress(style: .medium) ?? "N/A")")
print("Dumping resolved result:", dump(result))
showAnnotation(result)
}
func searchErrorHappened(searchError: SearchError, searchEngine: SearchEngine) {
print("Error during search: \(searchError)")
}
}
import UIKit
import MapboxMaps
import MapboxSearch
import MapboxSearchUI
class MapsSearchViewController: UIViewController {
var searchController = MapboxSearchController()
var mapView: MapView?
var annotationManager: PointAnnotationManager?
override func viewDidLoad() {
super.viewDidLoad()
// Search setup
searchController.delegate = self
let panelController = MapboxPanelController(rootViewController: searchController)
addChild(panelController)
// Map setup
let mapView = MapView(frame: view.bounds)
mapView.autoresizingMask = [.flexibleWidth, .flexibleHeight]
self.mapView = mapView
view.addSubview(mapView)
annotationManager = mapView.annotations.makePointAnnotationManager()
}
func showResults(_ results: [SearchResult]) {
let annotations = results.map { searchResult -> PointAnnotation in
var annotation = PointAnnotation(coordinate: searchResult.coordinate)
annotation.textField = searchResult.name
annotation.textOffset = [0, -2]
annotation.textColor = ColorRepresentable(color: .red)
annotation.image = .default
return annotation
}
annotationManager?.syncAnnotations(annotations)
if case let .point(point) = annotations.first?.feature.geometry {
let options = CameraOptions(center: point.coordinates)
mapView?.mapboxMap.setCamera(to: options)
}
}
}
extension MapsSearchViewController: SearchControllerDelegate {
func categorySearchResultsReceived(results: [SearchResult]) {
showResults(results)
}
func searchResultSelected(_ searchResult: SearchResult) {
showResults([searchResult])
}
func userFavoriteSelected(_ userFavorite: FavoriteRecord) {
showResults([userFavorite])
}
}
Was this example helpful?