Archive
Use Google Maps in Android Using Eclipse and Debug Key
Google Maps API is not included by default in the Android SDK. Download it first using the Android SDK and AVD Manager. It is included in the Google APIs by Google Inc. under Third party Add-ons.
To use the Google Maps service, we need to obtain a Google Maps API Key. We need to sign our android application and get the MD5 fingerprint of the certificate. In this example though, we’ll just be using the Android SDK’s debug certificate. This is the one used by the Eclipse ADT plugin to run our application in the emulator. Detailed steps in obtaining a key can be found here: http://code.google.com/android/add-ons/google-apis/mapkey.html. To make things short, go to the .android folder and execute the following in the command line: keytool -list -alias androiddebugkey -keystore debug.keystore -storepass android -keypass android. Here is a sample screenshot.
Copy the generated MD5 fingerprint and go to the following web page: http://code.google.com/android/maps-api-signup.html. Assuming you already have a Google account, enter the MD5 fingerprint in the specified text box and click the Generate API Key button. This will show you to another page containing your API Key.
When you create your project, ensure that the build target is set to Google APIs. If you already have an existing project, go to the project’s Properties window > Android and change the Project Build Target.
After that, open the AndroidManifest.xml file and add these two lines:
<uses-permission android:name=”android.permission.INTERNET” />
<uses-library android:name=”com.google.android.maps” />
Here’s a sample:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.mike.mapexample"
android:versionCode="1"
android:versionName="1.0">
<uses-sdk android:minSdkVersion="10" />
<uses-permission android:name="android.permission.INTERNET" />
<application android:icon="@drawable/icon" android:label="@string/app_name">
<uses-library android:name="com.google.android.maps" />
<activity android:name=".MapExampleActivity"
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>
To display the map on the main screen of our app, open the main.xml under the res > layout directory and add a MapView like below:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>
<com.google.android.maps.MapView
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:apiKey="<your API Key here>"
/>
</LinearLayout>
Next, our activity must inherit from MapActivity instead of Activity. Import com.google.android.maps.MapActivity and implement the necessary methods.
package com.mike.mapexample;
import com.google.android.maps.MapActivity;
import android.os.Bundle;
public class MapExampleActivity extends MapActivity {
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
}
@Override
protected boolean isRouteDisplayed() {
// TODO Auto-generated method stub
return false;
}
}
That’s it! You should now be able to see Google Maps in the emulator, assuming you have run the appropriate virtual device. If you still haven’t, go again to the Android SDK and AVD Manager. Go to Virtual devices and create a new android virtual device. Select the Google APIs (Google Inc.) with the desired API Level.
Set this device as the emulator for your project. Here’s a screenshot of the emulator running Google Maps.
Open Existing Android Project in Eclipse
I downloaded sample android applications from http://code.google.com/p/apps-for-android/. Coming from a .NET development background and using Visual Studio as IDE, it took me several minutes to figure out to open a sample app in Eclipse. To open an existing project, click on File > Import… menu item.
Select “Existing Projects into Workspace” and click the Next button. Select the root directory where the samples are located. Eclipse will search for existing projects in the said folder. Although there are a lot of samples in the folder, I think Eclipse will only load samples that have .project and .classpath files.
Afterwards, select the project that you want to import. You can optionally copy the project to your workspace. Click the Finish button and you should already see the imported projects in the package explorer. In my case, I get the following error:
[2011-06-26 11:20:12 - WebViewDemo] /WebViewDemo/gen already exists but is not a source folder. Convert to a source folder or rename it.
Just delete the gen directory in the package explorer and Eclipse will automatically regenerate it. That will clear the error.
Getting Started with Android Development Using Eclipse
I wanted to learn programming in Android, and might as well create an app that could generate extra income. I wanted to blog my experience with Android development and I hope that someone else find my posts helpful. Anyway, before I could create an Android program, I’ll need to install the tools to get started. I’ll be following the instructions laid out in http://developer.android.com/sdk/eclipse-adt.html. It’s been a long time since I used Eclipse because I use Visual Studio at work. I think it’s not required to use Eclipse for Android development but having an IDE would certainly make life easier.
First, we need to download and install the Android SDK. You can download it in http://developer.android.com/sdk/index.html. Select the appropriate version for your operating system. The installer will first check if you have a Java Development Kit (JDK) installed. If it is not yet installed, it won’t continue so just download the JDK and install it first. Afterwards, run the SDK manager to download packages. In my case, I just downloaded the packages checked by default.
You can download the Eclipse IDE in http://www.eclipse.org/downloads/. I downloaded the Eclipse Classic version. It is a zipped file containing the binaries. Just unzip the file and put the contents in the directory you desire. There is no setup required. You may also want to create a shortcut on the desktop. Open up Eclipse and select the directory for your workspace. Go to the Help > Install New Software menu. It opens up the Install dialog. Click on the Add button to add the ADT Plugin repository like shown below.
Afterwards, select the ADT Plugin in the “Work with” combo box. Also select the items that show up on the list.
Continue with the wizard, accept the license agreements, and install the selected software. Restart the Eclipse IDE after. Go to Window > Preferences. Select Android from the list. The Android SDK location is not set yet. Set it to the Android SDK directory. On my machine, I set it to C:\Program Files\Android\android-sdk. Click on the Apply button and the list of SDK targets is refreshed.
To test if our setup is working perfectly, we can try running a sample included in the Android SDK. Check the C:\Program Files\Android\android-sdk\samples directory for the different samples. There are different folders for each API level. To use one, click on the File > New > Other… from the menu. It will show a dialog with different wizard choices. Choose the Android Project wizard. It will open the New Android Project dialog. Select the “Create project from existing sample” radio button. Select the build target and the sample you want to run. I chose the 2.3.3 build target in this example.
Click the Finish button. You can see the added project in the Package Explorer. Right-click on the project and select Run As > Android Application. You can also run the application by using the Run button in the toolbar. You may be prompted to add a new Android Virtual Device. Click Yes and the Android SDK and AVD Manager dialog will be shown. You can see the list of available virtual devices. Click on the New button to create a virtual device compatible with the desired build target.
Click on the Create AVD (Android Virtual Device) button. Run the application again and it should launch the emulator.











