Monday 11 November 2013

Creating a Google Map application using map v2 with Release Key

Steps:-


  •      Open Eclipse and create a new Android project.
    The first thing that we will handle is the import of Google Map classes. To get the Google Maps files we need to download the last version of Google Play Services via the Android SDK Manager.
  • After you downloaded the Google Play Services, restart Eclipse and
    in the Package Explorer Right-Click –> Import….
    In the opened windows choose “Existing Android Code into Workspace” and click “Next”.
    Click the “Browse…” Button and head to the location of your SDK folder. In it find the following folder: \extras\google\google_play_services\libproject\google-play-services_lib and press “OK”, check the V next to it in the window and press the “Finish” button.
  •      Now you added Google Play Services to your work space, we have to create a reference
    from our project to this library. Right-Click your project and choose “Properties” go
    to the Android section, in the lower part press the “Add…” button and add a reference
    to that library. Your result should be as in the screen shot bellow:



         
      Note: 
If you try to reference google-play-service library and you receive a red X next to this reference, what you should do is to move the .jar file to a location where it’s path will be shorter and then reference it again.

  •      Another import we have to make in order to make our application work on Operation system prior to API11 is to import the support library this can be done very easily using Eclipse: Right-Click you project and choose “Android Tools” and then choose “Add Support Library…”:




When you finish those imports you should have the following libraries (Red) in the Android Dependencies folder (Green) of your project:


  • We are now ready for some coding: First of all open the Android Manifest file: add the following permissions:

<permission android:name="your.application.package.permission.MAPS_RECEIVE" android:protectionLevel="signature"/>
<uses-permission android:name="your.application.package.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" />

      Important: 
      Replace your application package instead of the current “your.application.package” string.
  •      Next, Google Maps uses OpenGL so we have to add OpenGL support to our application
    by adding this to the Manifest file:

<uses-feature
android:glEsVersion="0x00020000"
android:required="true"/>
  •      Finally add your key to you application right before you close your “application” node
    in the Manifest file:

<meta-data
android:name="com.google.android.maps.v2.API_KEY"
android:value="Your Google Maps API V2 Key" />

  •       Now create an Activity that extends from “FragmentActivity”:

import android.os.Bundle;
import android.support.v4.app.FragmentActivity;

public class MapActivity extends FragmentActivity {

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

  •      Finally for map_layout, XML layout file that was set as a content view of the map activity write the following:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical" >

    


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

</LinearLayout>



Steps to getting the Map API Key from the Google Map:  
  •        Create a new project




  •      Select services for the project. Here we need a Google Maps Android API v2 service. Search this service in list and ON this service.



  •        Get SHA1 Fingerprint form this command
C:\Users\pavan.p>keytool -list -alias androiddebugkey -keystore default-keystore-file
-storepass android -keypass android
androiddebugkey, Jun 25, 2012, PrivateKeyEntry,
Certificate fingerprint (SHA1): 67:3C:9D:----Gen SHA1 Key----:E8
In my case:-
default-keystore-file : C:\Users\pavan.p\.android\debug.keystore
Or find it in the Eclipse Window -> Preferences -> Android -> Build -> here you will found the both SHA1 and MD5 fingerprint.

  • Go to API Access tab from the console. Here we show the one key Key for browser apps (with referrers). We want to get API Key for android then click on create New Android Key button. The opened dialog asks for SHA1 fingerprint and package name followed by semicolon.

Enter your SHA1 fingerprint and package name,
In my case input is  67:3C:9D:3B:AB:00:D0:ED:6D:94:28:B9:63:EF:E0:33:32:E7:6D:E8;sample.sample




  •        Use this key in the Manifest file:

<meta-data
android:name="com.google.android.maps.v2.API_KEY"
android:value="Your Google Maps API V2 Key" />

  • One important thing about the Android Google map v2 is , When your application is in debugging mode the map works fine but when we create .apk of the application the Google map needs the Release Key for that.  With version 2 API's you can use the same key for release and debug. In your google api's console edit your allowed android apps and on each line put your debug/release key, and then your app name. You can use multiple lines, then it will work with both keys.

  •      To export the apk, see the bottom of the manifest file with link as “Use the Export Wizard”




Follow all the instructions:-













  • When apk is generated at the same time MD5 fingerprint and SHA1 generated in the console as follow

  •      Copy the SHA1 key, go to Google API Console and Click on Edit allowed Android apps... link, add the SHA1 key in the dialog box with package name. Make sure that generated the API key and Manifest key is same.
Hope all the steps helpful to you.

Happy Reading and Happy coding.. :-)

No comments:

Post a Comment