HERE Android SDK Developer's Guide

Adding Route Calculation to the Simple Application

An important feature of many map-based applications is offering directions to users by calculating a route and rendering it on a map. This tutorial lesson introduces the addition of such functionality to applications developed using the HERE Android SDK.

Getting Started

This tutorial uses the Android project from Creating a Simple Application Using the HERE SDK as a starting point.

Note: The tasks presented on this page assume that you gave the name "BasicMapSolution" to your Android Studio project and the name com.here.android.tutorial to your application package.

Add a "Get Directions" Button

Add a "Get Directions" Button element to the BasicMapSolution application as follows:
  1. From the Project view, double-click the BasicMapSolution/res/values/strings.xml file to open it for editing.
  2. Add the following lines to the existing group of strings
    <string name="label_button_getdirections">
      Get Directions
    </string>
    <string name="textview_routecoordinates_2waypoints">
      Press "Get Directions" button to calculate a route:\nStart: Burnaby\nEnd: YVR Airport
    </string>
  3. From the Android Studio Project view, double-click the BasicMapSolution/app/src/main/res/layout/activity_main.xml file to open it for editing.
  4. Add the following markup directly beneath the LinearLayout tag:
    <!-- UI element declaration for "Get Directions" button -->
    <android.widget.Button
      android:id="@+id/directionsbutton"
      android:layout_width="wrap_content"
      android:layout_height="wrap_content"
      android:text="@string/label_button_getdirections"
      android:onClick="getDirections" />
  5. Next, change the <TextView/> tag to the following:
    <TextView
      android:id="@+id/title"
      android:layout_width="wrap_content"
      android:layout_height="wrap_content"
      android:text="@string/textview_routecoordinates_2waypoints"
      tools:context=".BasicMapActivity" />
  6. From the Project view, double-click the BasicMapSolution/app/src/main/java/com.here.android.tutorial/BasicMapActivity.java file to open it for editing.
  7. Add the following lines to the import statements near the top of the source file:
    import java.util.EnumSet;
    import java.util.List;
    import android.view.View;
    import android.widget.TextView;
    import android.widget.Toast;
    import com.here.android.mpa.common.GeoBoundingBox;
    import com.here.android.mpa.mapping.MapRoute;
    import com.here.android.mpa.routing.RouteManager;
    import com.here.android.mpa.routing.RouteOptions;
    import com.here.android.mpa.routing.RoutePlan;
    import com.here.android.mpa.routing.RouteResult;
  8. Add the declaration of these private member variables above the onCreate method:
    // TextView for displaying the current map scheme
    private TextView textViewResult = null;
    
    // MapRoute for this activity
    private MapRoute mapRoute = null;
    and to set up the member variables for use later, add these lines at the end of the initialize method:
    textViewResult = (TextView) findViewById(R.id.title);
    textViewResult.setText(R.string.textview_routecoordinates_2waypoints);
  9. Add code to handle the click of the "Get Directions" button at the end of the class: First, create an implementation of the listener:
    private RouteManager.Listener routeManagerListener =
        new RouteManager.Listener()
    {
      public void onCalculateRouteFinished(RouteManager.Error errorCode,
        List<RouteResult> result) {
    
        if (errorCode == RouteManager.Error.NONE &&
          result.get(0).getRoute() != null) {
    
          // create a map route object and place it on the map
          mapRoute = new MapRoute(result.get(0).getRoute());
          map.addMapObject(mapRoute);
    
          // Get the bounding box containing the route and zoom in
          GeoBoundingBox gbb = result.get(0).getRoute().getBoundingBox();
          map.zoomTo(gbb, Map.Animation.NONE,
            Map.MOVE_PRESERVE_ORIENTATION);
    
          textViewResult.setText(
            String.format("Route calculated with %d maneuvers.",
              result.get(0).getRoute().getManeuvers().size()));
        } else {
          textViewResult.setText(
            String.format("Route calculation failed: %s",
              errorCode.toString()));
        }
      }
    
      public void onProgress(int percentage) {
        textViewResult.setText(
          String.format("... %d percent done ...", percentage));
      }
    };
    Next, add the implementation of getDirections() below:
    // Functionality for taps of the "Get Directions" button
    public void getDirections(View view) {
      // 1. clear previous results
      textViewResult.setText("");
      if (map != null && mapRoute != null) {
        map.removeMapObject(mapRoute);
        mapRoute = null;
      }
    
      // 2. Initialize RouteManager
      RouteManager routeManager = new RouteManager();
    
      // 3. Select routing options via RoutingMode
      RoutePlan routePlan = new RoutePlan();
    
      RouteOptions routeOptions = new RouteOptions();
      routeOptions.setTransportMode(RouteOptions.TransportMode.CAR);
      routeOptions.setRouteType(RouteOptions.Type.FASTEST);
      routePlan.setRouteOptions(routeOptions);
    
      // 4. Select Waypoints for your routes
      // START: Burnaby
      routePlan.addWaypoint(new GeoCoordinate(49.276271, -123.113224));
    
      // END: YVR Airport
      routePlan.addWaypoint(new GeoCoordinate(49.1947289, -123.1762924));
    
      // 5. Retrieve Routing information via RouteManagerListener
      RouteManager.Error error =
        routeManager.calculateRoute(routePlan, routeManagerListener);
      if (error != RouteManager.Error.NONE) {
        Toast.makeText(getApplicationContext(),
            "Route calculation failed with: " + error.toString(),
            Toast.LENGTH_SHORT)
            .show();
      }
    };
  10. Save all files in the BasicMapSolution project by selecting File > Save All.
  11. Press the key combination Shift + F10 (or Ctrl + R on Macs) within Android Studio to run this HERE Android SDK application with route calculation functionality.

Result: your HERE Android SDK application displays a map centered on the Vancouver municipal region, with a "Get Directions" button and a text box reflecting current status. Tapping on the button triggers a route calculation for the hard-coded set of waypoints between Burnaby and YVR Airport. The route is rendered and displayed on the map if it was successful.

You cannot use this account to purchase a commercial plan on Developer Portal, as it is already associated to plans with different payment methods.

To purchase a commercial plan on Developer Portal, please register for or sign in with a different HERE Account.

Something took longer than expected.

The project should be available soon under your projects page.

Sorry, our services are not available in this region.

Something seems to have gone wrong. Please try again later.

We've detected that your account is set to Australian Dollars (AUD).
Unfortunately, we do not offer checkouts in AUD anymore.
You can continue using your current plan as normal, but to subscribe to one of our new plans,
please register for a new HERE account or contact us for billing questions on selfservesupport@here.com.