Thank you. I was able to figure this out. I wanted to be able to make the MapView open to a specific country/state and zoom to the full extent of the selected country/state. In my example below, I am using "Mexico" as the search parameter. This is where you would pass in your parameter. There are somethings to consider when passing in your search string parameter, Georgia for example is a US State and a Country. I've constructed my search strings to be the name of the county/state followed by the 2 letter country code. "Georgia, GE" for the country, "Georgia, US".
import MapKit
import SwiftUI
struct MapView: UIViewRepresentable {
@State private var searchString = String()
class Coordinator: NSObject, MKMapViewDelegate {
var parent: MapView
init(_ parent: MapView) {
self.parent = parent
}
func mapViewDidChangeVisibleRegion(_ mapView: MKMapView) {
print("Center Coordinate: \(mapView.centerCoordinate)")
}
}
func makeCoordinator() -> Coordinator {
Coordinator(self)
}
func makeUIView(context: UIViewRepresentableContext<MapView>) -> MKMapView {
let mapView = MKMapView()
let searchRequest = MKLocalSearch.Request()
searchRequest.naturalLanguageQuery = "Mexico" // This is where you can pass in you search string parameter.
let search = MKLocalSearch(request: searchRequest)
search.start { response, error in
guard let response = response else {
print("Error: \(error?.localizedDescription ?? "Unknown error").")
return
}
mapView.setRegion(response.boundingRegion, animated: true) // .boundingRegion is what opens the map to the extent of the searched country/state.
}
return mapView
}
func updateUIView(_ uiView: MKMapView, context: Context) {
}
}
struct MapView_Previews: PreviewProvider {
static var previews: some View {
MapView()
}
}
Post
Replies
Boosts
Views
Activity
Thank you. I see those two files now. I was asking because the demo in the session was different that the code that is downloaded. Also, thank you for pointing me to the additional example.