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.