+92 308 5039935
Get a Free Quote
+92 308 5039935

Build Live Location Sharing in your App

Build Live Location Sharing in your App

Use the following tutorial to build Live Location Sharing in your app. This is divided into three section.

  1. In the first section, we will do a basic setup of Hypertrack SDK.
  2. In the second section, we will select a destination and start a Live Location trip to that place.
  3. In the last section, we will get your friend to join the trip started by you.

Let’s get started 😊 . Strap yourself in and get ready for an exciting ride 🚀 .


Basic Setup


Step 1. Get API keys

Get your HyperTrack API keys here.


Step 2. Use this project

Get your HyperTrack API keys here, and add the publishable key to key.properties file.


Step 3. Setup HyperTrack SDK

Set up the HyperTrack SDK by following these instructions. Else, initialize the SDK by putting the following code in MyApplication.java file.


Step 4. Create HyperTrack user

The next thing that you need to do is to create a HyperTrack user. It helps us tag the location/activity data with the user and share their Live Location status with friends. More details about the function here.

When the user is created, we need to start tracking his location and activity. Call the following method to do so HyperTrack.startTracking()

For starter project, go to ProfilePresenter.java. When the user taps login, get the name of the user and use the following function to create a user.


Step 5. FCM Integration

The HyperTrack SDK requires FCM for a battery efficient real-time tracking experience.

  1. Refer to the FCM Integration guide.
  2. After setting up your account on the Firebase console, you will need to add the google-services.json file to your app folder.


Step 6. Crashlytics Setup (Optional)

You can also enable the crashlytics crash reporting tool. This is optional and if you choose to skip this won’t affect your integration.

  1. Get your Crashlytics key, see the section Add Your API Key from here and you can copy your API Key.
  2. Paste the key to fabric.properties. Create a new fabric.properties file, if it doesn’t exist already.


Start a Live Location trip

Are you ready to rock and roll?


Step 1. Show Live Location view

Now to start a Live Location trip, the first thing that you need to do is to add a destination. For this, we will need a location picker. The good news is that HyperTrack Location View has a location picker within it. Once the user selects a location with the help of our inbuilt location picker, than the sdk gives a callback to the app with the selected location so that the app can start a trip.

For starter project, Check Home.java embedding the HyperTrackMapFragment view in content_home.xml view. Initialize the HyperTrackMapFragment inside oncreate method of Home activity and set your implementation of HyperTrackMapAdapterMapFragmentCallback for HyperTrackMapFragment.

Implement this method in the MapFragmentCallback as specified above. You will get a callback when a user selects an expected place using SDK’s Place Selector View.


Step 2. Create and track action

When the user selects a location, you will get a callback in the onExpectedPlaceSelected function of your MapFragmentCallback instance. This is the right time to start a trip. For starting a trip, you need to create a session. This can be achieved by creating a ‘visit’ action.
You will need two things to create an action.

  1. expectedPlace – This is the destination for the visit. You have it after you select the destination.
  2. collectionId – A collectionId is an identifier created by you for the Live Location trip. A collectionId is what needs to be shared with the friend, so they can join your trip and share their location. We chose it to be the UUID. You can use your own internal identifiers.

For starter project, go to Home.java and add the following code when you get a callback of place selection onExpectedPlaceSelected.

Also, allow user to stop sharing his location by handling stopSharing method in HomePresenter.java file so the action gets completed when the user taps stop sharing.


Step 3. Share your trip

As described earlier, a collectionId is an identifier which identifies a Live Location trip. When you want to share your trip, your trip’s collectionId needs to be shared.

You can share your collectionId to the other person in different ways.

  1. You can use the Android’s ShareCard Intent to share it through messaging apps.
  2. You can use your backend to send the collectionId.

For starter project, let us keep it simple and use Android’s ShareCard to do the job for us.


Track or join an ongoing trip

If you have completed the steps in above section, you have a user who has started a Live Location session. Once their friend receives a collectionId (either through your own backend or through a messaging app), she can use it to track the user and optionally join the trip if you add a few lines of code as described in the following steps.


Step 1. Track ongoing trip

To track the user, use the following function. Although the tracking has started in the SDK, visualizing it requires you to embed HyperTrack’s map fragment in your activity containing hypertrack map view.

For starter project, you have to enter the collectionId in the text field that you have received from the user who has started the session. Add the following code in the click of track button onTrackSharedLinkButtonClick().

Now to see the result, go to the other device and set up the user. After that click on ‘Track a Live Location trip’ and paste/enter the collectionId which you received from the user.


Step 2. Join ongoing trip

In this step, we will see how the friend can share her Live Location and join the trip. To join the trip, an action with the same collectionId needs to be created. This step is similar to Step 6. But this time it is a collectionId of an existing trip and NOT a new one in Step 6.

For starter project, add this code to onShareLiveLocationBack() when the user taps the ‘Share Live Location’ button. It creates and assign action for your friends Live Location session.

We hope you’ve enjoyed yourself on your epic quest to build a Live Location feature. If you have any problems or suggestions for the tutorial, please do not hestitate to buzz 🐝 us here.

Hello, I am a web designer and developer working since 2013. I build WordPress themes, write and extend WordPress plugins, edit WordPress themes and plugins in accordance to customer needs and play games when free ;-)
Related Post

Leave a Reply

Your email address will not be published. Required fields are marked *