Today we are going to add a map to an Android app using the Google Maps V2, we also use Android Studio instead of Eclipse. The steps to acquire the key are here
https://developers.google.com/maps/documentation/android/start .
Steps
- Get an API key from Google
- Create a new project
- Add a dependency for section to the gradle.build file
- Add permissions to the AndroidManifest.xml file
- Enable OpenGL ES in the AndroidManifest.xml file
- Add meta data section to the AndroidManifest.xmlfile with the API keys
- Add an xml fragment layout file with the declaration for the Google Maps
- Use java code to change and manipulate the map
- Add markers to the map
Get the Google Maps V2 Android API Key
To get the key for the Google Maps V2 navigate to https://code.google.com/apis/console
Create a new project if you already don’t have one. Navigate to the “Services” section and enable the “Google Maps Android API v2“. Under the project navigate to “API Access” and “Create New Android Key”.
A new window opens with the command to run and retrieve the android debug key on your development machine.
NB : Each android application is signed with a key, by default during development a debug key is used to sign the apps. The key is located under the folder “.android” . In Windows its located under “C:\.android”, in Linux and Mac OS X, the key is located under the home users directory, navigate to your home directory and cd to .android.
Run the keytool command to retrieve the SHA1 key, in my case its
keytool -list -v -keystore debug.keystore
Copy the SHA1 hash together with the package name for the Android application. E.g if the package name for the app was za.peruzal.maps, the we will have
45:B5:E4:6F:36:AD:0A:98:94:B4:02:66:2B:12:17:F2:56:26:A0:E0;za.peruzal.maps
Create a New Project
Start Android Studio and follow the steps to create a new project.
Add a Dependency to The Android Studio build.gradle file. Use the Google APIs as the target for the the project.
Open the project build.gradle file
Add the following line under dependencies :
dependencies {
compile 'com.android.support:support-v4:18.0.0'
compile 'com.google.android.gms:play-services:3.1.36'
}
Click the sync button and wait a little whilst Android Studio downloads the Google Play Services jar files and link them to the project.
Android Permissions to the AndroidManifest.xml file
<uses-permission android:name="android.permission.INTERNET"/> <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"/> <uses-permission android:name="com.google.android.providers.gsf.permission.READ_GSERVICES"/> <!-- The following two permissions are not required to use Google Maps Android API v2, but are recommended. -->
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION"/> <uses-permission android:name="android.permission.ACCESS_FINE_LOCATION"/>
Enable OpenGL ES version 2 in the AndroidManifest.xml file
<uses-feature
android:glEsVersion="0x00020000"
android:required="true"/>
Add the API key to the AndroidManifest.xml file
<meta-data
android:name="com.google.android.maps.v2.API_KEY"
android:value="API_KEY"/>
Get the key from the Google API console.
Add the Map Fragment
Create a new xml layout file
Add the following within the file
<fragment xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:map="http://schemas.android.com/apk/res-auto
android:id="@+id/mapView"
android:layout_width="match_parent"
android:layout_height="match_parent"
map:cameraZoom="14"
map:cameraTilt="45"
android:name="com.google.android.gms.maps.MapFragment"/>
Manipulate the map through Java code
At this point we can run the app on the phone. The emulator does not come with Google Play Services, or it might need to be updated.
In our next post we will use Java code to add markers to the Google Map. Until next time
转载地址:http://peruzal.wordpress.com/2013/08/27/using-google-maps-v2-for-android-with-android-studio/