HomeLearnHow-toUsing Maps and Location Data in Your SwiftUI (+Realm) App

Using Maps and Location Data in Your SwiftUI (+Realm) App

Updated: Jul 19, 2021 |

Published: Jul 08, 2021

  • Mobile
  • Realm
  • Charts
  • ...

By Andrew Morgan

Rate this article

#Introduction

Embedding Apple Maps and location functionality in SwiftUI apps used to be a bit of a pain. It required writing your own SwiftUI wrapper around UIKit code—see these examples from the O-FISH app:

If you only need to support iOS14 and later, then you can forget most of that messy code 😊. If you need to support iOS13—sorry, you need to go the O-FISH route!

iOS14 introduced the Map SwiftUI view (part of Mapkit) allowing you to embed maps directly into your SwiftUI apps without messy wrapper code.

This article shows you how to embed Apple Maps into your app views using Mapkit's Map view. We'll then look at how you can fetch the user's current location—with their permission, of course!

Finally, we'll see how to store the location data in Realm in a format that lets MongoDB Realm sync it to MongoDB Atlas. Once in Atlas, you can add a geospatial index and use MongoDB Charts to plot the data on a map—we'll look at that too.

Most of the code snippets have been extracted from the RChat app. That app is a good place to see maps and location data in action. Building a Mobile Chat App Using Realm – The New and Easier Way is a good place to learn more about the RChat app—including how to enable MongoDB Realm Sync.

#Prerequisites

  • Realm-Cocoa 10.8.0+ (may work with some 10.7.X versions)
  • iOS 14.5+ (Mapkit was introduced in iOS 14.0 and so most features should work with earlier iOS 14.X versions)

#How to Add an Apple Map to Your SwiftUI App

To begin, let's create a simple view that displays a map, the coordinates of the center of that map, and the zoom level:

Gif of scrolling around an embedded Apple Map and seeing the reported coordinates changing

With Mapkit and SwiftUI, this only takes a few lines of code:

1import MapKit
2import SwiftUI
3
4struct MyMapView: View {
5 @State private var region: MKCoordinateRegion = MKCoordinateRegion(
6 center: CLLocationCoordinate2D(latitude: MapDefaults.latitude, longitude: MapDefaults.longitude),
7 span: MKCoordinateSpan(latitudeDelta: MapDefaults.zoom, longitudeDelta: MapDefaults.zoom))
8
9 private enum MapDefaults {
10 static let latitude = 45.872
11 static let longitude = -1.248
12 static let zoom = 0.5
13 }
14
15 var body: some View {
16 VStack {
17 Text("lat: \(region.center.latitude), long: \(region.center.longitude). Zoom: \(region.span.latitudeDelta)")
18 .font(.caption)
19 .padding()
20 Map(coordinateRegion: $region,
21 interactionModes: .all,
22 showsUserLocation: true)
23 }
24 }
25}

Note that showsUserLocation won't work unless the user has already given the app permission to use their location—we'll get to that.

region is initialized to a starting location, but it's updated by the Map view as the user scrolls and zooms in and out.

#Adding Bells and Whistles to Your Maps (Pins at Least)

Pins can be added to a map in the form of "annotations." Let's start with a single pin:

Embedded Apple Map showing a red pin

Annotations are provided as an array of structs where each instance must contain the coordinates of the pin. The struct must also conform to the Identifiable protocol:

1struct MyAnnotationItem: Identifiable {
2 var coordinate: CLLocationCoordinate2D
3 let id = UUID()
4}

We can now create an array of MyAnnotationItem structs:

1let annotationItems = [
2 MyAnnotationItem(coordinate: CLLocationCoordinate2D(
3 latitude: MapDefaults.latitude,
4 longitude: MapDefaults.longitude))]

We then pass annotationItems to the MapView and indicate that we want a MapMarker at the contained coordinates:

1Map(coordinateRegion: $region,
2 interactionModes: .all,
3 showsUserLocation: true,
4 annotationItems: annotationItems) { item in
5 MapMarker(coordinate: item.coordinate)
6 }

That gives us the result we wanted.

What if we want multiple pins? Not a problem. Just add more MyAnnotationItem instances to the array.

All of the pins will be the same default color. But, what if we want different colored pins? It's simple to extend our code to produce this:

Embedded Apple Map showing red, yellow, and plue pins at different locations

Firstly, we need to extend MyAnnotationItem to include an optional color and a tint that returns color if it's been defined and "red" if not:

1struct MyAnnotationItem: Identifiable {
2 var coordinate: CLLocationCoordinate2D
3 var color: Color?
4 var tint: Color { color ?? .red }
5 let id = UUID()
6}

In our sample data, we can now choose to provide a color for each annotation:

1let annotationItems = [
2 MyAnnotationItem(
3 coordinate: CLLocationCoordinate2D(
4 latitude: MapDefaults.latitude,
5 longitude: MapDefaults.longitude)),
6 MyAnnotationItem(
7 coordinate: CLLocationCoordinate2D(
8 latitude: 45.8827419,
9 longitude: -1.1932383),
10 color: .yellow),
11 MyAnnotationItem(
12 coordinate: CLLocationCoordinate2D(
13 latitude: 45.915737,
14 longitude: -1.3300991),
15 color: .blue)
16]

The MapView can then use the tint:

1Map(coordinateRegion: $region,
2 interactionModes: .all,
3 showsUserLocation: true,
4 annotationItems: annotationItems) { item in
5 MapMarker(
6 coordinate: item.coordinate,
7 tint: item.tint)
8}

If you get bored of pins, you can use MapAnnotation to use any view you like for your annotations:

1Map(coordinateRegion: $region,
2 interactionModes: .all,
3 showsUserLocation: true,
4 annotationItems: annotationItems) { item in
5 MapAnnotation(coordinate: item.coordinate) {
6 Image(systemName: "gamecontroller.fill")
7 .foregroundColor(item.tint)
8 }
9}

This is the result:

Apple Map showing red, yellow and blue game controller icons at different locations on the map

You could also include the name of the system image to use with each annotation.

This gist contains the final code for the view.

#Finding Your User's Location

#Asking for Permission

Apple is pretty vocal about respecting the privacy of their users, and so it shouldn't be a shock that your app will have to request permission before being able to access a user's location.

The first step is to add a key-value pair to your Xcode project to indicate that the app may request permission to access the user's location, and what text should be displayed in the alert. You can add the pair to the "Info.plist" file:

1Privacy - Location When In Use Usage Description : We'll only use your location when you ask to include it in a message

Screenshot from Xcode showing the key-value pair for requesting permission for the app to access the user's location

Once that setting has been added, the user should see an alert the first time that the app attempts to access their current location:

iPhone screenshot – app is requesting permission to access the user's location

#Accessing Current Location

While Mapkit has made maps simple and native in SwiftUI, the same can't be said for location data.

You need to create a SwiftUI wrapper for Apple's Core Location functionality. There's not a lot of value in explaining this boilerplate code—just copy this code from RChat's LocationHelper.swift file, and paste it into your app:

1import CoreLocation
2
3class LocationHelper: NSObject, ObservableObject {
4
5 static let shared = LocationHelper()
6 static let DefaultLocation = CLLocationCoordinate2D(latitude: 45.8827419, longitude: -1.1932383)
7
8 static var currentLocation: CLLocationCoordinate2D {
9 guard let location = shared.locationManager.location else {
10 return DefaultLocation
11 }
12 return location.coordinate
13 }
14
15 private let locationManager = CLLocationManager()
16
17 private override init() {
18 super.init()
19 locationManager.delegate = self
20 locationManager.desiredAccuracy = kCLLocationAccuracyBest
21 locationManager.requestWhenInUseAuthorization()
22 locationManager.startUpdatingLocation()
23 }
24}
25
26extension LocationHelper: CLLocationManagerDelegate {
27 func locationManager(_ manager: CLLocationManager, didUpdateLocations locations: [CLLocation]) { }
28
29 public func locationManager(_ manager: CLLocationManager, didFailWithError error: Error) {
30 print("Location manager failed with error: \(error.localizedDescription)")
31 }
32
33 public func locationManager(_ manager: CLLocationManager, didChangeAuthorization status: CLAuthorizationStatus) {
34 print("Location manager changed the status: \(status)")
35 }
36}

Once added, you can access the user's location with this simple call:

1let location = LocationHelper.currentLocation

#Store Location Data in Your Realm Database

#The Location Format Expected by MongoDB

Realm doesn't have a native type for a geographic location, and so it's up to us how we choose to store it in a Realm Object. That is, unless we want to synchronize the data to MongoDB Atlas using MongoDB Realm Sync, and go on to use MongoDB's geospatial functionality.

To make the best use of the location data in Atlas, we need to add a geospatial index to the field (which we’ll see how to do soon.) That means storing the location in a supported format. Not all options will work with Realm Sync (e.g., it's not guaranteed that attributes will appear in the same order in your Realm Object and the synced Atlas document). The most robust approach is to use an array where the first element is longitude and the second is latitude:

1location: [<longitude>, <latitude>]

#Your Realm Object

The RChat app gives users the option to include their location in a chat message—this means that we need to include the location in the ChatMessage Object:

1class ChatMessage: Object, ObjectKeyIdentifiable {
2
3 @Persisted let location = List<Double>()
4
5 convenience init(author: String, text: String, image: Photo?, location: [Double] = []) {
6 ...
7 location.forEach { coord in
8 self.location.append(coord)
9 }
10 ...
11 }
12 }
13 .
14}

The location array that's passed to that initializer is formed like this:

1let location = LocationHelper.currentLocation
2self.location = [location.longitude, location.latitude]

#Location Data in Your Backend MongoDB Realm App

The easiest way to create your backend MongoDB Realm schema is to enable Development Mode—that way, the schema is automatically generated from your Swift Realm Objects.

This is the generated schema for our "ChatMessage" collection:

1{
2 "bsonType": "object",
3 "properties": {
4 "_id": {
5 "bsonType": "string"
6 },
7 ...
8 "location": {
9 "bsonType": "array",
10 "items": {
11 "bsonType": "double"
12 }
13 }
14 },
15 "required": [
16 "_id",
17 ...
18 ],
19 "title": "ChatMessage"
20}

This is a document that's been created from a synced Realm ChatMessage object:

Screen capture of an Atlas document, which includes an array named location

#Adding a Geospatial Index in Atlas

Now that you have location data stored in Atlas, it would be nice to be able to work with it—e.g., running geospatial queries. To enable this, you need to add a geospatial index to the location field.

From the Atlas UI, select the "Indexes" tab for your collection and click "CREATE INDEX":

Atlas screen capture of creating a new index

You should then configure a 2dsphere index:

Atlas screen capture of creating a new 2dsphere index

Most chat messages won't include the user's location and so I set the sparse option for efficiency.

Note that you'll get an error message if your ChatMessage collection contains any documents where the value in the location attribute isn't in a valid geospatial format.

Atlas will then build the index. This will be very quick, unless you already have a huge number of documents containing the location field. Once complete, you can move onto the next section.

#Plotting Your Location Data in MongoDB Charts

MongoDB Charts is a simple way to visualize MongoDB data. You can access it through the same UI as Realm and Atlas. Just click on the "Charts" button:

Atlas screen capture of MongoDB Charts button

The first step is to click the "Add Data Source" button:

Charts screen capture of adding a new data source

Select your Atlas cluster:

Charts screen capture of adding Atlas cluster as a data source

Select the RChat.ChatMessage collection:

Charts screen capture of selecting the ChatMessage collection in the RChat database

Click “Finish.” You’ll be taken to the default Dashboards view, which is empty for now. Click "Add Dashboard":

Charts screen capture of adding a new dashboard

In your new dashboard, click "ADD CHART":

Charts screen capture of adding a new chart

Configure your chart as shown here by:

  • Setting the chart type to "Geospatial" and the sub-type to "Scatter."
  • Dragging the "location" attribute to the coordinates box.
  • Dragging the "author" field to the "Color" box.

Charts screen capture of configuring a new chart

Once you've created your chart, you can embed it in web apps, etc. That's beyond the scope of this article, but check out the MongoDB Charts docs if you're interested.

#Conclusion

SwiftUI makes it easy to embed Apple Maps in your SwiftUI apps. As with most Apple frameworks, there are extra maps features available if you break out from SwiftUI, but I'd suggest that the simplicity of working with SwiftUI is enough incentive for you to avoid that unless you have a compelling reason.

Accessing location information from within SwiftUI still feels a bit of a hack, but in reality, you cut and paste the helper code once, and then you're good to go.

By storing the location as a [longitude, latitude] array (List) in your Realm database, it's simple to sync it with MongoDB Atlas. Once in Atlas, you have the full power of MongoDB's geospatial functionality to work your location data.

If you have questions, please head to our developer community website where the MongoDB engineers and the MongoDB community will help you build your next big idea with MongoDB.

Rate this article
MongoDB Icon
  • Developer Hub
  • Documentation
  • University
  • Community Forums

© MongoDB, Inc.