Recently I tried to integrate Mapbox default Search UI into my project. My project basically is based on SwiftUI, and the usage example on the Mapbox website is using UIKit. I was attempted to wrapping UIKit into the SwiftUI code. Here is what I wrote on the SearchController which conformed to protocol 'UIViewControllerRepresentable':
struct Search: UIViewControllerRepresentable, LocationProvider {
let userCoordinate = CLLocationCoordinate2D(latitude: 37.785834, longitude: -122.406417)
/// `LocationProvider` protocol implementation
func currentLocation() -> CLLocationCoordinate2D? { userCoordinate }
func makeUIViewController(context: UIViewControllerRepresentableContext<Search>) -> MapboxPanelController {
let searchController = MapboxSearchController(locationProvider: self)
// default panel
let panelController = MapboxPanelController(rootViewController: searchController)
searchController.delegate = context.coordinator
return panelController
}
func updateUIViewController(_ uiViewController: MapboxPanelController, context: UIViewControllerRepresentableContext<Search>) {
}
...
}
And the parent content View I wrote:
...
var body: some View {
VStack{
ZStack {
NavigationMap()
Search(annotations: $annotations, position: $position)
}
}
}
...
The NavigationMap() is the MapView I create on another file. Basically what I tried to do here is to add a default panelController by Mapbox as the child view of the NavigationMap. But the result I ran is I am not able to interact with the NavigationMap anymore. It seems like the searchController completely covers the NavigationMap even with the panelController which allows you to drag the panel up and down. I can't figure out what I did wrong. Here is the original document provided by Mapbox:
class SimpleUISearchViewController: UIViewController, LocationProvider {
lazy var searchController = MapboxSearchController(locationProvider: self)
/// `LocationProvider` protocol implementation
func currentLocation() -> CLLocationCoordinate2D? { mapboxSFOfficeCoordinate }
lazy var panelController = MapboxPanelController(rootViewController: searchController)
let mapView = MGLMapView()
let mapboxSFOfficeCoordinate = CLLocationCoordinate2D(latitude: 37.7911551, longitude: -122.3966103)
override func viewDidLoad() {
super.viewDidLoad()
/// Add MGLMapView on the screen
mapView.frame = view.bounds
mapView.autoresizingMask = [.flexibleWidth, .flexibleHeight]
mapView.showsUserLocation = true
mapView.setCenter(mapboxSFOfficeCoordinate, zoomLevel: 15, animated: false)
view.addSubview(mapView)
searchController.delegate = self
addChild(panelController)
}
...
}
I am not sure why I implemented the wrong way in SwiftUI or the default document it provided cannot completely support SwiftUI. May anyone help me with that? I appreciate it.