Need help with directions? Or want to know your current location? Google Maps have got you covered!
Google Maps is one of the great services that Google provides free of cost. This tutorial, you will create a simple application to display your current position in a map and update it every time as you move. Using Maps in your Android application is easy, simply follow the outlined steps below:
Step 1: Get Google Maps API key
To use the Google Maps Embed API, you must register your app project on the Google API Console and get a Google API key which you can add to your app or website. Go to the Google Maps API registration page and enter the MD5 certificate fingerprint to generate your API key.
Step 2: Create the Android Project
Create the Android project using the following settings:
To let your application use the Global Positioning System(GPS) of the Android phone, you need to add some permissions to the AndroidManifest.xml. These are the required permissions are for accessing location and internet services. After adding these permissions, your manifest file should like this:
<?xml version=”1.0″ encoding=”utf-8″?>
<manifest xmlns:android=”http://schemas.android.com/apk/res/android”
package=”com.example.mymaps”
android:versionCode=”1″
android:versionName=”1.0″ >
<uses-sdk
android:minSdkVersion=”9″
android:targetSdkVersion=”10″ />
<uses-permission android:name=”android.permission.ACCESS_COARSE_LOCATION” />
<uses-permission android:name=”android.permission.ACCESS_FINE_LOCATION” />
<uses-permission android:name=”android.permission.INTERNET” />
<application
android:allowBackup=”true”
android:icon=”@drawable/ic_launcher”
android:label=”@string/app_name”
android:theme=”@style/AppTheme” >
<activity
android:name=”com.example.mymaps.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>
Step 3: Create the View
In order to display the map in your Android app view add a MapView component inside your layout file.
<LinearLayout xmlns:android=”http://schemas.android.com/apk/res/android”
android:id=”@+id/frame”
android:layout_width=”fill_parent”
android:layout_height=”fill_parent”
android:orientation=”vertical” >
<com.google.android.maps.MapView
android:id=”@+id/map”
android:layout_width=”fill_parent”
android:layout_height=”fill_parent”
android:apiKey=”Your key goes here”
android:clickable=”true” />
</LinearLayout>
Note: Replace the word "Your key goes here" with your API key.
Also, specify that you are going to use the Google Maps Library in the manifest. So add the following line to your manifest file:
<uses-library android:name=”com.google.android.maps” />
Step 4: Write the map activity
In order to use a MapView, extend the MapActivity. Create a simple Activity class extending the MapActivity as shown below:
package com.example.mymaps; import com.google.android.maps.MapActivity; import com.google.android.maps.MapController; import com.google.android.maps.MapView; import com.google.android.maps.MyLocationOverlay; import android.os.Bundle; import android.app.Activity; import android.view.Menu; public class MainActivity extends MapActivity { private MapView map; private MapController controller; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); //functions to be implemented initMapView(); initMyLocation(); } @Override public boolean onCreateOptionsMenu(Menu menu) { // Inflate the menu; this adds items to the action bar if it is present. getMenuInflater().inflate(R.menu.activity_main, menu) return true; } @Override protected boolean isRouteDisplayed() { // TODO Auto-generated method stub return false; } } // Next thing that I did was to initialize my MapView: private void initMapView() { map = (MapView) findViewById(R.id.mapview); controller = map.getController(); map.setSatellite(true); map.setBuiltInZoomControls(true); } // And lastly added my location overlay: private void initMyLocation() { final MyLocationOverlay overlay = new MyLocationOverlay(this, map); overlay.enableMyLocation(); //overlay.enableCompass(); // does not work in emulator overlay.runOnFirstFix(new Runnable() { public void run() { // Zoom in to current location controller.setZoom(8); controller.animateTo(overlay.getMyLocation()); } }); map.getOverlays().add(overlay); }
You are all set, now run your app in the simulator or on your phone. Is it telling your location correctly? If it does, you have correctly used the Maps API.
Some Guidelines:
• Make sure that Google API is enabled for your Android Virtual Device (AVD).
• You can set the GPS coordinates using the Dalvik Debug Monitor Server (DDMS) in Eclipse. Your app will NOT show anything until you set the coordinates.
• Once you are sure your app is working fine don’t forget to sign it using a new certificate. The debug certificate is only for debugging!
The complete Eclipse project is attached with the post.