Thursday, 29 August 2013

Google maps v2 for Android - Basic start point guide

The second version of Google Maps for Android was release on December 2012 and beside the 3D maps, it facilitated a lot of works for the developers.

This basic guide try to mark a start point with all the steps that a developer should do in order to use it in Android.

1. Download the library
By default the library is not included in the SDK. To download, follow the steps below:
1.1 Click on Windows -> Android SDK Manager
1.2 Under Extras, mark the "Google Play services" and install it.

2. Import the Google Play library
Once done, it will appear as project the "extra" folder of your SDK installation folder. You must import it to Eclipse.
2.1 Click on File -> import
2.2 Select Android -> Existing Android Code Into Workspace and then click on Next
2.3 Click on Browse and go to the YOURSDKFOLDER/extras/google/google_play_services/libproject/google-play-services_lib
2.4 Click on Accept to import the project.

3. Create a new Android project
3.1 Click on File -> New -> Android Application Project
3.2 Enter the data. They could be:
- Application Name: Sample Google Maps v2
- Project Name: SampleGoogleMapsv2
- Package Name: com.jiahaoliuliu.android.samplegooglemapsv2
3.3 Click next several times and then Finish.

4. Import the Google Play library in your project
4.1 Right click on the created project and select "Properties"
4.2 On the left panel, select "Android"
4.3 On the right panel, click on Add and select "google-play-services_lib"
4.4 Click on Ok 2 times to finish

5. Get the SHA-1 key
5.1 Open the console
5.2 Write the follow command and click enter to show all the keys

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

It will show a list of keys. The unique one we are going to use is the one called SHA1. It could be something like this:

BB:0D:AC:74:D3:21:E1:43:67:71:9B:62:91:AF:A1:66:6E:44:5D:75

6. Activates the Google Maps v2 API for Android
6.2 Sign in with your Google account if you haven't done it yet.
6.3 On the left panel, click on Services
6.4 On the right panel go to the one called "Google Maps Android API v2" and enable the switch
6.5 Read carefully the contract :-) and accept it.
6.6 Once it is done, the API for Google Maps for Android has been activated

7. Get the API key
7.1 On the same screen of Google APIs console. on the left panel, click on API Access
7.2 Click on the button called "Create new Android key" at the bottom of the page.
7.3 On the popup windows, enter your SHA-1 key along with the package name of your application, separated by a semicolon. For example, this could be:

BB:0D:AC:74:D3:21:E1:43:67:71:9B:62:91:AF:A1:66:6E:44:5D:75;com.jiahaoliuliu.android.samplegooglemapsv2

A new API key will be created. It is something like this:

AIzaSyBdVl-cTICSwYKrZ95SuvNw7dbMuDt1KG0

Save it because we are going to use it later.

8. Creates the layout.
Paste the follow code in the activity_main.xml

<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"
    tools:context=".MainActivity" >

    <fragment
        android:id="@+id/map"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        class="com.google.android.gms.maps.MapFragment" />

</RelativeLayout>

9. Creates the activity.
Paste the follow code in the MainActivity.java

package com.jiahaoliuliu.android.samplegooglemapsv2

import android.os.Bundle;
import android.view.Menu;
import android.app.Activity;

public class MainActivity extends Activity {

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.activity_main, menu);
return true;
}
}

10. Modify the Manifest file
Copy and paste the follow code to the Android Manifest file.
Don't forget to copy and paste your API key from the step 7.3 to "YourAPIGoesHere".

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.jiahaoliuliu.android.samplegooglemapsv2"
android:versionCode="1"
android:versionName="1.0" >

<uses-sdk
android:minSdkVersion="8"
android:targetSdkVersion="17" />

    <permission
        android:name="com.jiahaoliuliu.android.samplegooglemapsv2.permission.MAPS_RECEIVE"
        android:protectionLevel="signature" />

    <uses-feature
        android:glEsVersion="0x00020000"
        android:required="true" />

    <uses-permission android:name="com.jiahaoliuliu.android.samplegooglemapsv2.permission.MAPS_RECEIVE" />
    <uses-permission android:name="android.permission.INTERNET" />
    <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
    <uses-permission android:name="com.google.android.providers.gsf.permission.READ_GSERVICES" />
    <uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
    <uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
    <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"/>

<application
android:allowBackup="true"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<activity
android:name="com.jiahaoliuliu.android.samplegooglemapsv2.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.maps.v2.API_KEY"
            android:value="YourAPIGoesHere" />
</application>
</manifest>

Source:

Source Code:

No comments:

Post a Comment