top of page

Mapping Your Way with MapKit and SwiftUI: A Beginner's Guide to Location-Based Wonders 🗺️

Updated: Mar 25, 2023

Greetings, brave explorers of the SwiftUI realm! Are you ready to embark on a thrilling adventure to unlock the secrets of location-based sorcery? Today, we’ll delve into the enchanting world of MapKit and SwiftUI to create stunning maps that will guide our users on their own heroic journeys. 🌍

Fear not, young adventurer, for this guide is designed to be informative, welcoming, and easy to understand. So grab your compass, pack your sense of wonder, and let’s set sail! ⛵

The Magic Ingredients: MapKit and SwiftUI 🧙

Before we begin, let’s familiarize ourselves with the mystical components that will help us craft our map:

  • MapKit: A powerful framework by Apple that enables us to integrate maps and location services into our apps.

  • SwiftUI: Apple’s modern UI toolkit for creating slick user interfaces with Swift code.

With these two forces combined, we’ll create an immersive and delightful map experience for our users.

Crafting the Location Model: Our Trusty Compass 🧭

First, we’ll create a simple Landmark model to represent the points of interest on our map:

import Foundation
import MapKit

struct Landmark: Identifiable {
    let id = UUID()
    let name: String
    let coordinate: CLLocationCoordinate2D

Our Landmark struct holds the name and coordinates of each point of interest. With this trusty compass in hand, we're ready to set sail!

Charting the MapView: SwiftUI’s Cartographic Canvas 🗺️

Now, let’s create a custom MapView using SwiftUI and MapKit:

import SwiftUI
import MapKit

struct MapView: UIViewRepresentable {
    var landmarks: [Landmark]

    func makeCoordinator() -> Coordinator {

    func makeUIView(context: Context) -> MKMapView {
        let mapView = MKMapView()
        mapView.delegate = context.coordinator
        return mapView

    func updateUIView(_ uiView: MKMapView, context: Context) {
        updateAnnotations(for: uiView)

    private func updateAnnotations(for mapView: MKMapView) {

        let annotations = { landmark -> MKPointAnnotation in
            let annotation = MKPointAnnotation()
            annotation.title =
            annotation.coordinate = landmark.coordinate
            return annotation


    class Coordinator: NSObject, MKMapViewDelegate {
        var parent: MapView

        init(_ parent: MapView) {
            self.parent = parent

In this code, we define a custom MapView that conforms to UIViewRepresentable. This protocol allows us to bridge the gap between the UIKit-based MKMapView and our SwiftUI app.

The MapView takes an array of Landmark objects and displays them as annotations on the map. It also defines a Coordinator class to handle the map view's delegate methods.

Assembling the MapView in SwiftUI: Exploring the New World 🌎

With our MapView crafted, it's time to showcase it in our SwiftUI app:

import SwiftUI

struct ContentView: View {
    let landmarks: [Landmark] = [
        Landmark(name: "Great Pyramid of Giza", coordinate: CLLocationCoordinate2D(latitude: 29.9792, longitude: 31.1342)),
        Landmark(name: "Eiffel Tower", coordinate: CLLocationCoordinate2D(latitude: 48.8584    longitude: 2.2945))

var body: some View {
    NavigationView {
        MapView(landmarks: landmarks)
            .navigationBarTitle("Wonders of the World", displayMode: .inline)

In our ContentView, we define an array of Landmark objects representing some of the world's most famous locations. Then, we create a NavigationView that contains our MapView, passing in the landmarks array. Finally, we set the navigation bar title to "Wonders of the World."

Behold the Magic: Run the App and Marvel at Your Creation 🌟

With everything in place, it’s time to run the app and witness the power of MapKit and SwiftUI combined! Your map will now display the chosen landmarks, and users can explore these wondrous locations at their fingertips.

In Conclusion: A World of Location-Based Wonder Awaits 🌈

Congratulations, brave explorer! You’ve successfully charted the world of MapKit and SwiftUI, and your map is now a shining beacon of location-based magic. With these newfound skills, you can create even more captivating experiences for your users as they navigate the enchanting realms of your app.

Now, go forth and spread the wonder of MapKit and SwiftUI! And remember, the world is your oyster, and the map is your canvas. Happy coding! 🌍🚀

7 views0 comments


bottom of page