Friday, 10 May 2013

Android - Google Maps (Part 1) : Loading map using Google Maps API V2

A step-by-step guide on using google maps API v2 in android :

High level overview of different steps involved :

1.) Create project, add all the required libraries
2.) Generate API Key from Google APIs console
3.) And the CODE !

Step 1 : 

a.) Create an android application project in eclipse. Import google-play-services to the workspace

Note : While importing, check the 'Copy projects into workspace', as you should be referencing a copy of the library that you copied to your source tree - you should not reference the library from android-sdk directory.

b.) Set the properties of the project, enabling it to use Google APIs and library

c.) Also import android-support-v4 library, if not already present
(It can be found in extras - android - support - v4)

Step 2 :

a.) Find out debug key - SHA1 fingerprint using command given below on command prompt after navigating to jdk bin :
keytool.exe -v -list -alias androiddebugkey -keystore c:\Users\divya_motiwala\.android\debug.keystore -storepass android -keypass android
-v will retrieve SHA1 key. Replace path for debug.keystore with appropriate path.

b.) Login to google APIs console :

Enable required APIs from services tab :

c.) In API Access Tab, select "Create new Android Key" and enter SHA1 Key;packagename

And now with all the ingredients available, lets bake the cake :D The CODE !

Step 3 :

a.) Add permissions and API key in manifest file :



In application tag, add

b.) Create XML Layout


Note : Use SupportMapFragment if target < 3.0, else MapFragment can be used

c.) In Java code, 
public class MainActivity extends FragmentActivity  //Extend fragment activity

  protected void onCreate(Bundle savedInstanceState) {

And thats it! Map will be loaded on screen..

Problems that you may face and possible solutions :

Prob : extras folder in sdk does not contain google-play-services library

Sol :  install it using sdk-manager.

Prob : Properties window of project does not list Google APIs

Sol : install the same using sdk-manager.

Prob : Blank map with grids is shown

Sol :
 a.) Make sure a proper API key is used. While generating it, both above mentioned APIs are ON

b.) All libraries are referenced correctly

c.) Class is extending "FragmentActivity"

d.) And lastly, check that you have a working data connection !

Hope this helps!


No comments:

Post a Comment

Would love to hear from you. Leave a comment :)