Saturday, March 29, 2014

Android:Displaying Google Map

Displaying Google Map in android project using Google Map API V2:

If you want to use Google Map API V2 to show Google Map in your android app, then you must follow the following steps:
  1. Open SDK manager and install Google Play Services from SDK manager and import that google-play-services_lib(i.e. library project) into your workspace(File->Import->Android->Existing Android code Into Workspace->Browse the google-play-services_lib from your sdk->extra->google->google-play-services->libproject)
  2. After importing the google-play-services_lib, you must use it in your project,for this: right click on your project->properties->android(in left)->Add->choose google-play-services_lib->Apply->OK
  3. Obtain Google API key. For this follow the steps following steps:
    • Create New Project(File->New->Android Application Project)
    •  Then Goto(Window->Preferences->Android->Build) and copy the SHA1 fingerprint
    • Open Google API Console and Register your project and Get API key for your android project(https://code.google.com/apis/console and create new project if you are new and click on API Access and then Create New Android Key and Paste that copied SHA1 fingerprint then semi-colon(;) and then your project's package name(com.example.secondmap))
  4. Your layout file(main_activity.xml) must contain this code: 
 <?xml version="1.0" encoding="utf-8"?>
<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"/>
5.Your  activity file(MainActivity.java ) must contain this code:

package com.example.mapsecond;

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

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

public class MainActivity extends Activity {

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

6.Your AndroidManifest.xml file must contain this code:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.example.mapsecond"
    android:versionCode="1"
    android:versionName="1.0" >

    <uses-sdk
        android:minSdkVersion="8"
        android:targetSdkVersion="19" />
    <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"/>
   

    <application
       
        android:allowBackup="true"
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name"
        android:theme="@style/AppTheme" >
        <meta-data
        android:name="com.google.android.gms.version"
        android:value="@integer/google_play_services_version" />
       
        <activity
            android:name="com.example.mapsecond.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="Here will be your own API Key obtained from Google API console after registering your project"/>
       
    </application>

</manifest>






7.Now create an emulator where Target SDK must be Google API(level 19)(this must be installed from SDK manager)
8.Now Run your project and you can see Google Map

Note: For detail Information Click here: