Sharing a user’s location in a message with a digestible map preview. Easily coordinate offline interactions between service providers and other users in a single message.
Sharing one’s location in a map view allows users to share their location with service providers of all kinds, so users can easily coordinate live interactions or share other geo information.
Using the Google Play Service Maps SDK, users can share a location in Google Maps and either open it or direct to that location in the Google Maps app. SendBird displays the map in an easily digestible preview for quick consumption.
Location sharing and map previews are useful for sharing real-time location information. They therefore benefit apps that can use this information for successfully delivering services. With location sharing, users can send their location to pick-ups, last mile delivery, or drivers to ensure complete transactions in a timely manner. On the other side of the marketplace, service providers can send location updates to inquiring customers and communicate transparently with other users.
On-demand services and delivery apps, or any service that facilitates an offline interaction, will find this feature particularly useful for coordinating pick-up, delivery, or other services.
These use-cases will find value in location sharing and map preview:
This guide outlines how to implement location sharing in SendBird on Android with Google’s MapView.
When a user presses the “Share Location” button, Android’s LocationManager class provides the user’s geographic coordinates. SendBird sends their location in a UserMessage with the CustomType set to location. After sending the message, insert it into the RecyclerView through its adapter. Based on the MessageType(UserMessage) and the CustomType(location) the layoutInflater inflates a custom view containing the MapView. Finally, you add geographic coordinates to the MapView.
To implement location sharing and map preview, follow these steps:
This guide assumes that you have already implemented chat using the SendBird SDK.
The code in this guide uses SendBird’s SyncManager SDK to send and receive messages.
You will need:
In order for location sharing to work, you need to obtain permission to use the user’s location.
Request this at runtime. This guide omits the runtime code for the sake of brevity.
Instead it shows permission as it is shown in the AndroidManifest.xml
Declare your API access token in the AndroidManifest.xml
Add a dependency for maps in the app level build.gradle file.
Create a layout for both sides of the chat, both sender and recipient. This guide differentiates between them as “me” and “other,” where “me” represents the sender and “other” represents the recipient. Both should contain a MapView.
See the full gist for each layout.
Other (recipient): https://gist.github.com/AlexPrestonSB/0ac419e532bc8c2cd668712bc40e6756
This guide skips over the implementation for setting up the recyclerview, setting button, listeners etc. You can view the code in the gist here.
Retrieve the geographic coordinates from the LocationManager. This happens when the user decides to share their location.
Once you obtain the longitude and latitude, send it as a UserMessage. To indicate that it is a location, set the CustomType on the individual message to “location.”
Next, insert the message into the RecyclerView by adding it to the adapter.
To see the full implementation, see here.
After adding the messages to the adapter, check the message type before binding the view. Do this by overriding getItemViewType.
Based on what getItemViewType returns, inflate the layout according to whether the message belongs to “me” (sender) or “other” (recipient).
Next, bind the views to your custom viewHolder.
In your custom viewHolder, implement OnMapReadyCallback, and override the onMapReady function.
The full Adapter code is here.
With SendBird’s map preview and location sharing implementation, users can send a map showing their location in a single tap, making communication between users satisfying and on-demand services easier to complete quickly.