Finding places, navigating our way, or simply checking what’s around — these are essential things that smartphones can help with. This year Apple added maps functionality to the SwiftUI framework. Let’s check out how we can use it and what are the current problems.

Image for post

I would like to point out that this covers Xcode 12 beta software and it can change in future releases.

Present the MapView

To show MapView we need to use MapKit’s structure Map that is specifically designed to use with SwiftUI. It is a view that displays an embedded map interface. We can use it to configure user-allowed interactions, show and track current location, and add annotations on the map.

It comes with several initialize methods. Let’s see how to create a map specifying the map visible map region and add annotations.

The coordinate region defines the area that is visible on the map. It is a Binding that takes an MKCoordinateRegion object that is a combination of center coordinate and a coordinate span around it.

struct MapView: View {
    @State var coordinateRegion = MKCoordinateRegion(
      center: CLLocationCoordinate2D(latitude: 56.948889, longitude: 24.106389),
      span: MKCoordinateSpan(latitudeDelta: 0.2, longitudeDelta: 0.2))

    var body: some View {
      Map(coordinateRegion: $coordinateRegion)
        .edgesIgnoringSafeArea(.all)
    }
  }

Now we have a map showing the capital city of Latvia — Riga. With this init method we can specify other things like interaction capabilities, showing user location, and more.

Image for post

#ios #swiftui #maps #swift #ios-app-development

Mapview with SwiftUI
3.45 GEEK