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.

Features

  • 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.
 * 
 */
package com.esri.arcgis.android.samples.addlayer;

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

import com.esri.android.map.MapView;
import com.esri.android.map.ags.ArcGISDynamicMapServiceLayer;
import com.esri.android.map.ags.ArcGISTiledMapServiceLayer;

/**
 * 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 = "http://services.arcgisonline.com/ArcGIS/rest/services/World_Street_Map/MapServer";
	private static final String DYNAMIC_USA_HIGHWAY_URL = "http://sampleserver1.arcgisonline.com/ArcGIS/rest/services/Specialty/ESRI_StateCityHighway_USA/MapServer";

	MapView mMapView = null;
	ArcGISDynamicMapServiceLayer mStreetsLayer = null;

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.main);

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

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

		// 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(
				DYNAMIC_USA_HIGHWAY_URL);
		
		mStreetsLayer.setOpacity(0.5f);

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

	    return true;
	  }
	  
	  @Override
	  public boolean onOptionsItemSelected(MenuItem item) {

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

	    default:
	    	return true;
	    }
	  }

	@Override
	protected void onPause() {
		super.onPause();

		// Call MapView.pause to suspend map rendering while the activity is
		// paused.
		mMapView.pause();
	}

	@Override
	protected void onResume() {
		super.onResume();

		// Call MapView.unpause to resume map rendering when the activity
		// returns to
		// the foreground.
		mMapView.unpause();
	}

}
Feedback on this topic?