Skip to main content

Implement location sharing with Apple MapKit on iOS

Jason Allshorn 1
Jason Allshorn
Solutions Engineer
  • Tutorial Type: Advanced
  • Reading Time: 15 mins
  • Building Time: 1-2 hrs
Chat SDK v4 2x

Swift, Kotlin, and TypeScript SDKs

Build in-app chat, calls, and live streaming

Introduction

Location sharing is one of the most useful functionalities on mobile devices. You can share your location to ensure greater security when using ride sharing services, to communicate the location of a desired delivery place with on-demand services, or to simply to let a friend know where you are.

This tutorial explains how to implement location sharing functionality on iOS using Sendbird and Apple’s MapKit. In this tutorial, we assume that you have already implemented chat using Sendbird. If not, please see this tutorial or the docs to understand how to do this. The code provided in this tutorial is a modification to Sendbird’s basic Swift sample. Note that you’ll get the maximum benefit from this tutorial if you have knowledge of Swift.

Before we dive into the implementation details, let’s talk about some prerequisites.

Prerequisites of building location sharing on iOS with Apple MapKit

For this tutorial, you will need:

  1. Sendbird SDK
  2. MapKit
  3. Sample Code
    1. Download → unzip
    2. In root folder, if needed, “pod install”
    3. In AppDelegate, add app_id
    4. In AppDelegate, remove the “return” statement
    5. Open “SendBird-iOS.xcworkspace”
    6. Run
  4. Search for “//LOCATION_BUILDER” to view related code.

Process of location sharing with Apple MapKit

Let’s first talk through a high level explanation of the steps necessary to send a map with a location pin.

After a user presses the “Share Location” button, the user’s geographic coordinates will be obtained. The location will be sent via a “UserMessage” with the “CustomType” set to “location”. Upon successfully sending the message, it will be inserted into the message view delegate. Based on the MessageType(UserMessage) and the CustomType(location), the message will display a map with a pin located on the coordinates and map radius set small enough for the location to be clear. In the code shared above, search for “//LOCATION_BUILDER” to find where the location sharing code is located.

Location sharing implementation on iOS with Apple MapKit

For location sharing to work, you will need to obtain permission to use the user’s location. This will need to be requested at runtime. The info.plist will need the following settings:

Detecting the user’s location consists of two parts. First, start to detect the user’s location.

Second, listen for when the user’s location is found, and then send a Sendbird message.

Finally, send the coordinates collected in the location sharing as the message text and set the message’s customType to “location”. Note that the code below is an oversimplification of what is in the sample.

Consider displaying left and right message views for both incoming and outgoing messages. The code provides two .xib files and associated TableViewCell classes to display the map messages. The concept is to read the message, detect that it has a customType of location, then render the correct message view. In doing so, also set the message’s map view coordinates and regionRadius.

Conclusion

And that’s it! We talked about how to get location coordinates, send the coordinates collected in the location sharing as the message text, and render a map with a location pin. You now know how to build location sharing functionality for your mobile application using Apple MapKit!

If you need additional information, please see the docs, post implementation questions in the Sendbird community, or contact us. We’re always happy to help!

Happy location sharing! ✌️