Overview

You will learn: how to display a vector basemap with custom styles in a native app.

With the ArcGIS Runtime SDK for Android you can build mapping applications that use Esri vector basemaps with custom styles. If you don't already have a custom basemap style, you can create a new one with the ArcGIS Vector Tile Style Editor. This application allows you to create a style interactively and then save the style in ArcGIS Online as a portal item. Each portal item has a unique ID that your application can use to load the style as a layer or a basemap. See Display a map for more details. To learn more about how to create a new custom style with the ArcGIS Vector Tile Style Editor, see the Style a vector basemap lab.

In this lab, you will build a mapping app that loads a custom vector basemap named Forest and Parks Canvas Map. This basemap emphasizes national forests and parks.

Before you begin

Make sure you have installed the latest version of Android Studio.

You must have previously installed the ArcGIS Runtime SDK for Android and set up your development environment. Please review the install and set up instructions if you have not done this.

Prepare your data

Complete the lab Style a vector basemap. You will use the map style created in that lab for the custom map required in this lab.

Reuse the starter project

In a new or empty project folder, make a copy of the Create a starter app or download and unzip the project solution.

  • Open the new Android project in Android Studio.

  • Run the project and verify the project builds and the map displays in the device simulator.

  • You may be required to sync the project dependencies.

Steps

  1. In the file app > java > {your.package.name} > MainActivity.java, find the existing setupMap method. Remove all of the code in this function and replace it by setting the initial view point with the latitude, longitude and scale variables below. Set the initial viewpoint with the variables so the map zooms to the Santa Monica Mountains when the app loads.

    private void setupMap() {
        if (mMapView != null) {
            double latitude = 34.09042;
            double longitude = -118.71511;
            double scale = 300000;
            map.setInitialViewpoint(new Viewpoint(latitude, longitude, scale));
        }
    }
    
  2. The Forest and Parks Canvas Map is stored on ArcGIS Online (portal). Create a new [Portal](/android/latest/api-reference/reference/com/esri/arcgisruntime/portal/Portal.html) that references ArcGIS Online with http://www.arcgis.com. Create a [PortalItem](/android/latest/api-reference/reference/com/esri/arcgisruntime/portal/PortalItem.html) and pass the portalService and the itemId to the constructor to create the layer.

            /* ** ADD ** */
            String myLayerId = "d2ff12395aeb45998c1b154e25d680e5";
            Portal portalService = new Portal("https://www.arcgis.com", false);
            PortalItem portalItemLayer = new PortalItem(portalService, myLayerId);
    
  3. Create a new basemap from your portal item, and then configure a new ArcGISMap object to use your custom basemap. Once the map object is configured, assign it to the map view.

            /* ** ADD ** */
            ArcGISVectorTiledLayer myCustomTileLayer = new ArcGISVectorTiledLayer(portalItemLayer);
            ArcGISMap map = new ArcGISMap(new Basemap(myCustomTileLayer));
            mMapView.setMap(map);
    
  4. Run the code and explore the custom national forest and parks vector styles.

Congratulations, you're done!

Your map should display the custom forest and parks canvas basemap. Compare your solution with our completed solution project.

Challenge

Use your own custom basemap

Complete the lab Style a vector basemap and use your own custom basemap instead of the one provided here.

Add layers

Review the lab Create a 2D map with a layer and load the trailheads layer on top of your custom basemap.

Handle load errors

Custom basemaps may not load in your app for multiple reasons, including network availability, the item's privacy settings, or an incorrect item ID. Add error handling to your app to alert the user if the map does not load and display a message explaining why.

Update completed labs

Return to any of your completed ArcGIS DevLabs for Android and replace the setupMap method with your new custom basemap version.