Home > DeveloperSection > Articles > Google Maps API v2 in Android

Google Maps API v2 in Android


Android Android 
Ratings:
0 Comment(s)
 1310  View(s)
Rate this:

Google Maps Android API v2


The Android platform provides easy and tight integration between Android applications and Google Maps. The well-established Google Maps APIis used under the hood in order to bring the power of Google Maps to your Android applications.

This tutorial is to learn how to show the current location on map in an Android application using Google Maps API. 

For use Google Map API you must be follow some steps-:

1.      Install Google Play Service form SDK Manager

2.      Import google-play-services_lib library from C:\Eclipse\adt-bundle-windows-x86_64-20131030\adt-bundle-windows-x86_64-20131030\sdk\extras\google\google_play_services\libproject

3.      Create a Google Map Key from Google Console.

Before create Google Map key you have need creating the SHA-1 for your signature key.

·         On Menu click Window then click Preferences

·         Click Android then select Build

 

Now open Google API console

Crate a new API Key

Add your SHA1 fingerprints and package name

 

After press Create button will be generate a key

 

 

4.       Add permission in Manifest file

 

<?xml version="1.0" encoding="utf-8"?>

<manifest xmlns:android="http://schemas.android.com/apk/res/android"

    package="com.example.androidgoogleapi"

    android:versionCode="1"

    android:versionName="1.0" >

 

    <uses-sdk

        android:minSdkVersion="12"

        android:targetSdkVersion="18" />

 

    <permission

   android:name="com.example.androidgoogleapi.permission.MAPS_RECEIVE"

        android:protectionLevel="signature" />

 

    <uses-permission android:name="com.example.androidgoogleapi. permission.MAPS_RECEIVE" />

    <uses-permission android:name="android.permission.INTERNET" />

<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"/>

<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"/>

 

    <!-- Required OpenGL ES 2.0. for Maps V2 -->

    <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" >

        <meta-data

            android:name="com.google.android.maps.v2.API_KEY"

            android:value="AlzaSyCrrkL5KnBrwzHFr2svng-hFMrqPX70wIO" />

        <meta-data

            android:name="com.google.android.gms.version"

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

 

        <activity

            android:name="com.example.androidgoogleapi.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>

    </application>

 

</manifest>

 

5.       My activity_main.xml file

<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="match_parent"

        android:layout_height="match_parent"

        class="com.google.android.gms.maps.SupportMapFragment" />

 

</RelativeLayout>

6.      Add following code in MainActivity.class

import android.app.Activity;

import android.os.Bundle;

import android.view.Menu;

 

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

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

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

import com.google.android.gms.maps.model.BitmapDescriptorFactory;

import com.google.android.gms.maps.model.LatLng;

import com.google.android.gms.maps.model.Marker;

import com.google.android.gms.maps.model.MarkerOptions;

 

public class MainActivity extends Activity {

  static final LatLng HAMBURG = new LatLng(53.558, 9.927);

  static final LatLng KIEL = new LatLng(53.551, 9.993);

  private GoogleMap map;

 

  @Override

  protected void onCreate(Bundle savedInstanceState) {

    super.onCreate(savedInstanceState);

    setContentView(R.layout.activity_main);

    map = ((MapFragment) getFragmentManager().findFragmentById(R.id.map))

        .getMap();

    Marker hamburg = map.addMarker(new MarkerOptions().position(HAMBURG)

        .title("Hamburg"));

    Marker kiel = map.addMarker(new MarkerOptions()

        .position(KIEL)

        .title("Kiel")

        .snippet("Kiel is cool")

        .icon(BitmapDescriptorFactory

            .fromResource(R.drawable.ic_launcher)));

 

    // Move the camera instantly to hamburg with a zoom of 15.

    map.moveCamera(CameraUpdateFactory.newLatLngZoom(HAMBURG, 15));

 

    // Zoom in, animating the camera.

    map.animateCamera(CameraUpdateFactory.zoomTo(10), 2000, null);

  }

 

  @Override

  public boolean onCreateOptionsMenu(Menu menu) {

    getMenuInflater().inflate(R.menu.activity_main, menu);

    return true;

  }

}

 

Now run your application

 

 


Don't want to miss updates? Please click the below button!

Follow MindStick