day 13: google maps android api v2: part 1

22
Google Maps API V2 Part 1 Ahsanul Karim [email protected] http://droidtraining.wordpress.com Android Application Development

Upload: ahsanul-karim

Post on 03-Sep-2014

8.919 views

Category:

Technology


2 download

DESCRIPTION

 

TRANSCRIPT

Page 1: Day 13: Google Maps Android API v2: Part 1

Google Maps API V2 Part 1

Ahsanul [email protected]

http://droidtraining.wordpress.com

Android Application Development

Page 2: Day 13: Google Maps Android API v2: Part 1

● Download and configure the Google Play services SDK. The Google Maps Android API is distributed as part of this SDK.

● Obtain an API key. To do this, you will need to register a project in the Google APIs Console, and get a signing certificate for your app.

● Specify settings in the Application Manifest.● Add a map to a new or existing Android project.● Publish your application

Steps Google Maps API V2

Page 3: Day 13: Google Maps Android API v2: Part 1

First need to install the Google Play services SDK using SDK Manager

1. Launch the SDK Manager

2. Select Extras > Google Play services, and install it

Google Maps API V2 Google Play Services [1]

Page 4: Day 13: Google Maps Android API v2: Part 1

The Google Play services SDK is saved in your Android SDK environment at <android-sdk-folder>/extras/google/google_play_services/

Google Maps API V2 Google Play Services [2]

Page 5: Day 13: Google Maps Android API v2: Part 1

3. import the library project into your workspace. Click File > Import, select Android > Existing Android Code into Workspace, and browse to the copy of the library project [libproject] to import it.

Google Maps API V2 Google Play Services [3]

Page 6: Day 13: Google Maps Android API v2: Part 1

Select and Import project.

Google Maps API V2 Google Play Services [4]

Page 7: Day 13: Google Maps Android API v2: Part 1

Project is imported as Library as seen in Project Properties

Google Maps API V2 Google Play Services [5]

Page 8: Day 13: Google Maps Android API v2: Part 1

4. Create a new project to integrate Maps

Google Maps API V2 Google Play Services [6]

Page 9: Day 13: Google Maps Android API v2: Part 1

5. Go to project properties of Google maps V2 Demo and reference the google-play-services_lib project.

Google Maps API V2 Google Play Services [7]

Page 10: Day 13: Google Maps Android API v2: Part 1

Now we are going to prepare the AndroidManifest.xml file of Google Maps V2 Demo project:

1. In AndroidManifest.xml, add the following element as a child of the <application> element, by inserting it just before the closing tag</application>

Google Maps API V2 Android Manifest [1]

Specifying Map API_KEY in metadata makes it available to any MapFragment used in the project. We'll create our own API_KEY a bit later.

Page 11: Day 13: Google Maps Android API v2: Part 1

2. Add the following elements to your manifest. Replace com.smartapps.googlemapsv2.demo with the package name of your application.

Google Maps API V2 Android Manifest [2]

3. Save AndroidManifest.xml and rebuild your application.

Page 12: Day 13: Google Maps Android API v2: Part 1

4. Add following permissions in AndroidManifest.xml

Google Maps API V2 Android Manifest [3]

5. 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

Page 13: Day 13: Google Maps Android API v2: Part 1

Google Maps API V2 Obtaining Maps API KEY [1] 1. To access the Google Maps servers with the Maps API, we have to add a Maps API key to our application.

2. The key is free, we can use it with any of our applications that call the Maps API, and it supports an unlimited number of users.

3. We'll obtain a Maps API key from the Google APIs Console by providing your application's signing certificate and its package name. So there will be separate Maps API key for debug keystore and release keystore.

4. Once we have the key, we add it to our application by adding an element to AndroidManifest.xml.

5. Recommended practice is to sign each of our applications with a different certificate and get a different key for each one.

Page 14: Day 13: Google Maps Android API v2: Part 1

Google Maps API V2 Obtaining Maps API KEY [2] Steps for obtaining an API key :

1. Retrieve information about the application's certificate.

2. Register a project in the Google APIs Console and add the Maps API as a service for the project.

3. Once we have a project set up, we can request one or more keys.

4. Finally, we can add the key to our application and begin development.

These steps will be described in next slides...

Page 15: Day 13: Google Maps Android API v2: Part 1

Google Maps API V2Step 1: Certificate Info & SHA1 Fingerprint

a. Locate Keystore: Go to Preferences-> Android-> Build

Obtaining Maps API KEY [3]

By default: Path to debug.keystoreOS X and Linux: ~/.android/Windows Vista and Windows 7: C:\Users\your_user_name\.android\

Page 16: Day 13: Google Maps Android API v2: Part 1

Google Maps API V2Step 1: Certificate Info & SHA1 Fingerprint

b. Get SHA1 Fingerprint:

Obtaining Maps API KEY [4]

Open Terminal/Cmd Prompt and write following command to get SHA1 Fingerprint:

In OSX/Linux:

keytool -list -v -keystore ~/.android/debug.keystore -alias androiddebugkey -storepass android -keypass android

In Windows Vista/Windows 7:

keytool -list -v -keystore "C:\Users\your_user_name\.android\debug.keystore" -alias androiddebugkey -storepass android -keypass android

1. Be careful about the path of debug.keystore in command.2. In windows, if environment variable is not set, you may need to write the command after going to java/bin folder with keytool.exe.

Page 17: Day 13: Google Maps Android API v2: Part 1

Google Maps API V2Step 1: Certificate Info & SHA1 Fingerprint

b. Get SHA1 Fingerprint: Output in Terminal

Obtaining Maps API KEY [4]

Page 18: Day 13: Google Maps Android API v2: Part 1

Google Maps API V2Step 2: Creating an API Project

1. In browser, navigate to Google API Concole [https://code.google.com/apis/console/] and Login with Gmail ID.

2. Create New Project and Enable Google Maps Android API V2 Service from services list.

Obtaining Maps API KEY [5]

Page 19: Day 13: Google Maps Android API v2: Part 1

Google Maps API V2Step 2: Request for API Key

It's possible to register more than one key per project.

To get the key:

1. In the left navigation bar, click API Access.

2. In the resulting page, click Create New Android Key....

3. In the resulting dialog, enter the SHA-1 fingerprint, then a semicolon, then our application's package name. For example: in our case

Obtaining Maps API KEY [6]

1E:36:29:E1:F4:DD:FB:5C:AF:8B:99:BA:FC:E7:A5:03:51:14:19:B65;com.smartapps.googlemapsv2.demo

4. API Console responds with a 40-character key:

AIzaSyCQL_pZ_EA85MzBEeNDxNCtlQnt3-V_OGU

Page 20: Day 13: Google Maps Android API v2: Part 1

Google Maps API V2Step 2: Request for API Key

Obtaining Maps API KEY [6]

Page 21: Day 13: Google Maps Android API v2: Part 1

Google Maps API V2Step 1: Add a MapFragment to activity_main.xml

Add a Map [1]

Step 2: MainActivity.java

Page 22: Day 13: Google Maps Android API v2: Part 1

Google Maps API V2Step 3: Build and Run

Add a Map [2]

1. Now we should see the map in a real device2. Map cannot be shown in emulator as long as Google Play Store application is not installed in emulator