Google Android Map v2-Step by Step

29 03 2013

Google Android Map v2-Step by Step

  1. Create a new android project with default settings.
  2. Import “google-play-services_lib”.
    1. Select File-->Import

    2. Select Android-->Existing Android Code into Workspace
    3. Browse and select <bundle root>\sdk\extras\google\google_play_services\libproject

       

    4. Select “google-play-services_lib” and finish.

  3. Install google play services.
    1. Open sdk manager.
    2. Select google play services and install.
  4. Add the google play services library to your project.
    1. Right-click on your project and select properties.
    2. Select the library, and press OK.
  5. Find your SHA1.
    1. Open command prompt and go to your jdk/bin folder. Such as: C:\Program Files\Java\jdk1.7.0_02\bin
      • Start-->run, cmd
      • cd C:\Program Files\Java\jdk1.7.0_02\bin
    2. find your debug.keystore path by checking windows-->preferences-->android-->build
    3. Run the following command to get your sha1: keytool -v -list -keystore "<debug.keystore path>" and press “enter” when a password asked.
    4. Then copy and keep SHA1 for later use. For my case it is (3C:64:03:7B:0E:F7:E5:71:0D:10:9D:62:4C:03:8C:F2:4E:AD:B8:94)
  6. Get the key for android apps.
    1. You need to have a gmail account.
    2. Create Google APIs project from https://code.google.com/apis/console adress.
    3. From services, Turn “Google Maps Android API v2” to ON.
    4. From API Access, create new Android Key.

    5. Paste SHA1 and your package name <SHA1>;<package name> for example at my case it is (3C:64:03:7B:0E:F7:E5:71:0D:10:9D:62:4C:03:8C:F2:4E:AD:B8:94;info.tekguc.umut.googlemapsmapsandroidv2)
    6. Copy generated API for later use, for my case it is AIzaSyDciL7-T3BphxGv2q-A77vNrcyJQ_sTrgI

       
  7. Create Android Virtual Device(AVD).
    1. Open Android Virtual Device Manager
    2. Create new AVD with the following configurations:
    3. Start created AVD.
  8. Installing google service packages; gms and vending
    1. Be sure that emulator started
    2. Copy attached com.google.android.gms-19032013.apk and com.google.android.vending-19032013.apk files to <bundel root>\sdk\platform-tools
    3. Open command from and go to <bundel root>\sdk\platform-tools
    4. then run command adb install com.google.android.gms-19032013.apk

       
    5. then run command adb install com.android.vending-19032013.apk
    6. if it says already exits then run commands
      • adb uninstall com.android.vending
      • adb uninstall com.google.android.gms
  9. Modify AndroidManifest.xml
    1. Double-click on androidmanifest.xml
    2. Add necessary permissions and glEs feature to androidmanifest.xml
       <permission
              android:name="com.example.androidmapsv2.permission.MAPS_RECEIVE"
              android:protectionLevel="signature"></permission>
          <uses-permission
              android:name="com.example.androidmapsv2.permission.MAPS_RECEIVE"/>
          <uses-permission
              android:name="com.google.android.providers.gsf.permission.READ_GSERVICES"/>
          <uses-permission
              android:name="android.permission.INTERNET"/>
          <uses-permission
              android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
          <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"/>
    3. Create metadata for map api in application
      <meta-data  android:name="com.google.android.maps.v2.API_KEY"
      android:value="AIzaSyDciL7-T3BphxGv2q-A77vNrcyJQ_sTrgI"/>
    4. Your final androidmanifest.xml file will lool like as follows:
       <?xml version="1.0" encoding="utf-8"?>
      <manifest xmlns:android="http://schemas.android.com/apk/res/android"
          package="info.tekguc.umut.googlemapsmapsandroidv2"
          android:versionCode="1"
          android:versionName="1.0" >
          <uses-sdk
              android:minSdkVersion="14"
              android:targetSdkVersion="17" />
              <permission
              android:name="com.example.androidmapsv2.permission.MAPS_RECEIVE"
              android:protectionLevel="signature"></permission>
          <uses-permission
              android:name="com.example.androidmapsv2.permission.MAPS_RECEIVE"/>
          <uses-permission
              android:name="com.google.android.providers.gsf.permission.READ_GSERVICES"/>
          <uses-permission
              android:name="android.permission.INTERNET"/>
          <uses-permission
              android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
          <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"/>
          
          <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="AIzaSyDciL7-T3BphxGv2q-A77vNrcyJQ_sTrgI"/>
              <activity
                  android:name="info.tekguc.umut.googlemapsmapsandroidv2.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>
  10. Modify activity_main.xml
    1. Double click on activity_main.xml

    2. Add to map fragment
       <fragment
        android:id="@+id/map"        
        android:layout_width="match_parent"        
        android:layout_height="match_parent"        
        class="com.google.android.gms.maps.MapFragment"/>
    3. Your final activity_main.xml file will lool like as follows:
       <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" >
      <TextView        
      android:layout_width="wrap_content"        
      android:layout_height="wrap_content"        
      android:text="@string/hello_world" />
      <fragment
              android:id="@+id/map"     
              android:layout_width="match_parent"        
              android:layout_height="match_parent"        
      class="com.google.android.gms.maps.MapFragment"/>
      </RelativeLayout>
  11. Modify MainActivity.Java
    1. Double-click mainactivity.java

    2. Add imports.
      import com.google.android.gms.maps.GoogleMap;
      import com.google.android.gms.maps.MapFragment;
      import com.google.android.gms.maps.model.LatLng;
      import com.google.android.gms.maps.model.Marker;
      import com.google.android.gms.maps.model.MarkerOptions;
    3. Add GoogleMap object
      private GoogleMap mMap;
    4. Add the following code to onCreate() à this code is not necessary to run the map but it gives you ideas how to use it.
       mMap = ((MapFragment) getFragmentManager().findFragmentById(R.id.map)).getMap();
                  mMap.setMapType(GoogleMap.MAP_TYPE_SATELLITE);
                  final LatLng CIU = new LatLng(35.21843892856462, 33.41662287712097);
                  Marker ciu = mMap.addMarker(new MarkerOptions()
                                            .position(CIU).title("My Office"));
    5. Your final mainactivity.java file will lool like as follows:
       package info.tekguc.umut.googlemapsmapsandroidv2; 
      import android.os.Bundle; 
      import android.app.Activity; 
      import android.view.Menu; 
      import com.google.android.gms.maps.GoogleMap; 
      import com.google.android.gms.maps.MapFragment; 
      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 {     
      private GoogleMap mMap;         
      @Override     
      protected void onCreate(Bundle savedInstanceState) {         
      super.onCreate(savedInstanceState);         
      setContentView(R.layout.activity_main);             
      mMap = ((MapFragment) getFragmentManager().findFragmentById(R.id.map)).getMap();         
      mMap.setMapType(GoogleMap.MAP_TYPE_SATELLITE);         
      final LatLng CIU = new LatLng(35.21843892856462, 33.41662287712097);         
      Marker ciu = mMap.addMarker(new MarkerOptions()                                   
      .position(CIU).title("My Office"));     
      }     
      @Override     
      public boolean onCreateOptionsMenu(Menu menu) {         
      // Inflate the menu; this adds items to the action bar if it is present.         
      getMenuInflater().inflate(R.menu.main, menu);         
      return true;     } } 
  12. Run the application
    1. Right-click on your application and select run as àandroid application.

    2. You wil see a screen as follows:

    3. See the attached project.