Click here to Skip to main content
15,868,016 members
Articles / Mobile Apps / Android

Android google map api v2 setup

Rate me:
Please Sign up or sign in to vote.
4.86/5 (4 votes)
3 Jul 2013CPOL2 min read 131K   8.5K   23   30
This article shows how to add google maps api v2 to in android projects.

Introduction 

    As you know, after 18th of March 2013, Android Google Api V1 key requests are no more avaible. For those who want to create a map activity, need to get Android Google Api V2 key. So, in this article I will show the steps to get Android Google Api V2 key . 

Background

 For reader, knowing the Eclipse environment , Android sdk and Google Api's will be helpful.  

Using the code   

These are the steps needed to be followed: 

  • Create new android application ( Compile with Google Api  )     
  • Import Google Play Services Lib     
  • Get your SHA1 fingerprint     
  • Get your mapKey by using your SHA1 key     
  • Modify the AndroidManifest.xml  

and thats all, you are ready to go...  

 Create New Android Application  

Image 1

 

 Import Google Play Services Lib  

  1. Find your google play services lib  which under the path 
 ....\android-sdk\adt-bundle-windows-x86_64-20130522\adt-bundle-windows-x86_64-20130522\sdk\extras\google and move it to your workspace.   

   2. Import this liblary to your Eclipse 

File\Import\Existing Android Code Into Workspace 

Image 2

 

  3. Add google play service lib to your project by right clicking on your project  properties\android and you will see little button on the buttom-right "add". Just add the google play service lib and click ok.


Image 3

 

 Get your SHA1 fingerprint  

Go to: 

Window\Preferences   Android\Build      

 find your SHA1 fingerprint and copy that.

Image 4

 

Go to Google Apis Console Window.  

Image 5

 

  • Create new project. 

Image 6

  • Activate the option "Google Maps Android API V2" 
 
Image 7

  • Click "Create New Android Key" 
 

Image 8

  • Enter your SHA1 fingerprint followed by '  ; ' and your application package name.
 

Image 9

 

Modify the AndroidManifest.xml  

 1- Add the following code right before the <application> tag. 



    <permission
        android:name="com.example.osman.permission.MAPS_RECEIVE"
        android:protectionLevel="signature"/>
<uses-permission android:name="com.example.osman.permission.MAPS_RECEIVE"/>
 
<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"/>
 
<uses-feature
        android:glEsVersion="0x00020000"
        android:required="true"/>  
 

 2- Add this right before the </application> tag 



  <meta-data
android:name="com.google.android.maps.v2.API_KEY"
android:value="YOUR_KEY"/>
  

3-Add this to your main_layout.xml

 
 <span style="color: black; font-family: Consolas, 'Courier New', Courier, mono; font-size: 9pt; white-space: pre;"> </span><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"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    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>
 

And your MainActivity will be like this:  

import android.os.Bundle;
import android.app.Activity;
import android.support.v4.app.FragmentActivity;
import android.view.Menu;
 
public class MainActivity extends FragmentActivity {
 
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);
	}
 

} 

 

  • To use marker on your map, add these codes in to constructor of your MainActivity...,
 
      GoogleMap googleMap;
        googleMap = ((SupportMapFragment)(getSupportFragmentManager().findFragmentById(R.id.map))).getMap();
        LatLng latLng = new LatLng(-33.796923, 150.922433);
        googleMap.setMapType(GoogleMap.MAP_TYPE_NORMAL);
        googleMap.addMarker(new MarkerOptions()
                .position(latLng)
                .title("My Spot")
                .snippet("This is my spot!")
                .icon(BitmapDescriptorFactory.defaultMarker(BitmapDescriptorFactory.HUE_AZURE)));
        googleMap.getUiSettings().setCompassEnabled(true);
        googleMap.getUiSettings().setZoomControlsEnabled(true);
        googleMap.animateCamera(CameraUpdateFactory.newLatLngZoom(latLng, 10));  

 

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



Comments and Discussions

 
QuestionGoogle map Pin
Member 1109545122-Apr-15 6:56
Member 1109545122-Apr-15 6:56 
GeneralIt Works Fine Pin
Sid_Joshi25-Mar-15 20:43
professionalSid_Joshi25-Mar-15 20:43 
QuestionMap shows but not my location Pin
Member 114277287-Feb-15 4:53
Member 114277287-Feb-15 4:53 
Questionapp give unfortunately stopeed Pin
Member 824171626-Oct-14 8:42
Member 824171626-Oct-14 8:42 
Questiongoogle map Pin
anamicaa15-May-14 0:02
anamicaa15-May-14 0:02 
QuestionAdd button Pin
Member 1070470027-Mar-14 13:39
Member 1070470027-Mar-14 13:39 
Questiongoogle map v2 Pin
Rye Salvador25-Mar-14 20:27
Rye Salvador25-Mar-14 20:27 
QuestionThe import com.google.android.gms.maps cannot be resolved Pin
LakshmiNarayan.G9-Feb-14 20:59
LakshmiNarayan.G9-Feb-14 20:59 
AnswerRe: The import com.google.android.gms.maps cannot be resolved Pin
EngrBS20-Mar-14 3:09
professionalEngrBS20-Mar-14 3:09 
GeneralRe: The import com.google.android.gms.maps cannot be resolved Pin
LakshmiNarayan.G24-Mar-14 19:45
LakshmiNarayan.G24-Mar-14 19:45 
QuestionI created new project but again this same error occur pls help me sir Pin
Member 102551852-Jan-14 11:45
Member 102551852-Jan-14 11:45 
QuestionApplication is force closing... please help me sir Pin
Member 102551852-Jan-14 11:40
Member 102551852-Jan-14 11:40 
Questionproblem Pin
Walaa Al-Showa3-Nov-13 12:47
Walaa Al-Showa3-Nov-13 12:47 
QuestionIt works fine Pin
Aliking1229-Oct-13 12:15
Aliking1229-Oct-13 12:15 
Questionafter all the above process, failing with Google Brifge services Pin
Hari Babu Suraneni27-Oct-13 23:14
Hari Babu Suraneni27-Oct-13 23:14 
AnswerRe: after all the above process, failing with Google Brifge services Pin
Safak Tarazan3-Nov-13 1:27
Safak Tarazan3-Nov-13 1:27 
AnswerRe: after all the above process, failing with Google Brifge services Pin
Truong Nguyen Quang17-Feb-14 23:04
Truong Nguyen Quang17-Feb-14 23:04 
Questiongoogle map is not showing Pin
Member 1009381113-Sep-13 0:14
Member 1009381113-Sep-13 0:14 
AnswerRe: google map is not showing Pin
Safak Tarazan13-Sep-13 0:17
Safak Tarazan13-Sep-13 0:17 
QuestionNull Pointer Exception Pin
ashish vijay pandey2-Aug-13 2:05
ashish vijay pandey2-Aug-13 2:05 
AnswerRe: Null Pointer Exception Pin
Safak Tarazan3-Aug-13 23:19
Safak Tarazan3-Aug-13 23:19 
GeneralRe: Null Pointer Exception Pin
ashish vijay pandey5-Aug-13 1:12
ashish vijay pandey5-Aug-13 1:12 
GeneralRe: Null Pointer Exception Pin
Safak Tarazan6-Aug-13 1:39
Safak Tarazan6-Aug-13 1:39 
GeneralRe: Null Pointer Exception Pin
ashish vijay pandey6-Aug-13 23:04
ashish vijay pandey6-Aug-13 23:04 
QuestionUnable yo view Map Pin
Member 1009797131-Jul-13 11:52
Member 1009797131-Jul-13 11:52 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.