Build live location sharing in your Android marketplace app
Users of marketplace apps often have to meet each other to complete a transactions. When these are out and about, they use their phones to check where the other has reached, often coordinating when and where to meet. Your users are likely messaging each other about this. It’s time to solve their problem better.

If your users can track their Uber coming to them turn-by-turn with an accurate ETA, why not track buyers and sellers similarly! Now it’s your turn.

Follow this tutorial and you will have live location sharing built in your product when you finish.

tl;dr: The tutorial will go through the basic flow of your user picking a destination, then sharing live location with other users, and then the other users sharing their live location right back. The tutorial will focus on building the feature and  trust you with building the right UI/UX for your product :)
PREREQUISITE
Make sure you have plugged in the Android SDK into your marketplace app
PLUG IN THE SDK
IMPLEMENTATION
Follow this 8-step guide to get your use case implemented. Get the source code of the example app implemented using this tutorial here on GitHub.
Developer Evangelist
Step 1: Create map view
When the user taps on the location icon while chatting with another user she is about to meet, open the activity containing the map view.
    // Start Map Activity containing HyperTrackMapFragment on Location Icon tap
    Intent intent = new Intent(this, MapActivity.class);
    startActivity(intent);
    
In this view, if your user taps on “Select a place” bar on the map view, we will trigger an interaction where she will be able to choose a place where she and her friends are going to. The interaction will allow the user to:
- Search for an address
- Choose a location by pinning on the map
- Use current current location
- Use one of the recent places
Developer Evangelist
Step 2: Setup map view
1. Enable HyperTrackMapFragment by adding the following fragment snippet in your Activity’s layout file
    // Add the fragment below in your activity's layout file
    <?xml version="1.0" encoding="utf-8"?>
    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:tools="http://schemas.android.com/tools"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical">
        ...
        <fragment
            android:id="@+id/htMapfragment"
            android:name="com.hypertrack.lib.HyperTrackMapFragment"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            tools:layout="@layout/fragment_map" />
        ...
    </LinearLayout>
    
2. Enable customizations of UI elements by creating a class extending HyperTrackMapAdapter
    //Extend HyperTrackMapAdapter for customizing UI elements in the map view
    public class MyMapAdapter extends HyperTrackMapAdapter {
      ...
      @Override
      public boolean showPlaceSelectorView() {
        return true;
      }
      @Override
      public boolean enableLiveLocationSharingView() {
        return true;
      }
      @Override
      public CameraUpdate getMapFragmentInitialState(HyperTrackMapFragment
            hyperTrackMapFragment) {
        if (SharedPreferenceManager.getLastKnownLocation() != null) {
          LatLng latLng = new LatLng(defaultLatitude, defaultLongitude);
          return CameraUpdateFactory.newLatLngZoom(latLng, 15.0f);
        }
        return super.getMapFragmentInitialState(hyperTrackMapFragment);
      }
      ...
    }
    
3. To get callback on various events which happen on HyperTrackMapFragment, construct an instance of the class MapFragmentCallback
    private MapFragmentCallback mapFragmentCallback = new MapFragmentCallback() {
      ...
      @Override
      public void onExpectedPlaceSelected(Place expectedPlace) {
        super.onExpectedPlaceSelected(expectedPlace);
        if (expectedPlace != null) {
          // Use this place to createAndAssignAction for current userId
        }
      }
      ...
    };
    
4. Instantiate HyperTrackMapFragment in the onCreate method of the activity in which map fragment has been included
    @Override
    protected void onCreate(Bundle savedInstanceState) {
      super.onCreate(savedInstanceState);
      ...
      // Initialize HyperTrackMapFragment in Activity’s layout file to getMapAsync
      HyperTrackMapFragment htMapFragment = (HyperTrackMapFragment) getSupportFragmentManager().findFragmentById(R.id.htMapfragment);
      htMapFragment.setHTMapAdapter(new MyMapAdapter(MapActivity.this));
      htMapFragment.setMapFragmentCallback(mapFragmentCallback);
      ...
    }
    
Developer Evangelist
Step 3: Create & assign action
On the above fragment, add a SHARE LIVE LOCATION button that the user can tap to start sharing location.
Once the same is tapped, create and assign a Visit action to the user.
createAndAssignAction API calls startTracking method automatically in its onSuccess method and you need not call it explicitly.

The lookup_id in ActionParams is a unique identifier for this location sharing session. This will be used to enable live location sharing view for all other users.

For creating ActionParams, receive the expectedPlace object selected by the user as indicated in Step 2.3.
    // Construct action params object using the Place selected by the user
    ActionParams params = new ActionParamsBuilder()
            .setExpectedPlace(MapActivity.this.expectedPlace)
            .setType(Action.ACTION_TYPE_VISIT)
            .setExpectedAt(expectedAtTimestamp)
            .setLookupId("ACTION_LOOKUP_ID_HERE")
            .build();
    HyperTrack.createAndAssignAction(actionParams, new HyperTrackCallback() {
        @Override
        public void onSuccess(@NonNull SuccessResponse response) {
            Action action = (Action) response.getResponseObject();
            String shareMessage = action.getShareMessage();
            // Use Action’s Share message for sharing tracking link
            ...
      }
      @Override
      public void onError(@NonNull ErrorResponse errorResponse) {
          // Handle createAndAssignAction error here
          Toast.makeText(MapActivity.this, errorResponse.getErrorMessage(), Toast.LENGTH_SHORT).show();
          ...
      }
    });
    
Developer Evangelist
Step 4: Share live location via URL
As soon as you assigned the above action to a user, the API would have returned a trck.at URL. Share this URL in the ongoing message. All the users in the chat would see live location of the user, alongwith ETA to the expected place.
Note that lookup_id created in Step 3 has to be passed as meta-data along with the message object that will be shared between users. This will be used to enable live location sharing view for all users who tap on the shared message.
Developer Evangelist
Step 5: See live location in-app
If any of the users on the chat taps on the above message, call HyperTrack.trackActionByLookupId using the lookup_id key passed along with the message.
    HyperTrack.trackActionByLookupId(lookupID,
            new HyperTrackCallback() {
      @Override
      public void onSuccess(@NonNull SuccessResponse response) {
        // Handle trackActionByLookupId API success here
        List<Action> actions = (List<Action>) response.getResponseObject();
        // Save this list of actions as a global variable to be used to
        // enable Share Live Location button for the current user
        MapActivity.this.actions = actions;
        // Show Share Live Location Button to the user
        showShareLiveLocationButton();
        ...
      }
      @Override
      public void onError(@NonNull ErrorResponse errorResponse) {
        // Handle trackActionByLookupId error here
        Toast.makeText(MapActivity.this, errorResponse.getErrorMessage(),
            Toast.LENGTH_SHORT).show();
        ...
      }
    });
    
Developer Evangelist
Step 6: Allow other users to share live location
In the above fragment, add a similar SHARE LIVE LOCATION button (as you did in Step 3) that any of the users on the chat can tap to start sharing live location. On tapping the button, create & assign an action, start tracking, and share URL (similar to Steps 3 & 4).

The ActionID for the Action object created needs to be saved and will be used while calling completeAction API.
    shareLiveLocationButton.setOnClickListener(new View.OnClickListener() {
      @Override
      public void onClick(View v) {
        // Check if a valid list of actions is available
        if (MapActivity.this.actions == null || MapActivity.this.actions.isEmpty())
          return;
        // Construct action params object using the
        ActionParams params = new ActionParamsBuilder()
            .setExpectedPlaceId(MapActivity.this.actions.get(0).getExpectedPlace().getId())
            .setType(MapActivity.this.actions.get(0).getType())
            .setLookupId(MapActivity.this.actions.get(0).getLookupId())
            .build();
        HyperTrack.createAndAssignAction(actionParams, new HyperTrackCallback() {
          @Override
          public void onSuccess(@NonNull SuccessResponse response) {
            Action action = (Action) response.getResponseObject();
            MapActivity.this.actionID = action.getId();
            // Use this actionID while calling completeAction API on stop sharing
            ...
          }
          @Override
          public void onError(@NonNull ErrorResponse errorResponse) {
            // Handle createAndAssignAction error here
            Toast.makeText(MapActivity.this, errorResponse.getErrorMessage(), Toast.LENGTH_SHORT).show();
            ...
          }
        });
      }
    });
    
Developer Evangelist
Step 7: Stop location sharing
Stop live location sharing when the users reach their expected place. When the user taps on Stop Sharing button, call completeAction for the action that was assigned using the actionID saved in Step 6 above, and stopTracking API.
    // Use actionId saved in Step 6 for the Visit Action that you created
    HyperTrack.completeAction(MapActivity.this.actionId);
    HyperTrack.stopTracking();
    
Note that you should replace the SHARE LIVE LOCATION button with STOP SHARING button when the user opens the trackAction fragment after sharing live location. If the user taps on STOP SHARING, call the above code snippet to complete the visit action and stop tracking.
Developer Evangelist
Step 8: Clear map view
Call HyperTrack.removeActions to clear the map view and reset it to default state when the user taps on Stop Sharing or closes the screen.
    HyperTrack.removeActions(null,  new HyperTrackCallback() {
      @Override
      public void onSuccess(@NonNull SuccessResponse response) {
        // Handle removeActions response here
        List<Action> actionList = (List<Action>) response.getResponseObject();
        ...
      }
      @Override
      public void onError(@NonNull ErrorResponse errorResponse) {
        // Handle removeActions error here
        Toast.makeText(MainActivity.this,errorResponse.getErrorMessage(), Toast.LENGTH_SHORT).show();
        ...
      }
    });
    
How simple was that
Yay! You just built live location sharing in your marketplace app. How simple was that? Just a few steps and an hour, and you now have an end-to-end live location sharing feature in your messaging service! 

We are excited to see what live location feature you build in your app. Do ping us at help@hypertrack.io once you build one, and we would love to feature your app on our blog!