Introduction
Modern applications increasingly are becoming location sensitive. If you open any mobile Web site or applicable, you can invariably notice that you are asked to provide permission to allow access to your location. Many of these applications rely on mapping technologies to provide you information with this context being sensitive.
In this article, we will learn what is needed to get started building mapping mobile applications on Android platform with Google Maps.
The Basics
To develop Android applications, knowledge of building Android applications is very helpful. If you do not have Android experience but have experience in Java, it would work. If you do not have Java experience, it is recommended to get some familiarity with Java as a prerequisite.
Prerequisites
To build Android applications, you will need the latest version of Java SDK and Android Studio, a Visual Studio equivalent, to build Android applications.
You can download the latest version of Android Studio from http://developer.android.com/sdk/index.html.
When you visit that page, you should have a link to download Android Studio. Installation of Android Studio is straight forward. However, installation will not begin if you do not have JDK installed on your machine.
You can get the latest version of JDK from http://www.oracle.com/technetwork/java/javase/downloads/index.html.
At the time of writing the article, the hardware requirements for Android Studio were as follows for Windows machines.
- Microsoft® Windows® 8/7/Vista/2003 (32- or 64-bit)
- 400 MB hard disk space
- At least 1 GB for Android SDK, emulator system images, and caches
- 1280 x 800 minimum screen resolution
- Java Development Kit (JDK) 7
- Optional for accelerated emulator: Intel® processor with support for Intel® VT-x, Intel® EM64T (Intel® 64), and Execute Disable (XD) Bit functionality
Installing the Google Play Services Library
Once you have installed Android Studio, open SDK manager from Tools-> Android -> SDK manager menu open.
Figure 1: Installing the Google Play Services library
Once the SDK manager opens, select “Google Play Services,” “Google USB driver,” and click “Install xx packages.” You will need to agree to any prompts about license agreements before you can finish the installation.
Figure 2: Accepting the license agreements
Once you have done the above, the rest of the steps are conducted inside our application.
Open Android Studio and create an app (the name of my app is “com.example.vipulp.myapplication”). Next, we will add Google Play Services as an Android Library project and reference them.
Your Android Studio after you have created the default app will look as follows:
Figure 3: The default Android Studio app
Now, double-click AndroidManifest.xml in the “Project” window under the “app” > manifests folder.
Figure 4: Selecting the AndroidManifest.xml file
We need to add a declaration within the <application> element:
<meta-data android_name="com.google.android.gms.version" android_value="@integer/google_play_services_version" />
Next, we need to get the Google Maps API key. The Maps API key is based on your application’s digital certificate, known as its SHA-1 fingerprint. To get the key for the “debug” application, we will need to use the Debug certificate in our development environment. To get the key for “release” application, make sure you use the Release certificate.
We can get the SHA-1 information for our application by executing the following command.
"C:Program FilesJavajdk1.7.0_75binkeytool.exe" -list -v -keystore ~/.android/debug.keystore -alias androiddebugkey -storepass android -keypass android
This will output your key:
Alias name: androiddebugkey Creation date: Mar 21, 2015 Entry type: PrivateKeyEntry Certificate chain length: 1 Certificate[1]: Owner: CN=Android Debug, O=Android, C=US Issuer: CN=Android Debug, O=Android, C=US Serial number: 58c0e596 Valid from: Sat Mar 21 19:18:06 PDT 2015 until: Mon Mar 13 19:18:06 PDT 2045 Certificate fingerprints: MD5: 2E:2E:44:AB:93:84:6B:E5:1F:05:5E: 03:5E:16:52:A1 SHA1: CA:74:BC:C7:C2:64:13:38:94:C6:81: AD:55:3B:D5:79:5A:3F:09:F9 SHA256: 05:C9:5E:77:DF:BE:61:17:6D:7D:78:4C: 9C:56:90:74:B4:DC:07:61:7E: B3:9B:BA: 6E:FB:FB:C2:48:B4:A4:C7 Signature algorithm name: SHA256withRSA Version: 3 Extensions: #1: ObjectId: 2.5.29.14 Criticality=false SubjectKeyIdentifier KeyIdentifier [ 0000: 88 58 FA B1 5E 47 EE B0 8B FF 25 D4 D2 C0 2B 6B .X..^G....%...+k 0010: 69 6B 73 B0 iks. ] ]
To get the Google Maps Api key, we need to register your app with Google Maps Android API 2 service. To register, you need the name of your application as well as the SHA-1 key listed above.
Go to https://console.developers.google.com and create a new project.
Figure 5: Creating a new project
Once the project is created, you will be navigated to the project dashboard. On the left bar, click “API & auth” followed by “APIs.” You will see all the APIs for that project.
Figure 6: Viewing the APIs
We now find the Google Maps Android v2 API and enable it.
Figure 7: Enabling the Google Maps Android v2 API
Click Off to “On” it. This will enable Google Maps Android API access.
Next, click “Credentials” under “APIs & Auth.”
Figure 8: Clicking “Credentials”
Click “Create new Key” under “Public API access.”
Figure 9: Creating a new key
Select Android Key.
You will be prompted to a screen that needs your SHA1 key as well as the application name in a specific format (SHA1Key;ApplicationName). Enter them and click Create.
Figure 10: Entering the application name
Your Android Maps API Key will be generated.
Figure 11: Generating your Android Maps API Key
We will now copy this APIKEY in the ApplicationManifest.xml file.
Create another node under <application> and enter your key as shown next:
<meta-data android_name="com.google.android.geo.API_KEY" android_value="API_KEY"/>
For my case, my ApplicationManifest.xml looked as under (changed highlighted):
<?xml version="1.0" encoding="utf-8"?> <manifest xmlns_android= "http://schemas.android.com/apk/res/android" package="com.example.vipulp.myapplication" > <application android_allowBackup="true" android_icon="@mipmap/ic_launcher" android_label="@string/app_name" android_theme="@style/AppTheme" > <activity android_name=".MainActivity" android_label="@string/app_name" > <intent-filter> <action android_name= "android.intent.action.MAIN" /> <category android_name= "android.intent.category.LAUNCHER" /> </intent-filter> </activity> <meta-data android:name="com.google.android.gms.version" android:value="@integer/google_play_services_version" /> <meta-data android:name="com.google.android.geo.API_KEY" android:value="AIzaSyB-az_wxYh2YgmT0lsd590H8To2aO_XQ4Q"/> </application> </manifest>
Next, we go to Project Settings via File -> Project Structure and add the Google Play Library Services as a reference.
Figure 12: Adding the Google Play Library Services as a reference
Click the + sign and select Library Dependency.
Figure 13: Clicking the + sign
Figure 14: Selecting Library Dependency
Click OK and you should now be able to build the project.
Next, we need to declare a few permissions.
Open ApplicationManifest.xml and provide the following permissions which are at least needed to any mapping application (you might need more).
<uses-permission android_name= "android.permission.INTERNET"/> <uses-permission android_name= "android.permission.ACCESS_NETWORK_STATE"/> <uses-permission android_name= "android.permission.WRITE_EXTERNAL_STORAGE"/> <uses-permission android_name= "android.permission.ACCESS_COARSE_LOCATION"/> <uses-permission android_name= "android.permission.FINE_LOCATION"/>
Because Google Maps depend on OpenGL, we also need to declare that:
<uses-feature android_glEsVersion="0x00020000" android_required="true"/>
Your application manifest should look as follows:
<?xml version="1.0" encoding="utf-8"?> <manifest xmlns_android= "http://schemas.android.com/apk/res/android" package="com.example.vipulp.myapplication" > <uses-permission android:name="android.permission.INTERNET"/> <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"/> <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/> <uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION"/> <uses-permission android:name="android.permission.FINE_LOCATION"/> <uses-feature android:glEsVersion="0x00020000" android:required="true"/> <application android_allowBackup="true" android_icon="@mipmap/ic_launcher" android_label="@string/app_name" android_theme="@style/AppTheme" > <activity android_name=".MainActivity" android_label="@string/app_name" > <intent-filter> <action android_name="android.intent.action.MAIN" /> <category android_name= "android.intent.category.LAUNCHER" /> </intent-filter> </activity> <meta-data android:name="com.google.android.gms.version" android:value="@integer/google_play_services_version" /> <meta-data android:name="com.google.android.geo.API_KEY" android:value="AIzaSyB-az_wxYh2YgmT0lsd590H8To2aO_XQ4Q"/> </application> </manifest>
Our application is now ready to be a mapping application.
In the next article, we will learn how to add a map to an Android Application and use it.
Summary
In this article, we learned how to get started with the development tools and setup needed to build mobile Android mapping applications. I hope you have found this information useful.
About the Author
Vipul Patel is a technology geek based in Seattle. He can be reached at vipul.patel@hotmail.com. You can visit his LinkedIn profile at https://www.linkedin.com/pub/vipul-patel/6/675/508.