Thursday, 7 March 2013

Google Maps Android API v2

NOTE: Version 1 of the Google Maps Android API has been officially deprecated as of December 3rd, 2012 . This means that from March 3rd , 2013 you will no longer will able to request an API key for this  version. No new features will be added to Google Maps API v1 . However , apps using v1 will continue work on devices. Existing and new developers are encouraged to use Google Maps Android API v2.


NOTE: You cannot run your application using Google Play API(Google Maps Android API V2) on Android emulator, Because your emulator doesn't support Google Play services. So you need to check your application in Android mobile phone which supports google play services.

watch video tutorial here:click here

download this project here :GoogleMapsAndroidAPIV2 


Here i'm giving the procedure how to setup Google Map Android API v2 SDK and integrating in Application.

ScreenShot:


Step1: Download and configure the Google Play Services SDK. In Eclipse open go to window menu
          and run Android SDK Manager and under Extras select Google Play Services package and
         download it.

Step 2: Now create your application where you want use Google maps api v2.

Step 3: Import Google Play Services library project in eclipse

  • Under package explorer right click choose import option and under android choose importing existing project into workspace as  follows.


  • In next window choose browse option and open your ADT Bundle has been located or your Android SDK and inside open extras folder and open google folder and open google_play _service folder and choose libproject folder and click ok button.



  • After clicking ok button under Import Project window select google play service lib project and click finish button.


  • After clicking finish button you will observe in under package explorer google_play_services_lib project has been added.


Step4: Adding googleplay_services_lib to our project:
  • Right click on project and choose properties option and select android .
  • Under library choose add option and add googleplay_service_library to our project as follows:

Next


Next again perform same action.
  • Right click on project and choose properties option and select android and if you got following error under library then follow the bellow procedure.

Note: you will get this problem because of lengthy path.

open adt bundle -->open sdk folder-->open extras folder-->open google folder-->open googleplayservices folder-->copy lib project and paste in your current workspace folder and from there import libproject after importing add library to the project now you wont get any error.


Step5: Finding SHA1 fingerprint key

Now two ways are avialable to findout SHA1 key 
Procedure1:
  • Go to window menu choose preferences under preferences choose android under that choose build and copy SHA1 fingerprint key as follows:
    


or

Procedure 2:
  • If your using windows operation system follow below procedure
  1. Under package explorer right click and choose properties.
  2. Under Android choose build and there you can see default debug keystore copy that keystore value.
  3. After goto MyComputer and open the drive where your operating system has been located and under that drive open program files folder there you will see java folder open that and open jre folder on bin folder click SHIFT+Right click choose open command window here and type following command.
  4. keytool -list --alias androiddebugkey -keystore "paste your default debug keystore here" -storepass android -keypass android
  5. After executing above command you will get SHA1 certificate fingerpint key copy that and go to internet and in Google type google api console and there sign in using your google account.
  • If your using Mac or Linux operating system procedure is very easy just type following command in Terminal
  • keytool -list -v -keystore ~/.android/debug.keystore -alias androiddebugkey -storepass android -keypass android

After executing above command you will get SHA1 certificate fingerpint key copy that and go to internet and in Google type google api console and there sign in using your google account.

Step 5: Sign in google api console website as follows

Next


Step 6: After signing in follow the steps below

  • -->Under google api's console select create project option as follows

Next
-->provide project name and select create button.


Next
-->After selecting create button you will get following window.
-->In following window from left side navigation bar select APIs & auth option as follows


Next
-->In following window scroll down and activate Google Maps Android API v2 Services by clicking on OFF button to ON

Next


Next
-->Select Credentials option from left side navigation bar under APIs option


Next
-->After selecting Credentials option next you will get following window 
-->From following window select CREATE NEW KEY option

  • In following window enter your SHA1 cerficate fingerprint key (go to step5)with semicolon and after enter your application package name as follows.
  • And click create button.

Next
-->After selecting create button you will get following window from following window you can copy the map API key and insert into AndroidManifest.xml.



Step 7: Go to eclipse open your application and open AndroidManifest.xml file and insert following code .

AndroidManifest.xml:

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

    <uses-sdk
        android:minSdkVersion="8"
        android:targetSdkVersion="17" />
    
    <uses-feature
        android:glEsVersion="0x00020000"
        android:required="true" />

    <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" />
    <!--
     The following two permissions are not required to use
     Google Maps Android API v2, but are recommended.
    -->
    <uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
    <uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />

    <permission
        android:name="your application package name.permission.MAPS_RECEIVE"
        android:protectionLevel="signature" />

    <uses-permission android:name="your application package name.permission.MAPS_RECEIVE" />

    <application
        android:allowBackup="true"
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name"
        android:theme="@style/AppTheme" >
        <activity
            android:name="com.ram.googlemapsv2.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.gms.version"

            android:value="@integer/google_play_services_version" />

         <meta-data
            android:name="com.google.android.maps.v2.API_KEY"
            android:value="paste your api key here" />
    </application>

</manifest> 

Step 8: Insert following code in your activity_main.xml

activity_main.xml:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    xmlns:map="http://schemas.android.com/apk/res-auto"
    android:id="@+id/LinearLayout1"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical" >

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

Step 9: Insert following code in your MainActivity.java

MainActivity.java:
package com.ram.googlemapsv2;

import android.annotation.SuppressLint;
import android.app.Activity;
import android.os.Bundle;

import com.google.android.gms.maps.GoogleMap;
import com.google.android.gms.maps.MapFragment;

public class MainActivity extends Activity {
GoogleMap map;
@SuppressLint("NewApi")
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);

map = ((MapFragment) getFragmentManager().findFragmentById(R.id.map))
.getMap();
// map.setMapType(GoogleMap.MAP_TYPE_HYBRID);
// map.setMapType(GoogleMap.MAP_TYPE_NONE);
map.setMapType(GoogleMap.MAP_TYPE_NORMAL);
// map.setMapType(GoogleMap.MAP_TYPE_SATELLITE);
// map.setMapType(GoogleMap.MAP_TYPE_TERRAIN);
}
}