Hello World Map
The following will guide you through the process of creating a Hello Map app using the SDK Eclipse plugin. The instructions and discussion below ultimately culminate in an application that is available as a sample (see Hello World). This topic's purpose is to help developers understand the basics of working with the ArcGIS for Android plug-ins in the Eclipse integrated development environment (IDE) to build a basic map application as well as fundamental principles to guide future application building. The discussion will concern how to add a map to your application with an initial extent and how to best address orientation changes.
- Prerequisites
- Create new ArcGIS project for Android
- Adding a map and setting an initial extent
- Executing the application
- Orientation change handling
Prerequisites
The workflows presented here assume that you have a development environment set up based on the system requirements, have the ArcGIS for Android SDK plug-in installed, and have Eclipse open with your workspace set. This topic also assumes a basic understanding of the Android platform. Throughout this topic, Android-specific terms are used with the understanding that you are familiar with the terminology of the Android platform. When possible, references are provided to Google's documentation for further reading for those who are less familiar with Android development. If some terminology is unfamiliar to you, it is strongly encouraged that you follow the supplied links and familiarize yourself with the language used in mobile development.
Create new ArcGIS Project for Android
Once you have your developer environment set you can start a new ArcGIS Project for Android in Eclipse.
- In Eclipse, select File > New > Project
- Select "ArcGIS Project for Android" and click Next.
- Fill the New Project form with the Project Name and click Next.
- In the Application info window enter a valid Package Name and click Finish.
You have now successfully created a new ArcGIS project for Android. Next, you will add a map service to the application and set up an initial extent for that map.
Adding a map and setting an initial extent
Now that you have created a new ArcGIS project for Android, the next step is to add a map to the application. The map you will add to this particular application is the worldwide street map available from ArcGIS.com. However, you will not display the street map for the entire world when the map is initialized. It is often more desirable for the application to initially display a map for a specific region instead of its entire extent—in this instance, a specific region of Southern California as opposed to the entire world. The following discussion will guide you through the process of adding a map and specifying an initial extent for the application:
- In your new project, expand the newly created HelloWorld project in the Eclipse package explorer. We will edit 2 source files for the HelloWorld project.
- HelloWorld/src/com.esri.android.samples/HelloWorldActivity.java:
- HelloWorld/res/layout/main.xml
- Open up the HelloWorld/res/layout/main.xml file by double clicking on the file name. This will open the layout xml file in graphical laytout by default. Select the main.xml tab, lower left hand corner of file view. This will open up the raw xml contents..
By default, there are two tags in main.xml. The first is Android's linear layout, which arranges its children views in a single column or a single row. To understand this layout template further, see the LinearLayout documentation provided by Google. Linear layout is just another Android view, but since its purpose is to organize children views that are added to it, it can be referred to as a parent view. For a detailed explanation of the Android view hierarchy, see the User Interface documentation. The second tag is the TextView tag, considered the child view in this example. This tag is used to add text to the application, in this case "Hello World, HelloWorld!". If you follow Android's Hello, World tutorial, this project in its current form represents Android's first basic application.
- Remove the <TextView> element and all it's attributes from the file.
/* Remove this section */ <TextView android:layout_width="fill_parent" android:layout_height="wrap_content" android:text="@string/hello" />
- Add a MapView XML Layout and it's initial extent. Add the following code snippet where you removed the <TextView> element in main.xml.
<!-- MapView layout and initial extent --> <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.MapView>
- Save main.xml.
- Open HelloWorld/src/com.esri.android.samples/HelloWorldActivity.java file by double clicking on the file name. This will open the Java source file in the Java Editor. Add the code in Bold to your HelloWorldActivity class:
public class HelloWorldActivity extends Activity { MapView map = null; /** Called when the activity is first created. */ public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); // Retrieve the map and initial extent from XML layout map = (MapView)findViewById(R.id.map); // Add dynamic layer to MapView map.addLayer(new ArcGISTiledMapServiceLayer("" + "http://services.arcgisonline.com/ArcGIS/rest/services/World_Street_Map/MapServer")); } protected void onPause() { super.onPause(); map.pause(); } protected void onResume() { super.onResume(); map.unpause(); } }
- Save HelloWorldActivity.java source file.
Your application is now ready for deployment. Before we go through the deployment steps let's take a look at some of the features the New Project wizard automated for our project:
Every Android application must have an AndroidManifest.xml (located at the root folder of every Android project in Eclipse) file as part of the Android package (APK) file, which is used to distribute and install applications onto an Android device. One important aspect of this file is the declaration of permissions that your application must have to execute on a device. The New Project wizard feature included the addition of three specific permissions to the Android manifest file. The following are descriptions of each of these permissions that are added to the manifest file:
- android.permission.INTERNET: This allows your application to open network sockets. This is very important because the ArcGIS API for Android only works in connected mode, meaning it must be allowed to open network connections to communicate with ArcGIS Server.
- android.permission.WRITE_EXTERNAL_STORAGE: This permission allows your application to write data to an external storage medium, such as a secure digital (SD) card. To optimize the performance of your applications that use the ArcGIS API, granting this permission is vital.
- android.permission.ACCESS_FINE_LOCATION: This permission allows your application to access the Global Positioning System (GPS) information from a device. Programmatically, it is possible for you to work with the LocationService class in the ArcGIS API, which supports the display of the current location on the map view. However, the reason for the permission is due to the tight integration of the LocationService class in the ArcGIS API and the Location Manager class from the Android SDK. LocationManager is what provides access to a device's location services and allows the ArcGIS API to obtain and display on a map periodic updates of a device's geographic location. By default, the Esri-provided plugins assume that this functionality is needed and includes it by default. If you do not have a need for the geographic location in your application, you can remove this permission from your Android manifest file. To do so, manually edit the AndroidManifest.xml file or use the Android Development Tools (ADT) plug-in for Eclipse and click the Permissions tab of the manifest file.
The ArcGIS for Android API uses the OpenGL ES subset specification for high performance graphics designed for embedded systems. We declare this hardware feature in the <uses-feature> declaration.
- android:glEsVersion="0x00020000": To specifiy OpenGL ES version 2.0 we set the value as "0x00020000"
The ArcGIS for Android API supports Android 2.2 Platform and above. To distinguish this in your app we include <uses-sdk> declartion in the manifest.
- minSdkVersion="8": Specifies our applications compability with Android 2.2 Platform API level 8. The Android system will prevent your users from installing your applications build with ArcGIS for Android API if the systems API level is lower than the value specified.
You have now completed the steps necessary to add a map to and set an initial extent for a new ArcGIS project for Android. For a deeper understanding of the declarative approach to user interface (UI) design, see Declaring Layout. Next, you will learn how to take this basic application and deploy to a device.
Executing the application
It is easy to run your application from within Eclipse
- Right click on the HelloWorld project in Project Explorer panel and select 'Run As > Android Application'. Be sure to have an Android device connected to the machine. We do not support running in an Emulator.
- When the app opens on your device, double click on the map to zoom in.
The Eclipse plugin automatically creates a new run configuration and launches the ArcGIS for Android application on your device. You should now see something like this:
This concludes the development of your first ArcGIS for Android API application. As stated at the beginning of this topic, this project is available to you as a sample. Learn about how to work with samples, and read more in-depth sample documentation.