HERE Android SDK Developer's Guide

Extending the Simple Application

Beyond creating a basic application, you can extend your application with the use of features from the HERE Android SDK. This tutorial introduces methods to change map attributes such as map center, daytime/nighttime map schemes, and zoom levels.

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 "Go Home" Button

Within this map-based application, users can use gestures to pan, zoom in, and zoom out on different regions on the map. They can also use a UI element to reset the initial map view.

In the following steps, we add a "Go Home" Button element to BasicMapSolution that resets the application's position, orientation, and tilt back to their initial values:
  1. From the Android Studio Project view, double-click the BasicMapSolution/app/src/main/res/values/strings.xml file to open it for editing.
  2. Add the following line to the group of <string></string> tags:
    <string name="label_button_gohome">Go Home</string>
  3. From the Android Studio Project view, double-click the file BasicMapSolution/app/src/main/res/layout/activity_main.xml to open it for editing.
  4. Add the following markup directly above the <fragment /> tag:
    <!-- UI element declaration for "Go Home" button -->
    <android.widget.Button
      android:id="@+id/homebutton"
      android:layout_width="wrap_content"
      android:layout_height="wrap_content"
      android:text="@string/label_button_gohome"
      android:onClick="goHome" />
  5. From the Android Studio Project view, double-click the BasicMapSolution/src/com.here.android.tutorial/BasicMapActivity.java file to open it for editing.
  6. Add the following lines to the import statements near the top of the source file:
    import android.view.View;
    import java.util.List;
  7. Add the following method to the end of the class:
    // Functionality for taps of the "Go Home" button
    public void goHome(View view) {
      if (map != null) {
        // Change map view to "home" coordinate and zoom level
        map.setCenter(new GeoCoordinate(49.196261, -123.004773, 0.0),
            Map.Animation.NONE);
        map.setZoomLevel((map.getMaxZoomLevel() + map.getMinZoomLevel()) / 2);
      }
    }
  8. Press Shift + F10 (or Ctrl + R on Macs) to run this extended basic HERE Android SDK application.

Result: Your HERE Android SDK application displays a map centered on the Vancouver municipal region, plus a "Go Home" button near the top-left corner that allows users to reset the map to its initial view.

Add a "Change Map Scheme" Button

This task introduces the following HERE Android SDK class methods:
  • com.here.android.mpa.mapping.Map.getMapScheme()
  • com.here.android.mpa.mapping.Map.getMapSchemes()
  • com.here.android.mpa.mapping.Map.setMapScheme()

There are numerous map schemes available from HERE Android SDK. You can add a UI element to your application that allows users to change the scheme of the map they are viewing.

Add a "Change Map Scheme" Button element to the BasicMapSolution application as follows:
  1. From the Android Studio Project view, double-click the BasicMapSolution/app/src/main/res/values/strings.xml file to open it for editing.
  2. Add the following lines to the group of <string></string> tags:
    <string name="label_button_changescheme">Change Map Scheme</string>
  3. From the Android Studio Project view, double-click the file BasicMapSolution/app/src/main/res/layout/activity_main.xml to open it for editing.
  4. Add the following markup directly beneath the homebutton tag but above the <fragment /> tag:
    <!-- UI element declaration for "Change Map Scheme" button -->
    <android.widget.Button
      android:id="@+id/schemebutton"
      android:layout_width="wrap_content"
      android:layout_height="wrap_content"
      android:text="@string/label_button_changescheme"
      android:onClick="changeScheme" />
  5. From the Android Studio Project view, double-click the file BasicMapSolution/app/src/main/java/com.here.android.tutorial/BasicMapActivity.java to open it for editing.
  6. Add the following to the import statements near the top of the source file:
    import java.util.List;
  7. Add variable declarations for initial_scheme to the top of the BasicMapActivity class, immediately beneath the declaration for map, as follows:
    public class BasicMapActivity extends Activity {
    
      // map embedded in the map fragment
      private Map map = null;
    
      // map fragment embedded in this activity
      private MapFragment mapFragment = null;
    
      // Initial map scheme, initialized in onCreate() and accessed in goHome()
      private static String initial_scheme = "";
    
      @Override
      public void onCreate(Bundle savedInstanceState) {
      ...
  8. Create function private void onMapFragmentInitializationCompleted() and move all the existing text within OnEngineInitListener's onEngineInitializationCompleted() into it:
    private void onMapFragmentInitializationCompleted() {
      // retrieve a reference of the map from the map fragment
      map = mapFragment.getMap();
      // Set the map center coordinate to the Vancouver region (no animation)
      map.setCenter(new GeoCoordinate(49.196261, -123.004773, 0.0),
          Map.Animation.NONE);
      // Set the map zoom level to the average between min and max (no
      // animation)
      map.setZoomLevel((map.getMaxZoomLevel() + map.getMinZoomLevel()) / 2);
    }
  9. Update the OnEngineInitListener's onEngineInitializationCompleted() implementation as follows:
    mapFragment.init(new OnEngineInitListener() {
      @Override
      public void onEngineInitializationCompleted(OnEngineInitListener.Error error) {
        if (error == OnEngineInitListener.Error.NONE) {
          onMapFragmentInitializationCompleted();
        }
        ...
  10. Add the following method to the end of the class:
    // Functionality for taps of the "Change Map Scheme" button
    public void changeScheme(View view) {
      if (map != null) {
        // Local variable representing the current map scheme
        String current = map.getMapScheme();
        // Local array containing string values of available map schemes
        List<String> schemes = map.getMapSchemes();
        // Local variable representing the number of available map schemes
        int total = map.getMapSchemes().size();
    
        if (initial_scheme.isEmpty())
        {
          //save the initial scheme
          initial_scheme = current;
        }
    
        // If the current scheme is the last element in the array, reset to
        // the scheme at array index 0
        if (schemes.get(total - 1).equals(current))
          map.setMapScheme(schemes.get(0));
        else {
          // If the current scheme is any other element, set to the next
          // scheme in the array
          for (int count = 0; count < total - 1; count++) {
            if (schemes.get(count).equals(current))
              map.setMapScheme(schemes.get(count + 1));
          }
        }
      }
    }
  11. To ensure that a tap of the "Go Home" button resets the map scheme while it is resetting everything else (coordinates, zoom level, etc.), add the following lines to the end of the goHome() method:
    // Reset the map scheme to the initial scheme
    if (!initial_scheme.isEmpty()) {
      map.setMapScheme(initial_scheme);
    }
  12. Press the key combination Shift + F10 (or Ctrl + R on Macs) to run this extended basic HERE Android SDK application.

Result: Your HERE Android SDK application displays a map centered on the Vancouver municipal region, plus a "Go Home" button near the top-left corner that allows users to reset the map to its initial view, followed by a "Change Map Scheme" button that allows users to cycle through available map schemes.

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.