Integrating Google maps API v2 with Android application

Google Maps Android API v2 The Google Maps Android API v2 allows us integrate interactive, feature-rich Google maps to our Android application. Advantage of API v2 over API v1
  • The Maps API now uses vector tiles. Their data representation is smaller, so maps appear in your apps faster, and use less bandwidth.
  • Caching is improved, so users will typically see a map without empty areas.
  • Maps are now 3D. By moving the user’s viewpoint, you can show the map with perspective.
Pre-Requisites for integrating Google Maps API v2 into my Android application
  • The API is distributed as part of the Google Play services SDK, which can be downloaded with the Android SDK Manager. To use the Google Maps Android API v2 in your application, we first need to install the Google Play services SDK.Installing Google Play Services In Eclipse, choose Window > Android SDK Manager. In the list of packages that appears scroll down to Extras folder and expand it. Select the Google Play services checkbox and install the package.
  • Next, to use Google Maps we need to create a valid Google Maps API key. The key is free, we get this key via Google APIS Console. We have to provide our application signature key and the application package name in order to get the Google Maps API key
What is Application signature key? The Maps API key is based on a short form of your application’s digital certificate, known as its SHA-1 fingerprint. The fingerprint is a unique text string generated from the commonly-used SHA-1 hashing algorithm. Because the fingerprint is itself unique, Google Maps uses it as a way to identify our application. Generating SHA1 Key Find .android directory on your device : This directory is under your home directory. Windows Vista/7: windows installation drive: (C,DE, whatever)\Users\\.android OS X and Linux: ~/.android Find debug Keystore File called debug.keystore lies in .android directory To create the SHA-1 for your debug keystore you use the keytool command from your JDK installation pointing to the debug.keystore file. Command:
keytool -list -v -alias androiddebugkey \-keystore <path_to_debug_keystore>debug.keystore \-storepass android -keypass android
Now follow following documentation to generate API key https://developers.google.com/maps/documentation/android/start#obtaining_an_api_key
  • Now you have installed Google play services library and generated an API key for you app lest now move to actual process to integrate a google map
Steps to Integrate Google maps to an Android application
  • Create a new android project in Eclipse, with package name that you have registered with Google console to get API key
  • Import Google play Services library to your workspace Browse to /extras/google/google_play_services/libproject/google-play-services_lib and select google-play-services_lib
  • Add this Project to your android app as -> Properties -> Android -> Library, Add -> google-play-services_lib into your project
  • Code in MainActivity should look like this
  • public class MainActivity extends Activity {
     @Override
     protected void onCreate(Bundle savedInstanceState) {
     super.onCreate(savedInstanceState);
     setContentView(R.layout.main);
     }
  • Maps are now encapsulated in the MapFragment class, an extension of Android’s Fragment class.Now you can add a map as a piece of a larger Activity.With a MapFragment object, you can show a map by itself on smaller screens, such as mobile phones, or as a part of a more complex UI on larger-screen devices, such as tablets.
What is MapFragment? It is a Map component in an app. This fragment is the simplest way to place a map in an application. It’s a wrapper around a view of a map to automatically handle the necessary life cycle needs. Being a fragment, this component can be added to an activity’s layout file simply with the XML below.

<?xml version="1.0" encoding="utf-8"?>
 <fragment xmlns:android="http://schemas.android.com/apk/res/android"
 android:id="@+id/map"
 android:layout_width="match_parent"
 android:layout_height="match_parent" android:name="com.google.android.gms.maps.MapFragment"/>

so we need to replace content ofres/layout/activity_main.xml with code above
  • Add the following tag into your AndroidManifest.xml just before the closing tag From here, the Maps API reads the key value and passes it to the Google Maps server, which then confirms that you have access to Google Maps data.
    <meta-data android:name="com.google.android.maps.v2.API_KEY"android:value="<strong>your_api_key</strong>"/>
  • Your application now also needs the following permissions in the AndroidManifest.xml
    <permission android:name="your_package_name.permission.MAPS_RECEIVE" android:protectionLevel="signature"/>
     <uses-permission android:name="your_package_name.permission.MAPS_RECEIVE"/>
     <uses-permission android:name="android.permission.INTERNET"/>
  • Maps v2 uses OpenGl so the following uses-feature is also required <uses-feature android:glEsVersion=”0x00020000″ android:required=”true”/>
  • Thats it !! Build and run your application.
  • You should see a map. If you don’t see a map, please confirm if you have missed out any of the configurations.