Add layer to map

Complexity: Beginner Data Requirement: ArcGIS.com

This topic introduces concepts of map controls and map layers and guides you through the process to quickly add a map service into the map control. It also explains how basic map attributes (spatial reference, initial extent, layer visibility, and so forth) work with the map.

Before you start, you should have completed the Hello World Map sample, which teaches you how to create an Android map application using the Eclipse IDE.

Map

You use the map control to display geographic information. The Android map control is MapView, which is a ViewGroup displaying map and feature services as child views.

Layer

A layer represents a displayable GIS resource inside the map control, which could be a map service, a feature service, an image service, or a graphics resource that is the geometry container.

A Layer object in the Android API is not an Android view so it cannot be added through XML. Instead all instances of layers on a MapView must be configured programatically.

Add a layer programmatically

Steps:
  1. Add some variables to your class.
    private MapView map = null;
    String dynamicMapURL = "http://sampleserver1.arcgisonline.com/ArcGIS/rest/services/Specialty/ESRI_StateCityHighway_USA/MapServer";
    

    The map variable references the MapView instance. dynamicMapURL points to the dynamic map service you are going to add.

  2. From the onCreate() method of an activity add the following:
    // Retrieve the map from XML layout
    		map = (MapView) findViewById(R.id.map);
    	// Add dynamic layer to MapView
    		ArcGISDynamicMapServiceLayer dynamicLayer = new ArcGISDynamicMapServiceLayer(
    				dynamicMapURL);
    		map.addLayer(dynamicLayer);
    

    The map reference is captured using the findViewById method. To add a layer to the map, construct the ArcGIS dynamic map service layer using the service URL, then call the map.addLayer() method.

Layer visibility

Hiding and showing the layer are basic operations of the map. Through a single tap, you can toggle the visibility of the USA layer you just added.

Steps:
  1. Insert the following code into the onCreate method:
    map.setOnSingleTapListener(new OnSingleTapListener() {
    			private static final long serialVersionUID = 1L;
    		
    			public void onSingleTap(float x, float y) {
    				if (map.isLoaded()) {
    
    					Layer layer = map.getLayer(0);
    
    					if (layer.isVisible) {
    						layer.setVisibile(false);
    					} else {
    						layer.setVisibile(true);
    					}
    				}
    
    			}
    		});
    

    Implement OnSingleTapListener() and set it to MapView.setOnSingleTapListener. Then use the Layer.isVisible() method to determine if the layer is visible and the Layer.setVisible() method as appropriate.

Spatial reference, initial extent, and current extent

Layers in the same map view might come with different spatial references by default. It is important to render all the layers with the same spatial reference. MapView takes the first added layer's spatial reference as its spatial reference. If you configure a map layout file, the topmost layer inside the map view would be the map's spatial reference.

When you have multiple tiled layers in the map, these tiled map services should use the same spatial reference. You can have the dynamic map service with a different spatial reference and MapView would do the automatic projection.

Initial extent defines the visible area that you expect to see when the map first loads, while current extent describes the current visible area. A map has a current extent at any time, while sometimes, the map loads without an initial extent.

You can set an arbitrary initial extent for MapView in your map layout file, but when you call getExtent on MapView to get the current extent, most probably you will get an extent that is different from the initial extent. MapView would try its best to render the map area defined in initExtent to fit the device screen, but since the aspect ratio of your initial extent might not be the same as the aspect ratio of the device screen, the map would recalculate the aspect of the extent for you.

1/24/2013