Tabs

Wednesday 13 August 2014

Google Map Integration With Android Application.


This example will show you how to implement Google map in your application. The steps show all the requirement for using map in your application.Lets see Practical example. 


There are some requirements those are importing required library, generating SHA1 fingerprint and configuring maps in Google console. Just follow the steps.

1. Download Google Play Services : 

Google has introduce Maps V2 API as a part of Google Play Services SDK. So we need to download Google play services from SDK manger.  We can open SDK manager either from Eclipse or from android SDK folder.

Open Eclipse ⇒ Windows ⇒ Android SDK Manager and check whether you have already downloaded Google Play Services or not under Extras section. If not select play services and install the package.


Downloading Google Play Services SDK
Downloading Google Play Services SDK

Another way to download Google play service is directly download from website. From Here you can Download it. 

2. Importing Google Play Services into Eclipse :

After downloading play services we need to import it to Eclipse which will be used as a library for our maps project. The Steps are as below.

1. In Eclipse go to File ⇒ Import ⇒ Android ⇒ Existing Android Code Into Workspace.

2. Click on Browse and select Google Play Services project from your android sdk folder or from your downloaded location. You can locate play services library project from
android-sdk-windows\extras\google\google_play_services\libproject\google-play-services_lib


3. Importantly while importing check Copy projects into workspace option as shown in the below image.

Importing Google Play Services
Importing Google Play Services

3. Getting Google Maps API key :

1. we need to generate SHA-1 fingerprint using java keytool. Open your CMD and execute the following command to generate SHA-1 fingerprint.
keytool -list -v -keystore "%USERPROFILE%\.android\debug.keystore" -alias androiddebugkey -storepass android -keypass android
In the output you can see SHA 1 finger print.

Android Google Maps SHA-1 Fingerprint
Android Google Maps SHA-1 Fingerprint
2. Now open Google API Console
3. If you are new on Google API Console than create New Project. After creation in project dashboard go to Enable Api.
4. From there select Google Maps Android API v2 and make it ON.

Android Google Console Maps API
Android Google Console Maps API
5. Now Under Aps&Auth section select Credentials. From there select Create New Key. Select Android key.
6. It will popup a window asking for your SHA1 and package name.Enter your SHA 1 and your android project package name separated by semicolon ; and click on create.

Note down the generated API key which is useful in project. 
Google Console API Key
Google Console API Key

4. Creating new Project :

After completing required configuration, It’s time to start our project.

1. In Eclipse create a new project by going to File ⇒ New ⇒ Android Application Project and fill required details. I kept my project name as Google Maps V2.

2. Now we need to use Google Play Services project as a library to use project. So right click on project and select properties. In the properties window on left side select Android. On the right you can see a Add button under library section. Click it and select Google play services project which we imported previously.

Linking Google Play Services To Project
Linking Google Play Services To Project


Linking Google Play Services To Project
Linking Google Play Services To Project with Green Signal.
3. Add the Map Key in the manifest file. Open AndroidManifest.xml file and add the following code before tag. Replace the android:value with your map key which you got from google console.
<!-- Goolge Maps API Key -->
<meta-data
     android:name="com.google.android.maps.v2.API_KEY"
     android:value="AIzaSyBZMlkOv4sj-M5JO9p6wksdax4TEjDVLgo" />
4. Google maps needs following permissions and features.

ACCESS_NETWORK_STATE – To check network state whether data can be downloaded or not
INTERNET – To check internet connection status
WRITE_EXTERNAL_STORAGE – To write to external storage as google maps store map data in external storage
ACCESS_COARSE_LOCATION – To determine user’s location using WiFi and mobile cell data
ACCESS_FINE_LOCATION – To determine user’s location using GPS
OpenGL ES V2 – Required for Google Maps V2.

Finally my AndroidManifest.xml file looks like this (Replace the package name with your project package)


Main Activity : 
public class MainActivity extends Activity 
{
//Google Map
private GoogleMap googlemap;
@Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
try 
{
initilizemap();

catch (Exception e) 
{
// TODO: handle exception
}
}

@SuppressLint("NewApi")
private void initilizemap() 
{
// TODO Auto-generated method stub
if(googlemap == null)
{
googlemap = ((MapFragment) getFragmentManager().findFragmentById(R.id.FRG_MAP)).getMap();
googlemap.setMyLocationEnabled(true);
googlemap.getMyLocation();
Toast.makeText(getApplicationContext(), "Your Location" + googlemap.getMyLocation().toString(),Toast.LENGTH_LONG).show();
}

if(googlemap == null)
{
Toast.makeText(getBaseContext(), "Sorry .. unable to create map..",Toast.LENGTH_LONG).show();
}
}

@Override
protected void onResume() 
{
// TODO Auto-generated method stub
super.onResume();
initilizemap();
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}

Run your project and congratulations if you see a map displaying on your device.


Map_Location
Map_Location
Map_Location

Map_Location
Map_Location