Working with MapKit and Annotation for SwiftUI

Simon Ng
AppCoda Tutorials
Published in
6 min readJul 27, 2023

--

MapKit is a powerful framework that allows developers to add maps, annotations, and location-based features to their iOS applications. With SwiftUI, you can easily integrate MapKit into your app and create interactive and dynamic maps that offer a great user experience. In this tutorial, we will explore how to work with maps and annotations in SwiftUI, and how to customize the map style and camera position.

The MapKit Basics

Let’s start with the basics of MapKit. The MapKit framework includes a Map view that developers can use to embed a map in any SwiftUI project. Here is an example:

import SwiftUI
import MapKit

struct ContentView: View {
var body: some View {
Map()
}
}

Before using the Map view, you have to import the MapKit framework. Then, to create a map, simply instantiate a Map view. If you’ve opened the Preview canvas in Xcode, you should see a full screen map in the simulator.

Changing the Initial Position with Map Camera

Instead of displaying a default location, the Map view has another init method for you to change the initial position of the map:

init(
initialPosition: MapCameraPosition,
bounds: MapCameraBounds? = nil,
interactionModes…

--

--

Simon Ng
AppCoda Tutorials

Founder of AppCoda. iOS Developer and Indie Entrepreneur. Love coffee, food and travel. http://www.appcoda.com