Add Layer to Map
This sample introduces concepts of map control and map layer and guides you through the process to add map service into the map control quickly. It also explains how basic map attributes(spatial reference, initial extent, layer visibility and etc) work with map.
Before you start, you should have completed 'Hello, World' sample, which teaches you how to create an android map application using eclipse IDE.
Map
You use map control to display geographic information(map mainly). The Android map control is 'MapView', which is a 'ViewGroup' displaying map services, feature service as child views.
Layer
Layer represents a displayable GIS resource inside map control, which could be a map service, a feature service, an image service or a graphics resource which is the geometry container.
There are two ways to add layer. You can either configure the map service through the layout XML file or add it programatically.
Before you start, make sure you have put following permission into your project’s manifest file.
<manifest xmlns:android="http://schemas.android.com/apk/res/android" package="com.esri.android.AddLayer" android:versionCode="1" android:versionName="1.0"> <uses-permission android:name="android.permission.INTERNET" /> <uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" /> <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
Add layer statically and dynamicly
- Start a new ‘ArcGIS Android Project’ named 'addLayer'
- Open the 'res/layout/main.xml' and insert the following:
<?xml version="1.0" encoding="utf-8"?> <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="fill_parent" android:layout_height="fill_parent"> <com.esri.android.map.MapView android:id="@+id/map" android:layout_width="fill_parent" android:layout_height="fill_parent" initExtent="-1.3296373526814876E7 3930962.41823043 -1.2807176545789773E7 4201243.7502468005" > <com.esri.android.map.ags.ArcGISTiledMapServiceLayer android:id="@+id/tiles1" url="http://services.arcgisonline.com/ArcGIS/rest/services/World_Street_Map/MapServer"/> </com.esri.android.map.MapView> <TextView android:id="@+id/label" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Tap the screen to toggle USA layer's visibility" android:textColor="#000000" android:layout_centerHorizontal="true" android:layout_marginBottom="10dip" android:layout_alignParentBottom="true" /> </RelativeLayout> - Restart the project, you should be able to see a map centered at Southern California
- Open ‘addLayer.java’ and add following variables into the ‘AddLayer’ class
private MapView map = null; String dynamicMapURL = "http://sampleserver1.arcgisonline.com/ArcGIS/rest/services/Specialty/ESRI_StateCityHighway_USA/MapServer"; int usaLayerId;
Variable ‘map’ references ‘MapView’ instance. ‘dynamicMapURL’ points to the the dynamic map service you are going to add. ‘usaLayerId’ is the layer id of the dynamic layer you are going to create.
- Now insert following code into ‘onCreate()’ method
public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); this.map = (MapView) findViewById(R.id.map); ArcGISDynamicMapServiceLayer dynamicLayer = new ArcGISDynamicMapServiceLayer( this, this.dynamicMapURL); this.map.addLayer(dynamicLayer); this.usaLayerId = 1234; dynamicLayer.setId(this.usaLayerId); }In ‘setContentView’ method, the content is set to ‘main.xml’, which is a tile map representation configured in Step 2. Then ‘map’ reference is captured using ‘findViewById’ method. To add a layer to map, construct the ‘ArcGISDynamicMapServiceLayer’ using context and service URL, then call ‘map.addLayer(LayerView<?> layer)’ method. The newly added layer also has a layer id set to it for later retrieval.
You can also use Eclipse plug-in to add a layer quickly. The eclipse plug-in would do the same as Step 2 does.
Layer visibility
Hide and show the layer is the basic operation of the map. Through a single tap, you could toggle the visibility of USA layer you just added.
- Insert the following code into 'onCreate' method
this.map.setOnSingleTapListener(new OnSingleTapListener() { private static final long serialVersionUID = 1L; public void onSingleTap(float x, float y) { if (AddLayer.this.map.isLoaded()) { LayerView<?> layer = AddLayer.this.map.getLayerById(AddLayer.this.usaLayerId); if (layer.getVisibility() == ViewGroup.VISIBLE) { layer.setVisibility(ViewGroup.INVISIBLE); } else { layer.setVisibility(ViewGroup.VISIBLE); } } } });Implement ‘OnSingleTapListener()’ and set it to ‘MapView.setOnSingleTapListener’. Since ‘LayerView’ is a ‘View’ and ‘MapView’ is a ‘ViewGroup’, you can use the Android inherent method--’View.setVisibility()’ to toggle the layer visibility. In the ‘onSingleTap’ method, if the map is loaded, find USA layer using layer id saved in 'usaLayerId' variable, then set the layer’s visibility alternatively.
Spatial reference, initial extent and current extent
Layers in same MapView might come with different spatial references by default. It is important to render all the layers with same spatial reference. The ‘MapView’ takes the first added layer’s spatial reference as its spatial reference. If you configure a map layout file, the top-most layer inside map view would be 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 different spatial reference and ‘MapView’ would do the automatic projection.
Initial extent defines the visible area that you expect to see when map first loads while current extent describes the current visible area. A map has current extent at any time, while some time map loads without initial extent.
You can set an arbitrary initial extent to ‘MapView’ in your map layout file, but when you call the ‘getExtent’ on ‘MapView’ to get current extent, most probably you get an extent which is different from the initial extent. ‘MapView’ would try its best to render the map area defined in ‘initExtent’ to fit device screen.But since the aspect ratio of your initial extent might not be same as the aspect ratio of device screen, the map would re-aspect the extent for you.