Subscribe
Subscribe to Email updates

Please use a valid email address.

SendBird's
Privacy Policy.
Search
Map preview and location sharing on Android
Share

Map preview and location sharing on Android

Nov 20, 2019

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.

map preview

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

...waiting for Gist...

Declare your API access token and add a dependency for maps

Declare your API access token in the AndroidManifest.xml

...waiting for Gist...

Add a dependency for maps in the app level build.gradle file.

...waiting for Gist...

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.

...waiting for Gist...

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.

...waiting for Gist...

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.

...waiting for Gist...

Based on what getItemViewType returns, inflate the layout according to whether the message belongs to “me” (sender) or “other” (recipient).

...waiting for Gist...

Next, bind the views to your custom viewHolder.

...waiting for Gist...

In your custom viewHolder, implement OnMapReadyCallback, and override the onMapReady function.

...waiting for Gist...

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.

Get Started!
1,000 MAU and 25 connections free forever
Get Started!
1,000 MAU and 25 connections free forever
Related articles
How SendBird’s user roles and permissions help you moderate chat
Permissions hierarchy and roles to empower your users to moderate and manage conversations.
ALEX PRESTON
Solutions Engineer
Read status for messages and users
Time dependent information users need to jump in and out of conversations and easily find their place.
JASON ALLSHORN
Solutions Engineer
Enabling push notifications, snooze, and Do not disturb in SendBird
Push notifications are essential for any app with chat and messaging because users receive messages from people they care about, about topics highly relevant to them, or from
IMJU BYON
Solutions Engineering Manager
© SendBird 2019. All rights reserved.
Follow us