MapType For Maps in SwiftUI

DevTechie
DevTechie
Published in
3 min readApr 8, 2022

--

Photo by NASA on Unsplash

SwiftUI Map view is powerful, but if you need to do more than display a map or annotation then you will have to look into UIKit based maps.

SwiftUI makes is super easy to port over maps from UIKit to SwiftUI world with the help of UIViewRepresentable.

In this article, we will build a map using UIKit and UIViewRepresentable to add support for MapType.

Let’s get started.

We will start by creating new struct for UIViewRepresentable, let’s call this UIKitMapView.

struct UIKitMapView: UIViewRepresentable {

}

This struct will expect two properties, a region for map center and MapType:

struct UIKitMapView: UIViewRepresentable {
let region: MKCoordinateRegion
let mapType: MKMapType

}

UIViewRepresentable protocol needs conformance of two functions as shown below:

struct UIKitMapView: UIViewRepresentable {
let region: MKCoordinateRegion
let mapType: MKMapType

func makeUIView(context: Context) -> MKMapView {

}

func updateUIView(_ mapView: MKMapView, context: Context) {

}

}

MakeUIView, creates the instance for the type and UpdateUIView is responsible to respond to the changes to the view.

--

--