Google map api v2 on Android.(part 1)
Steps to get map working on Android.
1. Download the Google Play services.
Goto Windows. Goto Android Sdk Manager. Choose Google play services under extras.
If not installed install the package.
2. Copy the google-play services_lib library project to your workspace. The library project can be found under the following path.
<android-sdk-folder>/extras/google/google_play_services/libproject/google-play-services_lib
library project .
3. Import the library project to your eclipse
Click File > Import, select Android > Existing Android Code into Workspace, and browse the workspace import the library project. You can check if it is library project. Right click on the library project. Goto properties. Click Android on the left panel. You will see Is Library checked.
4. Create your Android project like normally you would do for other projects.
5. You need to refer the google-play services_lib library project in your android project.
Right click on your android project. Goto properties. Choose Android on the left panel. Click on Add and browse the library project. Select the same. Click ok and apply.
6. Obtain API key
A) Locate your debug keystore file. The file name is
debug.keystore
, and is created the first time you build your project. By default, it is stored in the same directory as your Android Virtual Device (AVD) files:- OS X and Linux:
~/.android/
- Windows Vista and Windows 7:
C:\Users\your_user_name\.android\
If you are using Eclipse with ADT, and you're not sure where your debug keystore is located, you can select Windows > Prefs > Android > Build to check the full path, which you can then paste into a file explorer to locate the directory containing the keystore.
B) If your using linux or mac os, open terminal and use the following
keytool -list -v -keystore ~/.android/debug.keystore -alias androiddebugkey -storepass android -keypass android
Example for linux:
keytool -list -v -keystore /home/raghu/.android/debug.keystore -alias androiddebugkey -storepass android -keypass android
If you are using Windows or Vista use the following in our command prompt
If you are using Windows or Vista use the following in our command prompt
keytool -list -v -keystore "C:\Users\your_username\.android\debug.keystore" -alias androiddebugkey -storepass android -keypass android.
Note: keytool can be found under jdk bin folder. jdk1.7.0_07/bin/keytool
When you execute the above commands you should see the below output
Alias name: androiddebugkey
Creation date: Jan 01, 2013
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: 4aa9b300
Valid from: Mon Jan 01 08:04:04 UTC 2013 until: Mon Jan 01 18:04:04 PST 2033
Certificate fingerprints:
MD5: AE:9F:95:D0:A6:86:89:BC:A8:70:BA:34:FF:6A:AC:F9
SHA1: BB:0D:AC:74:D3:21:E1:43:07:71:9B:62:90:AF:A1:66:6E:44:5D:75
Signature algorithm name: SHA1withRSA
Version: 3
The line that begins
SHA1
contains the certificate's SHA-1 fingerprint. The fingerprint is the sequence of 20 two-digit hexadecimal numbers.
SHA1: BB:0D:AC:74:D3:21:E1:43:07:71:9B:62:90:AF:A1:66:6E:44:5D:75.
7 .Open the browser and open the following url. (Rememeber to login into google account)
You should see a window as below
Click on the create project. On the left you can see API Project. You can click the drop down list and rename it to whatever you like. Click on the services. Scroll Down the list and look for Google Maps API V2 and Google Maps Android ApI v2. Enable the same.
Click on API Access on the left panel. Click on create new Android key
Add the fingerprint along with your android project package name.
BB:0D:AC:74:D3:21:E1:43:67:71:9B:62:91:AF:A1:66:6E:44:5D:75;com.example.android.mapexample
Note: You package name is seperated by a semicolon. Click On Create.
You should the above window. Copy the API key for later use.
Adding the API Key to your application
The final step is to add the API key to your application. It goes in your application's manifest, contained in the file
AndroidManifest.xml
. From there, the Maps API reads the key value and passes it to the Google Maps server, which then confirms that you have access to Google Maps data.
To add the key to your application:
- In
AndroidManifest.xml
, add the following element as a child of the<application>
element, by inserting it just before the closing tag</application>
:<meta-data android:name="com.google.android.maps.v2.API_KEY" android:value="your_api_key"/>
substituting your API key for your_api_key. This element sets the keycom.google.android.maps.v2.API_KEY
to the value your_api_key and makes the API key visible to anyMapFragment
in your application. - Add the following elements to your manifest. Replace
com.example.mapdemo
with the package name of your application<permission android:name="com.example.mapdemo.permission.MAPS_RECEIVE" android:protectionLevel="signature"/> <uses-permission android:name="com.example.mapdemo.permission.MAPS_RECEIVE"/>
3. Add the following permissions in the Manifest file
<uses-permission android:name="com.example.mapdemo.permission.MAPS_RECEIVE"/>
<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="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.ACCESS_COARSE_LOCATION"/>
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION"/>
<uses-permission android:name="com.example.mapdemo.permission.MAPS_RECEIVE"/>
In the above permission you need to replace com.example.mapdemo with your project package name.
Because version 2 of the Google Maps Android API requires OpenGL ES version 2, you must add a
<uses-feature>
element as a child of the <manifest>
element in AndroidManifest.xml
:<uses-feature
android:glEsVersion="0x00020000"
android:required="true"/>
Save the manifest file.
The last few steps
Add the following to your activity_main.xml file
Run the project on your device and you should see the Google map on your screen<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"xmlns:tools="http://schemas.android.com/tools"android:layout_width="match_parent"android:layout_height="match_parent"android:gravity="center"tools:context=".MainActivity" ><TextViewandroid:id="@+id/textView1"android:layout_width="wrap_content"android:layout_height="wrap_content"android:layout_alignParentTop="true"android:layout_centerHorizontal="true"android:text="My Map" /><fragmentandroid:id="@+id/map"android:layout_below="@+id/textView1"android:layout_width="match_parent"android:layout_height="fill_parent"android:name="com.google.android.gms.maps.MapFragment"/></RelativeLayout>
MainActivity
package com.example.mapdemo; import android.app.Activity; import android.os.Bundle; public class MainActivity extends Activity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); } }
No comments:
Post a Comment