Search UI SDK and Maps SDK integration
This example uses the Mapbox Maps SDK v10.2.0 and Search SDK v1.0.0-beta.21 for iOS Add the Maps SDK as a dependency using the Maps SDK installation instructions.
import UIKit
import MapboxMaps
import MapboxSearchUI
class SimpleUISearchViewController: MapsViewController {
lazy var searchController: MapboxSearchController = {
let locationProvider = PointLocationProvider(coordinate: .sanFrancisco)
var configuration = Configuration(locationProvider: locationProvider)
return MapboxSearchController(configuration: configuration)
}()
lazy var panelController = MapboxPanelController(rootViewController: searchController)
override func viewDidLoad() {
super.viewDidLoad()
let cameraOptions = CameraOptions(center: .sanFrancisco, zoom: 15)
mapView.camera.fly(to: cameraOptions, duration: 1, completion: nil)
searchController.delegate = self
addChild(panelController)
}
}
extension SimpleUISearchViewController: SearchControllerDelegate {
func categorySearchResultsReceived(category: SearchCategory, results: [SearchResult]) {
showAnnotations(results: results)
}
func searchResultSelected(_ searchResult: SearchResult) {
showAnnotation(searchResult)
}
func userFavoriteSelected(_ userFavorite: FavoriteRecord) {
showAnnotation(userFavorite)
}
}
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])
}
}
このexampleは役に立ちましたか?