14 June 2015

Create a Google Map Application in Android

You must have created the project and obtained the API key for MAP as shown in the previous tutorial. Now we will make the application code.

What we will do ?
-> Display a Google Map in your android application.

What we need ?
  • Google API key
  • Google Play Services enabled device (Mobile phone with Android v4.2 above)
  • Import libraries (Upgraded ADT with Play Services)
Step 1 : You may have created your Android Application with name MapDemo and package name com.mia.mapdemo.
mapdemo
 Click on Next continuously Then click Finish.
activityname

Step 2 : Import libraries in your Application. Click on File-> Import.
file import

Step 3 : Choose Android-> Existing Code into workspace. Then Click Next.
existcode

Step 4 : Click on Browse and go to location
adt-bundle-windows-x86-20140702\sdk\extras\google\google_play_services\libproject\google-play-services_lib
Your location might be different from mine, so please do not copy-paste the above link. Instead Browse it in your Computer. Tick the Project and click Finish as shown below.
playservices

Step 5 : Add fragment to activity_main.xml.

activity_main.xml
<?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"
        android:id="@+id/map"
        android:layout_width="match_parent"
        android:layout_height="match_parent" />
</LinearLayout>

Step 6 : Code for MainActivity.java is shown below.

MainActivity.java
package com.mia.mapdemo;

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

public class MainActivity extends FragmentActivity  {

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

Step 7 : Code for MapDemoManifest.xml is shown below.

MapDemoManifest.xml
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.mia.mapdemo"
    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"/>
    <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" >
        <activity
            android:name="com.mia.mapdemo.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="copy_your_api_key_here" />
        <meta-data android:name="com.google.android.gms.version"
             android:value="@integer/google_play_services_version" />
    </application>

</manifest>

Step 8 : Right-click on MapDemo in Project Explorer and click on Properties.
properties

Step 9 : Click on Android in left-panel, then click on Add and choose Google-play-services, then click OK.
playserviceslib
.You will see Google-play-services in the box as shown below, Also tick on Google API (4.2.2/17) then click OK.
googleapi17

Step 10 : Run your Application in device with Google Play Services.
mapop

Explanation of the code :

Explanation from activity_main.xml :

Explanation from MainActivity.java :
  • Here we just import the class library FragmentActivity and extend it to MainActivity. You can extend your code further to make manipulations in the Map.
import android.support.v4.app.FragmentActivity;
public class MainActivity extends FragmentActivity  {

Explanation from MapDemoManifest.xml :
  • We include all the permissions required like INTERNET, ACCESS_COARSE_LOCATION etc. to get internet connection,location in the map etc.
 <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"/>
    <uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
    <uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
  • In meta-data you get the API KEY and connect with google-play-developer-console, which permits us to use Maps in our application.
<meta-data
            android:name="com.google.android.maps.v2.API_KEY"
            android:value="copy_your_api_key_here" />
download
Stay Tuned with Made In Android

Previous Page Next Page Home
Top