Pages

2014/11/08

Beginning’s Introduction to Developing with Google Maps V2 for Android

This is a summary of my experience with getting Google Maps V2 API to work on my Samsung Galaxy S3 phone.


Development Environment.


Android ADT Version: 23.0.2.






















JDK version: 1.7.0_71












Eclipse Version: 4.2.1



-----------------------------------------------------------------------------------------------------------------


Building the project


  1. Create new project. For our example, the project will be namedcom.example.maptest01”


Note, this name must be the same as the one you use for applying your Google MAPs V2 API for Android.


  1. Include “google-play-services_lib”


Right click on project (com.example.maptest01) -> select Properties




Select “Android” -> “Add”























  1. Adding support library.


Right click on project (com.example.maptest01) -> select Android Tools -> select Add Support Library.



Note, this is for supporting API 11 or earlier system.


  1. Checking support libraries.























  1. Writing the codes


-----------------------------------------------------------------------------------------------------------------
MainActivity.java
-----------------------------------------------------------------------------------------------------------------


package com.example.maptest01;


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


public class MainActivity extends FragmentActivity {


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


-----------------------------------------------------------------------------------------------------------------
activity_main.xml
-----------------------------------------------------------------------------------------------------------------


<fragment xmlns:android="http://schemas.android.com/apk/res/android"
   android:id="@+id/map"
   android:layout_width="match_parent"
   android:layout_height="match_parent"
   class="com.google.android.gms.maps.MapFragment" />


-----------------------------------------------------------------------------------------------------------------
AndroidManifest.xml
-----------------------------------------------------------------------------------------------------------------


<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
   package="com.example.maptest01"
   android:versionCode="1"
   android:versionName="1.0" >


   <uses-sdk
       android:minSdkVersion="8"
       android:targetSdkVersion="16" />
   
   <uses-feature
       android:glEsVersion="0x00020000"
       android:required="true" />
   
   <permission
       android:name="com.example.maptest01.permission.MAPS_RECEIVE"
       android:protectionLevel="signature" />
   <uses-permission
     android:name="com.example.maptest01.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" />
   <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=".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="Your Google Maps API V2 Key" />
   
    <meta-data
       android:name="com.google.android.gms.version"
    android:value="@integer/google_play_services_version" />
   
   </application>


</manifest>


-----------------------------------------------------------------------------------------------------------------


  1. Upload to actual device (for this example: Samsung Galaxy S3)


Right click on project (com.example.maptest01) -> select Run As -> select Android Application.


Note, Currently, Google Maps V2 API for Android doesn’t run on Emulator. There are ways to make it runs on Emulator, please check the reference links at the end of this post for details. I haven’t made it work successfully yet.


Select the device in AVD and click OK.



The map will show up within seconds.




































References:


The info. in the following links are very helpful and contains more info. on how to apply for Google Maps V2 API Key, further development of Apps. using the API, and more.


Getting the API Key
Note, It’s very important that the path to keystore (in this example: debug.keystore) doesn’t contain any blank space. Otherwise, keytool will not function properly. If  blank space exists in the path, copy the keystore to a different directory without blank space in its path.









No comments:

Post a Comment