Plug in the Android SDK

In this guide, we will setup the HyperTrack SDK in your Android app. At the end of this guide, you would be able see the Live Tracking view with Action's ETA and status, and track progress in real-time on a map with polylines and expected place. This guide covers these objectives, by using the HyperTrack and HyperTrackMapFragment classes.

[warning] Requirements

The Live-Tracking View in the SDK is compatible with Android 4.1 (API level 16) and above.

Live tracking

Step 1: Install the SDK

If not done already, define the repositories and dependencies as shown in your app's build.gradle file. Once configured, run a gradle sync to import the SDK and its dependencies to your project.

[info] build.gradle files

Your project will have two gradle files: one for the app module, and one for the project. Edit the app module file to install the SDK.

repositories {
    maven { url "http://hypertrack-android-sdk.s3-website-us-west-2.amazonaws.com/" }
}

dependencies {
    compile("com.hypertrack:android:0.1.31:release@aar") {
        transitive = true;
    }
}

[warning] Proguard rules

If you are using Proguard in your project add the following lines to your proguard configuration file (proguard-rules.pro):

  • -keep class com.hypertrack.lib.** { *; }
  • -dontwarn com.hypertrack.lib.**

Step 2: Configure the SDK

Now that the SDK has been installed, you can use the SDK methods inside Android Studio. Configure your publishable key and initialise the SDK in the onCreate method of your Application class.

HyperTrack.initialize(this.getApplicationContext(), “YOUR_PUBLISHABLE_KEY”);

Step 3: Disable ActionBar for HyperTrackMapFragment Activity

In case your AppTheme adds an ActionBar by default, add the following under your Activity's theme style-tag in styles.xml file. This is to disable default Action Bar for the activity containing HyperTrackMapFragment in order to enable support for Android AppBar. Refer to Android documentation on Setting up the AppBar.

<!-- Change tracking activity's theme to remove default ActionBar. -->
<style name="TrackingActivityTheme" parent="Theme.AppCompat.Light.NoActionBar">
    ...
    <!-- We will be using the toolbar so no need to show ActionBar -->
    <item name="windowActionBar">false</item>
    <item name="windowNoTitle">true</item>
      ...
</style>
<!--  -->
 <activity android:name=".TrackingActivity"
    android:theme="@style/TrackingActivityTheme"/>

Step 4: Track Actions

Use the HyperTrack.trackAction method with a list of Action Ids created using the HyperTrack SDK or backend APIs.

// Construct an list of ActionIds to be tracked
ArrayList<String> actionIDs = new ArrayList<>();
actionsID.add(actionID.getText().toString());

HyperTrack.trackAction(actionIDs, new HyperTrackCallback() {
    @Override
    public void onSuccess(@NonNull SuccessResponse response) {

        // Handle trackAction success here
        List<Action> actionList = (List<Action>) response.getResponseObject();
        // Start Activity containing HyperTrackMapFragment
        // ActionId can also be passed along as intent extras
        Intent intent = new Intent(this, TrackingActivity.class);
        startActivity(intent);
    }

    @Override
    public void onError(@NonNull ErrorResponse errorResponse) {
        // Handle trackAction error here
        ...
    }
});

Step 5: Setup Activity with HyperTrackMapFragment

  • Firstly, add the following xml snippet in your view layout, to enable the HyperTrack map fragment.
<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" />
  • Secondly, instantiate HyperTrackMapFragment in the onCreate method of the activity in which map fragment has been included.
HyperTrackMapFragment htMapFragment = (HyperTrackMapFragment) getSupportFragmentManager().findFragmentById(R.id.htMapfragment);
  • Optionally, To enable customizations for UI elements, you need to create a class overriding HyperTrackMapAdapter. This HyperTrackMapAdapter class instance then needs to be set using the HyperTrackMapFragment.setHTMapAdapter() method.
public class MyMapAdapter extends HyperTrackMapAdapter {
    public MyMapAdapter(Context mContext) {
        super(mContext);
    }

    @Override
    public int getHeroMarkerIconForActionID(HyperTrackMapFragment hyperTrackMapFragment, String actionID) {
        return R.drawable.custom_icon_hero_marker;
    }
}
  • Optionally, if you want to get callback on various events which happen on HyperTrackMapFragment, you need to instantiate an implementation of the class MapFragmentCallback. This instance then needs to be set using the HyperTrackMapFragment.setMapFragmentCallback() method. For more callback methods, please refer to the reference.

  • Whenever the status of actions are changed then onActionStatusChanged method will invoke and similarly when the Action data refreshed then onActionRefreshed method will invoke with the list of updated actions id.

private MapFragmentCallback callback = new MapFragmentCallback() {
    @Override
    public void onMapReadyCallback(HyperTrackMapFragment hyperTrackMapFragment, GoogleMap map){
        Toast.makeText(context, "On Map Ready callback", Toast.LENGTH_SHORT).show();
    }

    @Override
    public void onHeroMarkerAdded(HyperTrackMapFragment hyperTrackMapFragment, String actionID, Marker heroMarker) {
        Toast.makeText(context, "Hero Marker Added callback", Toast.LENGTH_SHORT).show();
    }

    /**
     * Called when status of any action changes during live tracking.
     *  @param changedStatusActionList is the list of action IDs whose status has changed.
     */
    @Override
    public void onActionStatusChanged(List<String> changedStatusActionList) {
        super.onActionStatusChanged(changedStatusActionList);
    }

    /**
     * Called when action refreshed during live tracking.
     *  @param refreshedActionList is the list of action IDs that are refreshed
     */
    @Override
    public void onActionRefreshed(List<String> refreshedActionList) {
        super.onActionRefreshed(refreshedActionList);
    }
};

Once all this is done, the code snippet would look like as below and once you have the ActionIds to be tracked on the map, your are ready to track actions on the map.

@Override
protected void onCreate(Bundle savedInstanceState) {
  super.onCreate(savedInstanceState);

  ...

  // Initialize Map Fragment added in Activity Layout to getMapAsync
  HyperTrackMapFragment htMapFragment = (HyperTrackMapFragment) getSupportFragmentManager().findFragmentById(R.id.htMapfragment);   
  htMapFragment.setHTMapAdapter(new MyMapAdapter(this));
  htMapFragment.setMapFragmentCallback(mapFragmentCallback);

  ...
}

Step 6: Remove Actions

Call HyperTrack.removeActions API to remove a list of Action Ids from HyperTrack SDK's Live-tracking view.

[warning] Note

This methods is only useful when you have implemented a live tracking experience view in your app.

If NULL is passed as an input parameter in place of actionIDList then all actions will be removed from live tracking view.

Parameter Required Description
actionIds No List of Action IDs
HyperTrack.removeActions(actionIds,  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();
    ...
  }
});

Was this helpful? Yes, thanks! Not really

results matching ""

    No results matching ""