You are currently browsing the tag archive for the ‘Google Maps API Key’ tag.

I believe that you have gone through the first part of this article. i.e,

https://mirnauman.wordpress.com/2013/06/11/our-first-android-google-maps-api-ver-2-0-application-part-1/

In this part we will go through a detail explanation of getting the Api ver 2.0 key one baby step at a time. I ll try not to skip even the smallest step so that fresh developers finds it easy to make it to the finish line successfully. The main sections covered in this article are

  • Signing the application and why its necessary.
  • Retrieving our application’s certificate.
  • Registering our project
  • Our Api project creation.
  • Obtaining the Api key
  • Adding the Api key to our application

Signing the application and why its necessary.

The signing of the application is not something very complicated or suspicious thing. It is used to identify the author of a certain application. Their is no signing authority involved and it is allowed to use self signed certificates for android application. The private key is with the developer of the application. Without signing our application we will not be able to  get the Android Google Maps API Ver 2.0 key. So we have to do it as any cost.  We will skip a lot of boring theory about this signing application and will go through the important part but if the readers are interested in some more details they can always click on the link “Signing Application Details“. Signing can be in two modes. Signing in Debug mode and Signing in Release mode. In this article I ll only go through the Signing in Debug mode.

Signing in Debug mode

To sign our application in Debug mode first we will make sure that we have the “keytool” utility present at our JDK, JRE install location. The keytool is used to create the debug keystore and key automatically. The key is than used to sign the application automatically. The location of keytool on my system is  “C:\Program Files\Java\jre7\bin\keytool.exe”. the debug.keystore is created with predetermined names and passwords. e.g,

  • Keystore name: “debug.keystore”
  • Keystore password: “android”
  • Key alias: “androiddebugkey”
  • Key password: “android”
  • CN: “CN=Android Debug,O=Android,C=US”

Wc can change the location and name of the debug keystore but for now we will leave it as it is. One thing to make note of is that the self signed certificate of debug mode has a validity of 365 days, than it expires. Now to complete the process we have to retrieve our SHA-1 fingerprint and use it to sign our application and get the api key.

What is SHA-1 fingerprint , where and how to find it.

SHA-1 fingerprint is a unique text string generated by SHA-1 hash Algorithm and as it is unique , Google Maps uses it to  identify the application. To find it there are two ways, first use keytool to generate the SHA-1 fingerprint through command prompt. The command is explained in detail in the  link How to get Google Maps API Key but there is a simple way to find out SHA-1 fingerprint but before we get this fingerprint we need to understand that there are two type of certificates through which we can generate the SHA-1 fingerprint.

  1. The Debug Certificate
  2. The Release Certificate

The Debug certificate is created automatically by the SDK tools when the application is built without exporting it as released application. This certificate can only be used for testing and not for publishing the application as released version. The Release certificate is generated by the SDK tools when a release built of the application is done. With this certificate you can the application can be published. For testing and development we will use the Debug certificate. To display the SHA-1 fingerprint on Debug certificate go to Windows menue in Eclipse and click Preferences, than select Android from the list and click Build. We will see the following screen.

Debug keystore and SHA-1 Fingerprint

Debug keystore and SHA-1 Fingerprint

This is the easiest way to display the SHA-1 fingerprint. Note down the SHA-1 fingerprint. Keytool commands from command prompt can also be used to display the same SHA-1 fingerprint. But i guess using Eclipse to display this is alot easier.

Registering our application

We will use our gmapv2 application that we have created in Part # 1 of this series for registration process. Open a browser and navigate to the following link. Google API Console.

You will be prompted for logging In, Log In with your gmail id and password. You will be taken to the following screen.

Google API Console Main Page

Google API Console Main Page

Expand the top Left Dropdown list Where it is written API Project V2 and click Create. A dialog box will appear where will give our new API Project Name, In the above snapshot I have named it API Project V2. Click the Create project button and you will taken the the following screen.

The services tab in the google api console

The services tab in the google api console

Scroll down in the services and find “Google Maps Android API V 2“. The button in front of it will be showing Off. Click it to turn the service on.

Turning the Google Maps Android API v2  service on

Turning the Google Maps Android API v2 service on

When we turn on the service we will be prompted for Terms of Service. Agree and and click Accept button to proceed. Almost at the finish line to get our API key. Get to the Google API Console. In the navigation bar to the left click API Access.

Obtaining The API Key

API Access Page

API Access Page

From the above screen click on “Create new Android key” button. This will bring us to the following screen.

Configure Android Key for our project

Configure Android Key for our project

Copy your SHA-1 that we have previously displayed and noted down. Paste that in the empty text box and than enter a “;” semicolon at the end of the string and write your complete package name so the complete string becomes SHA-1 fingerprint+;+complete package name.

Key Generation

Key Generation

Click create and you will come to the previous screen with the following addition.

Our Google Maps Android API Ver 2.0 Key and Fingerprint with package name

Our Google Maps Android API Ver 2.0 Key and Fingerprint with package name

As you can see on the screen. Our API Key is listed and below that is the SHA-1+;+complete package name that will be some what like this.

A7:95:FC:3D:A8:1E:4B:CD:9D:D3:E2:E0:1B:D4:9C:39:5A:30:74:7F;com.android.gmapv2

The API Key will look like this.

AIzaSyAlm8fZijW4ViYnxfp-zF8-4-WK6LFY0ik
Copy the key and move on.

Adding the key to our gmapv2 application Open the gmapv2 application in Eclipse. Open the AndroidManifest.xml file and write the following code with your own API Key. Just above the application ending tag.

Putting the API Key in AndroidManifest.xml file

Putting the API Key in AndroidManifest.xml file

Now add the necessary permissions to AndroidManifest.xml file.

Setting up permissions in AndroidManifest.xml file for our first Android Google Maps API Ver 2.0 Application

Setting up permissions in AndroidManifest.xml file for our first Android Google Maps API Ver 2.0 Application

Code of the complete AndroidManifest.xml file will look like this.

Complete code of AndroidManifest.xml

Complete code of AndroidManifest.xml

So far we are done with getting the API Key and putting it in our gmapv2 application. In the next part of this series we will go through all the steps of how to deploy or test this application on actual device. How actual device can be connected to Eclipse. What challenges we will face and what are the solutions for that.

Note : Source code for this article

https://docs.google.com/file/d/0B7W5NGdltb1jeXBvRDNqaV9maFk/edit?usp=sharing

The reason behind writing this article is that I got a new machine and I was configuring it for Android development. Everything went well but when came to the step for generating the Google Maps API Key, I got stuck when couldn’t find “debug.keystore” file at the desired location inside “.android”  folder.

.android folder without debug.keystore file

If we don’t have debug.keystore file we can’t get Google Maps API Key. To solve this issue and to get the debug.keystore file in the desired location is

  1. Open Eclipse
  2. Start a new Android project
  3. Run the project in any AVD that’s already created.

When the project is successfully RUN on the Android Virtual Device. Now go to your “.android” folder and check. You will see that the “debug.keystore” is present there.

.android folder with debug.keystore

REASON

By default the “debug.keystore” file is not created in “.android” folder. When we successfully build our first project, the “debug.keystore” file is automatically created in “.android” folder.

Now we can use it to generate our Google Maps API Key.

Related Article

https://mirnauman.wordpress.com/2012/01/26/how-to-get-google-maps-api-key-for-android-issues-and-errors-solved/

 

This tutorial is first of the detailed tutorial that am planning to write. Some short details about my development environment.

  • Windows 7 Professional
  • Android 2.3.3(API10)
  • Google APIs (Google Inc)-API Level 10. (Create AVD of this API)
  • Eclipse Classic version 3.7.0

Before we start with the development of our first Google Maps Application for Android. We need to get Google Maps Api Key. I have made it very simple to get the key. Follow the link.

https://mirnauman.wordpress.com/2012/01/26/how-to-get-google-maps-api-key-for-android-issues-and-errors-solved/

Perform all the steps in the above link and get the key. When we have the key than we will proceed will our development of Google Maps Application for Android.

Start a new Android Project, when prompted for SDK selection

SDK selection

Select Google APIs Platform 2.3.3 API level 10, and move next. When the project has been created. Open your MainActivity.java file. In my case I have named it GooglemapsActivity.java. Import the following.


import com.google.android.maps.GeoPoint;
import com.google.android.maps.MapActivity;
import com.google.android.maps.MapController;
import com.google.android.maps.MapView;

Now change the line

public class GooglemapsActivity extends Activity {

to

public class GooglemapsActivity extends MapActivity {

Now open the main.xml file and add a MapView

<com.google.android.maps.<span class="hiddenSpellError">MapView
 android:id="@+id/mapview1"
 android:layout_width="fill_parent"
 android:layout_height="fill_parent"
 android:enabled="true"
 android:clickable="true"
 android:apiKey=""
 />


Save the changes. Now open the AndroidManifest.xml file and Add the following permissions above the application tag.

<uses-permission android:name="android.permission.INTERNET"/>
 <uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION"/>
 <uses-permission android:name="android.permission.ACCESS_FINE_LOCATION"/>

Now add the google maps library in the manifest file, inside the application tag above the activity tag.

<uses-library android:name="com.google.android.maps"/>

Save the changes.  Now Open the Android Virtual Device Manager and create a new AVD with the following settings.

Creating & Configuring New AVD

We must provide GPS and SD Card support in the AVD because we will need these two functionalities for our future tutorials. Now Right click on our googlemaps application from the PackageExplorer and click Run Configuration from Run As menu. Now Create new configuration from the selecting our googlemaps application on the Android Tab.

Run Configuration For Our Android Project

Selected Target AVD

And Select our recently created AVD from the target Tab. Click Apply and Run. Everything is fine still the application will not load any map. Reason is that, we have not created an instance of our MapView in our main GooglemapsActivity Class. To do that. Add the following lines of code below the setContentView() line.

mapView = (MapView) findViewById(R.id.mapview1);
mc = mapView.getController();

Save the changes and run the project again. Now everything is double fine but still the emulator will show empty grid like the one shown in the picture.

Google Maps Empty Grid

Reasons for this blank grid is that we have not supplied our application with the google maps api key. For this Open the main.xml file and replace the following code.

<com.google.android.maps.MapView
 android:id="@+id/mapview1"
 android:layout_width="fill_parent"
 android:layout_height="fill_parent"
 android:enabled="true"
 android:clickable="true"
 android:apiKey=""
 />

with

<com.google.android.maps.MapView
 android:id="@+id/mapview1"
 android:layout_width="fill_parent"
 android:layout_height="fill_parent"
 android:enabled="true"
 android:clickable="true"
 android:apiKey="xxxxxxxxxxxxxxxxxxxxxxxxxxx"
 />

The “xxxxxxxxxxxxxxxxxxxxxxx” show your Google Maps API Key that we have generated at the start of this tutorial. If the Google Maps API Key is correct than we will get a screen like this.

Default Map Location Loaded in the MapView Control

We can animate the map to a default location or to our desired coordinates. Add the following lines of code and the map will load at our desired coordinates and our desired zoom level.

String coordinates[] = {"30", "71"};
 double lat = Double.parseDouble(coordinates[0]);
 double lng = Double.parseDouble(coordinates[1]);

 GeoPoint p = new GeoPoint(
 (int) (lat * 1E6),
 (int) (lng * 1E6));

mc.animateTo(p);
 mc.setZoom(7);
 mapView.invalidate();

Map animated to our desired location

We have provided latitude = 30 and longitude = 71, zooming level =7. The map will center at the provided coordinates at the provided zooming level. I may have forgotten to explain some details so i am attaching the complete code the 3 main files for this project.

GooglemapsActivity.java Complete code

package com.maptest.googlemap;

import com.google.android.maps.GeoPoint;
import com.google.android.maps.MapActivity;
import com.google.android.maps.MapController;
import com.google.android.maps.MapView;
import android.os.Bundle;

public class GooglemapsActivity extends MapActivity {

 private MapView mapView;
 private MapController mc;

 /** Called when the activity is first created. */
 @Override
 public void onCreate(Bundle savedInstanceState) {
 super.onCreate(savedInstanceState);
 setContentView(R.layout.main);

 mapView = (MapView) findViewById(R.id.mapview1);
 mc = mapView.getController();

String coordinates[] = {"30", "71"};
 double lat = Double.parseDouble(coordinates[0]);
 double lng = Double.parseDouble(coordinates[1]);

 GeoPoint p = new GeoPoint(
 (int) (lat * 1E6),
 (int) (lng * 1E6));

mc.animateTo(p);
 mc.setZoom(7);
 mapView.invalidate();
 }

@Override
 protected boolean isRouteDisplayed() {
 // TODO Auto-generated method stub
 return false;
 }
}

AndroidManifest.xml Complete Code

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

<uses-sdk android:minSdkVersion="10" />
 <uses-permission android:name="android.permission.INTERNET"/>
 <uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION"/>
 <uses-permission android:name="android.permission.ACCESS_FINE_LOCATION"/>

<application
 android:icon="@drawable/ic_launcher"
 android:label="@string/app_name" >

 <uses-library android:name="com.google.android.maps"/>

 <activity
 android:name=".GooglemapsActivity"
 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>

main.xml Complete Code

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
 android:layout_width="fill_parent"
 android:layout_height="fill_parent"
 android:orientation="vertical" >

<com.google.android.maps.MapView
 android:id="@+id/mapview1"
 android:layout_width="fill_parent"
 android:layout_height="fill_parent"
 android:enabled="true"
 android:clickable="true"
 android:apiKey="xxxxxxxxxxxxxxxxxxxxxxxxxxxxx"
 />

</LinearLayout>

Note:- Please leave your comments if this article was helpful.

Update # 1.

Addressing the issues in comments from harrison , hennie and topan. The problem with force cash is solved by the following :

replace ur mapview control code in main.xml with this

<com.google.android.maps.MapView
        android:id=”@+id/mapview1″
        android:layout_width=”fill_parent”
        android:layout_height=”322dp”
        android:layout_weight=”0.64″
        android:apiKey=”0VUxp2vFvlmp4w3BsQu-xxxxxxxxxxxxxxxxxxxxxxg”
        android:clickable=”true” >
    </com.google.android.maps.MapView>
if this doesnt work than check ur google maps api key.
The above solution worked for Harrison and his reply to me was
Mir!Thank you very much, you have gotten this to work for me! Thank you very much!The solution was:
<com.google.android.maps.MapView

>

    </com.google.android.maps.MapView>For some reason it is really strict about the format of com.google.android.maps.MapViewThank you very much! Your help is GREATLY appreciated and now I can resume working on my design project.Thanks again,

Harrison

Update # 2.
Issue in which maps is not shown on the phone, if only dark screen is shown than this can be solved by zooming out a few times. Zooming out will solve the blackout screen problem. If map grid shows but no map appears than check the google maps api key.
Update # 3
For Android Google Maps API Ver 2.0 developers follow the following link.

 

Blog Stats

  • 326,315 hits

Enter your email address to follow this blog and receive notifications of new posts by email.

Join 233 other followers

%d bloggers like this: