Friday 1 March 2013

Get Start with Google MapKey in Android

In Order to Obtain Google MapKey in Android application 



Here is the simple tutorial to work with mapview in android.

Step to be followed:


In order to use google maps in your android application. you need to get map key.

Open your command prompt by typing cmd in your run. Start ⇒ Run ⇒ cmd  (In Windows PC)

 If your installed jdk version is 1.6 or below then you can get the MD5 fingerprint using  the code below:


C:\Program Files\Java\jdk1.6.0\bin>keytool.exe -list -alias androiddebugkey -keystore   "C:\Users \janmejoylayek\.android\debug.keystore" -storepass android -keypass android

Else you will get only (SHA1) fingerprint.

 

get key


For 1.7 or above jdk versions you can go for this code.

 

C:\Program Files\Java\jdk1.7.0\bin>keytool -list -alias androiddebugkey -v -keystore "C:\Users\janmejoylayek\.android\debug.keystore" -storepass android -keypass android.


get all fingerprints

By using the above code you can get both fingerprints.

Now to get map key using the following fingerprints go to Google Maps Android v1 API Key Signup and get your map key.

generate api


  If you will enter the correct fingerprint then you will receive the code like below..

get api

After getting the code assign the code in your api key file:

<?xml version="1.0" encoding="utf-8"?>
<com.google.android.maps.MapView
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/mapView"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:clickable="true"
    android:apiKey="08eG45cVt7Aelu9h9opbPzrcrD5JPlPtCQH8oyA"
/>

How to get start with Android google map api v2 ?


The Maps APIs for Android have been updated in the latest version of the Google Play Services library and have been aroused to Android devices with the Play Store.

Steps to follow:

 To use Google Maps API you need an API Key:

  1. Go to the Google APIs Console
  2. In Services enable ‘Google Maps Android API v2’ and agree to the terms
  3. In the left navigation bar, click API Access.
  4. Click Create New Android Key
  5. In the resulting dialog box, enter the SHA-1 fingerprint, then a semicolon(;), then your application's package name. For example:
  6. 4C:D6:CB:18:A5:73:AC:11:04:2D:77:AB:46:D8:09:E0:1C:99:DF:A0;com.example.androidmapview

Click Ok then you will receive something like this..





After receiving api key come to the programming Part. 


Create a new project.

     File -> New -> Android Application Project 

     Select Window > Android SDK Manager. 
     

             Scroll to the bottom of the package list and select Extras > Google Play services. The   Google Play services SDK is downloaded to your computer and installed in your Android SDK environment at <android-sdk-folder>/extras/google/google_play_services/ 


extras

selection
  
 Add the Google Play Services project into your Eclipse workspace.  

             Click File -> Import..., select Android -> Existing Android Code into WorkspaceBrowse to and select <android-sdk-folder>/extras/google/google_play_services/libproject/google-play-services_lib


library

         To add the dependency to Google Play Services into your project

         Project -> Properties -> Android -> Library, Add -> google-play-services_lib. 


Xml Layout:

<?xml version="1.0" encoding="utf-8"?>
<fragment xmlns:android="http://schemas.android.com/apk/res/android"
  xmlns:map="http://schemas.android.com/apk/res-auto"
  android:id="@+id/map"
  android:layout_width="match_parent"
  android:layout_height="match_parent"
 class="com.google.android.gms.maps.SupportMapFragment"/>
 
Java Code:

package com.example.androidmapview;

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

import com.google.android.gms.maps.SupportMapFragment;

public class MainActivity extends FragmentActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        SupportMapFragment fragment = new SupportMapFragment();
        getSupportFragmentManager().beginTransaction()
                .add(android.R.id.content, fragment).commit();
    }
}

                                                   
Manifest File:

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

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

    <permission
        android:name="com.example.androidmapview.permission.MAPS_RECEIVE"
        android:protectionLevel="signature" />

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

    <application
        android:allowBackup="true"
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name"
        android:theme="@style/AppTheme" >
        <activity
            android:name="com.example.androidmapview.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="AIzaSyCkpPuGvz04vogPV71j4TLu5ZNg_X9WXuw"></meta-data>
    </application>

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


Issues:

  

  Sometime you will face this error:

 


android.content.ActivityNotFoundException: No Activity found to handle Intent { act=android.intent.action.VIEW dat=http://play.google.com/store/apps/details?id=com.google.android.gms flg=0x80000 pkg=com.android.vending }



     if you will run in Android 4.1.2


  Error:


android.content.ActivityNotFoundException: No Activity found to handle Intent { act=android.intent.action.VIEW dat=market://details?id=com.google.android.gms flg=0x80000 pkg=com.android.vending }




Actually these error will arise only when you are running your app in emulator,when you will run your app in device ,it will ask you to update Google play after that the map will be visible.












2 comments:

Anonymous said...

nice

Anonymous said...

@thanks..nice tutorial