Add Layer

This sample shows how to add a tiled map service as a basemap and a dynamic map service as a map overlay layer.


  • ArcGISTiledMapServiceLayer
  • ArcGISDynamicMapServiceLayer

Sample Design

The sample is a simple illustration on how to toggle the visibility of a map layer using a long press gesture. The MapView and it's initial extent are created inside a LinearLayout which gets inflated in the onCreate() method. The Tiled service basemap and the Dynamic US Streets overlay layer get added to the MapView with the addLayer() method on start of the application. The MapView.setOnPressListener() method is where the logic is to toggle the visibility of the Dynamic US Streets overlay layer. It first checks that the MapView is loaded and if so does a secondary check to confirm dynamic layer is not null. If the app passes both checks the layers visibility is toggled through the ArcGISDynamicMapServiceLayer.setVisibility() method.

Sample Code

/* Copyright 2012 ESRI
 * All rights reserved under the copyright laws of the United States
 * and applicable international laws, treaties, and conventions.
 * You may freely redistribute and use this sample code, with or
 * without modification, provided you include the original copyright
 * notice and use restrictions.
 * See the Sample code usage restrictions document for further information.

import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;


 * This sample shows how you can add a tiled map service as a basemap, and a
 * dynamic map service as a second map layer. It also allows the user to toggle
 * the visibility of the dynamic layer with a long press on the map.
public class AddLayer extends Activity {

	private static final String TILED_WORLD_STREETS_URL = "";
	private static final String DYNAMIC_USA_HIGHWAY_URL = "";

	MapView mMapView = null;
	ArcGISDynamicMapServiceLayer mStreetsLayer = null;

	public void onCreate(Bundle savedInstanceState) {

		// Retrieve the MapView from XML layout.
		mMapView = (MapView) findViewById(;

		// Add a base map layer to the MapView.
		mMapView.addLayer(new ArcGISTiledMapServiceLayer(

		// Create a second layer, this time using a US Highways and Streets map service.
		// Set an instance variable to this layer so it can be used from within
		// a long press listener.
		mStreetsLayer = new ArcGISDynamicMapServiceLayer(

		// Add the dynamic layer to the MapView.
	  public boolean onCreateOptionsMenu(Menu menu) {
	    // Inflate the menu; this adds items from the Menu XML to the action bar, if
	    // present.
	    getMenuInflater().inflate(, menu);

	    return true;
	  public boolean onOptionsItemSelected(MenuItem item) {

	    // menu item selected.
	    switch (item.getItemId()) {
			// Check the map is already loaded before attempting to change it.
			if (mMapView.isLoaded()) {
				if (mStreetsLayer != null) {
					// Toggle the dynamic layer's visibility
		return true;

	    	return true;

	protected void onPause() {

		// Call MapView.pause to suspend map rendering while the activity is
		// paused.

	protected void onResume() {

		// Call MapView.unpause to resume map rendering when the activity
		// returns to
		// the foreground.

Feedback on this topic?