The Maps API & Adding a Map to your android Project


While developing Ceeq,  I had to include a Map to our application, this is how I achieved the feat.

To add a Map we first have to fulfill few requirements

1. Get the Play Services library in your project
2. Get the API key.
3. Add the permissions.
4. Add a map fragment to your activity or fragment layout
5. Install play services on device to test.
6. Run your app.

Lets get in details of each step, 

1. Get the Play Services to your project


a) Open the SDK Manager
        Eclipse > Window > SDK manager > Extras > Google Play Services  ✓ 
        Install the Google Play Services

image

b) The play service are downloaded to your sdk folder

         sdk > /extras/google/google_play_services/

c) Import the google_play_services to your project

        Eclipse > File > Import… > Android > Existing Android Code into Workspace,

image

Click Next

image

Click Browse  and browse to the sdk > /extras/google/google_play_services/ directory
and click Okay

image

Untick the samples\ and click Finish.

If all goes okay you will see google-play-services_lib in the package explorer of eclipse, now we will import it to our project.

To import it to your project right click your project and open properties
 Your_Project > Right Click > Properties or Simply press ALT + ENTER in your project to open its properties.


image

In the Android tab, in Library click on Add

image

If you have successfully imported the Google play Services to your workspace, then Select google-play-services_lib and press Okay

image

Click Apply and Click OK.

Thats all you have successfully imported Google Play Services to your project.

2.Let’s get the api key now

a) Login to your google account and open 
 https://code.google.com/apis/console/


b) Now create a new project and you will be at your dashboard as

image

c) Click on Services Tab

In the list of provided services, scroll down to Google Maps Android API v2

image

and switch it ON 

image

d) Click on API Access Tab

image


Click on Create New Android Key…

A dialog box opens up asking for the SHA1 key,

Now, open your Windows explorer and open your user folder, there you have the .android folder which has the debug.keystore file.

os_drive:\Users\User.android 

You may see this directory has a debug.keystore file

CTRL + SHIFT + Right Click and open CMD window here, or simply open a Command Window(CMD) and navigate to this directory.

Type in you command prompt

keytool -list -v -keystore debug.keystore > key.txt
it will ask for a password, type android and hit Enter.

Now, you will see a key.txt file has been generated in the os_drive:\Users\User.android directory. Open this file and Copy the SHA1 signature (i.e the code after SHA1:), and paste it

image

put a semi-colon (;) after the SHA1 code and write your package name and click Create.

You have now successfully generated your API key, which shows on your dashboard, copy the API key and put it in you manifest file as:

<meta-data
   
android:name="com.google.android.maps.v2.API_KEY"
   
android:value="API_KEY"/>

3. Changes to manifest file and adding the Permissions

<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"/>
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION"/>
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION"/>

Add the uses-feature

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

4. Adding a map fragment to your activity layout/ fragment layout

If you are supporting backward compatibility and importing an activity or fragment activity, or for projects targeting below API14
import android.support.v4.app.*
then you need to include a SupportMapFragment to your layout

<fragment xmlns:android="http://schemas.android.com/apk/res/android"
         
android:id="@+id/map"
         
android:layout_width="match_parent"
         
android:layout_height="match_parent"
         
android:name="com.google.android.gms.maps.SupportMapFragment"/>

Or else if you are not supporting backward compatibility and for projects targeting API 14+, we need to add a MapFragment to our layout

<fragment xmlns:android="http://schemas.android.com/apk/res/android"
         
android:id="@+id/map"
         
android:layout_width="match_parent"
         
android:layout_height="match_parent"
         
android:name="com.google.android.gms.maps.MapFragment"/>

You can get the map fragment in your onCreate() or method (incase you using fragments in onCreateView( ) ) as

For Support Map Fragment:

SupportFragment mapFragment = ((SupportMapFragment) this.getActivity().getSupportFragmentManager().findFragmentById(R.id.map));

For Map Fragment:

MapFragment mapFragment = ((MapFragment) this.getActivity().getSupportFragmentManager().findFragmentById(R.id.map));

5. Add play services to your emulator or device

The application is ready to test on a real device, if you have play services on it but for testing on emulator, we need google play services on it too,

So, again open the SDK Manager, Click on your relative API [mine is Android 4.3 (API 18)] and download the Google APIs. Create a new AVD with Google APIs as the platform.
Remember only Google APIs higher than 4.2.2 include play services.

You can also manually install Play Services to your virtual device via adb.

First download the files from here
com.google.android.gms_20130908.apk
com.android.vending_20130908.apk

After that create an AVD/emulator, any platform, and then install the downloaded files via adb as

adb install com.android.vending-20130716.apk 
adb install com.google.android.gms-20130716.apk 

if you are updating, or want to un install

adb uninstall com.android.vending
adb uninstall com.google.android.gms                   

So, if you now have an emulator AVD with latest Google Play Services, or you have an AVD with latest Google Play Services,
Your application is ready to run.

— Update —

For updated Play Services version 13 you need to add

<meta-data           
android:name="com.google.android.gms.version"           
android:value="@integer/google_play_services_version" />

to your manifest file so to avoid application crash.



Possible errors:

Error in Authentication this error is primary due to missing API key, missing play services, or if you have changed the API key.
Solution  
1. Remove your app, wipe data of emulator/AVD, reinstall your app. 
2. Check and cross check the permissions.
3. Recheck you have enabled the Android map services from the Google APIs Console, if not enabled, Enable it, regenerate a new key, add it to your manifest, remove the application from AVD/Emulator/real device, and run your project.


Google Play Service missing this error is due to missing play services either in your test device/emulator or in the project.
Solution
1. Install the latest updated Google Play Services to the test device.
2. Recheck you have added the play services library to your project.

Error in inflating class SupportMapFragment
Solution
1. Check you have play services added to your project.
2. Try using a MapFragment if your project is for API14 and above.

No Map shown, only Zoom buttons shown
1. Remove your app, wipe data of emulator/AVD, reinstall your app. 
2. Check and cross check the permissions.
3. Recheck you have enabled the Android map services from the Google APIs Console
4. Re check you have include API key in manifest, and you have generated it via command prompt only.

References:
https://developers.google.com/maps/documentation/android/start
www.stackoverflow.com