Skip to main content
SBM blog CTA mobile 1

Drive growth and reduce costs with omnichannel business messaging

Map preview and location sharing on Android

20191112 Map20preview20and20location20sharing20on20android1
Nov 20, 2019
Alek Jeziorek
Alek Jeziorek
Staff Writer
SBM blog CTA mobile 1

Drive growth and reduce costs with omnichannel business messaging

SBM blog CTA mobile 1

Drive growth and reduce costs with omnichannel business messaging

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.

Sending a message with location information and a map preview in Sendbird

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.

sharing a map preview android blog image

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.

Which apps should use map preview and location sharing?

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.

Use-cases for map preview and location sharing

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:

  • Transportation and ridesharing services
  • Delivery and logistics services
  • Food or grocery delivery
  • Other consumer product delivery
  • Online to offline services

Implementing location sharing and map preview in Sendbird

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:

  • Understand the prerequisites for location sharing and map preview on Android
  • Obtain permission to use the user’s location
  • Declare your API access token and a dependency for maps
  • Create the layout for sender and recipient
  • Retrieve the user’s location
  • Send the user’s location in a UserMessage
  • Handle the received message by checking the custom type and bind it to a view

Prerequisites for location sharing and map preview

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:

Note:

  • If you are using Android’s built in emulator, note that it will likely not show your location.

Other resources:

Obtain permission to user the user’s location

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 and add a dependency for maps

Declare your API access token in the AndroidManifest.xml

Create the layout for sender and recipient

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.

Me (sender): https://gist.github.com/AlexPrestonSB/e8b8f5ef59993e689f443aa7eab1b7e5

Other (recipient): https://gist.github.com/AlexPrestonSB/0ac419e532bc8c2cd668712bc40e6756

Note:

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 user’s location

Retrieve the geographic coordinates from the LocationManager. This happens when the user decides to share their location.

Send the user’s location in a UserMessage

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.

Bind the view to your customer viewHolders

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.

Users can now share their location in a single tap

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.

Ebook Grow background mobile

Take customer relationships to the next level.

Ready for the next level?