Google V2 maps for android

Leave a Comment
In this tutorial am going to show you  how to integrate google maps v2 on your android app easily.
To accomplish this you  have to obtain an API key from google which will enable you to access this service.
This key is obtained for the google console found  https://code.google.com/apis/console/
First you  begin by  creating a project in the google console if its your first time to use it. After creating the project follow the following steps to generate your API KEY

On the google console click create new Android Key
This API key is created using SHA1 fingerprint and the package name of your project that we will create in eclipse.
SHA1 fingerprint can be obtained using the cmd or directly from the eclipse menu  by  clicking on window>preferences>Android>build



To create the API key  obtain the SHA1 fingerprint and then the package name of your project and paste it in the the field provided in the google console  as shown below and then click create


Now you  will be having your API key  under the package name of your project. For this project i used the package com.mojas.googlemapsforandoid



The next step will be creating the project in eclipse and  before creating the project you  must ensure that google play services is installed in your sdk. 



If google play services is installed then you  are set to go butif its not installed then ensure that you  install the library to your sdk inorder to continue with this tutorial.
Import the google play services library to your eclipse workspace by  clicking the File>Import



Select google play services library as the project to import. This library is usually found in the sdk folder under extras>google>google play services> libproject


Make sure that you  check the Copy  project to workspace as shown below

With that now you have googleplay services successfully imported to your workspace.
Now Lets create the project click File>New>Android Application project
Give your application a name. I named my project   GoogleMapsForAndroid. Select compile with Google APIs for me i used API 19.



Now, add the google play services library to   your project by clicking on build path>Configure build path>Android.  Click Add and select the library


Now edit the main Xml to look like this 

activity_main.xml

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity" >

  
    <fragment
        android:id="@+id/map"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        class="com.google.android.gms.maps.SupportMapFragment" />


  

</RelativeLayout>


MainActivity.java

package com.mojas.googlemapsforandoid;

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

import android.support.v7.app.ActionBarActivity;
import android.support.v7.app.ActionBar;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentActivity;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.os.Build;

public class MainActivity extends FragmentActivity {
private GoogleMap mMap;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        SupportMapFragment fragment = ( SupportMapFragment) getSupportFragmentManager().findFragmentById(R.id.map);
        // Getting Google Map
        mMap = fragment.getMap();

}
   

   
}



Android Manifest.xml

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.mojas.googlemapsforandoid"
    android:versionCode="1"
    android:versionName="1.0" >
   
     <!-- Replace the package name below with your project pacakge name -->
    <permission
        android:name="com.mojas.googlemapsforandoid.permission.MAPS_RECEIVE"
        android:protectionLevel="signature" />

    <uses-permission android:name="com.mojas.googlemapsforandoid.permission.MAPS_RECEIVE" />

   

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

     <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
    <uses-permission android:name="android.permission.INTERNET" />
    <uses-permission android:name="com.google.android.providers.gsf.permission.READ_GSERVICES" />
    <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />


    <!-- Required to show current location -->
    <uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
    <uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />

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

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

        <!-- Replace the API KEY with your API key obtained form google console -->
         <meta-data
            android:name="com.google.android.maps.v2.API_KEY"
            android:value="API KEY" />
        <meta-data
            android:name="com.google.android.gms.version"
            android:value="@integer/google_play_services_version" />
    </application>

</manifest>

Run the project using a real device and you  should see a map displayed as shown below.


 



Click  here  to Download the  project


0 comments:

Post a Comment