Implement location sharing with Apple MapKit on iOS
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:
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.
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!
Happy location sharing! ✌️