September 17, 2014
Hot Topics:
RSS RSS feed Download our iPhone app

Using the Google Maps API in Android

  • Updated: February 15, 2012
  • By Chunyen Liu
  • Send Email »
  • More Articles »

Mapping functionality has become a must-have feature for new mobile devices. With all the new technology advances, mobile devices as well as smartphones are more than capable of handling complicated mathematical calculations on their own or keeping up with the heavy-traffic communication with the servers. GPS devices used to be the predominant gadgets for mapping capabilities, but more and more mobile devices are offering almost full-featured GPS functionalities. Android provides free, direct access to Google's popular mapping tools.

In this article, I will explore key programming APIs that power Android mapping features using a sample software project, which you can download and save.

What Google APIs Are Available for Mapping?

Before you start, all the necessary Android development tools, plug-ins, and sample codes you will need are from Google's own Android site at http://developer.android.com/. It also provides easy-to-follow instructions to get you started. I recommend you do that first if you have not done so already.

The majority of the mapping APIs are within the package com.google.android.maps. As a minimum, two of them are required to embed the mapping tools inside your software: MapActivity and MapView. MapActivity manages the activity life cycle and services behind a MapView. MapView is an Android View that displays a map. Other than these APIs, you also have MapController to perform panning and zooming of a map. ItemizedOverlay and Overlay are used to draw the user's info or objects on top of the map.

Discussing mapping without mentioning GPS is nearly impossible now because GPS has become one of the indispensable features most people would want from their mobile devices. The package android.location is included for GPS support. LocationManager is the most important API; it provides access to the system location services. The mapping and GPS APIs are the essential elements for building location-based services (LBS). You will pretty much cover all these APIs in a working example later.

Constructing a MapView by a MapActivity

First, when you create a new Android project, you need to select the option with Google APIs instead of a generic version. Then you are required to enable the following permissions and libraries in the project's AndroidManifest.xml before your software can get access to mapping and location services:

<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" />

 

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

Next, you can construct a MapView only by a MapActivity because it depends on background threads that access the network and filesystem managed by MapActivity. That is to say, you should always start by extending your class from MapActivity as follows:

public class TutorialOnMaps extends MapActivity {

private static MapView mMapView;

 

/** Called when the activity is first created. */

@Override

public void onCreate(Bundle icicle) {

super.onCreate(icicle);

 

requestWindowFeature(Window.FEATURE_NO_TITLE);

setContentView(R.layout.main);

 

// Get the map view from resource file

mMapView = (MapView)findViewById(R.id.mv);

 

// Add zoom controls

mMapView.setBuiltInZoomControls(true);

}

}

}

While in the default resource file main.xml, you add in some basic on-screen buttons with transparent panels. To properly "inflate" the MapView, you declare a com.google.android.maps.MapView.

One very important thing to note is android:apiKey, which authenticates your software and Google's service. This key is mandatory and needs to be signed correctly. Using the default SDK debug certificate is fine for testing purpose but when software is ready to be released to the public, an officially signed release key is required. Check out the official page, "Obtaining a Maps API Key" at code.google.com. It takes only a couple of minutes to get your own API key.

Figure 1 shows the initial screen on the emulator with your on-screen buttons.



Click here for larger image

Figure 1. Initial Map Centered On A Selected Position

<?xml version="1.0" encoding="utf-8"?>

 

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"

android:layout_width="fill_parent"

android:layout_height="fill_parent" >

 

<com.google.android.maps.MapView android:id="@+id/mv"

android:apiKey="0u1BZtyaS2K9fcA1ach_lc_Au7HvIJwXtr1Devw"

android:enabled="true"

android:clickable="true"

android:focusableInTouchMode="true"

android:layout_width="fill_parent"

android:layout_height="fill_parent"

android:layout_weight="1" />

 

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"

android:orientation="horizontal"

android:layout_width="fill_parent"

android:layout_height="wrap_content"

android:background="#550000ff"

android:layout_alignParentTop="true"

android:padding="2px" >

<Button android:id="@+id/sat"

android:layout_width="wrap_content"

android:layout_height="wrap_content"

android:layout_marginLeft="40px"

android:text="Satellite" />

(...snipped)

</LinearLayout/>

 

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"

android:orientation="vertical"

android:layout_width="wrap_content"

android:layout_height="fill_parent"

android:background="#550000ff"

android:padding="2px" >

<Button android:id="@+id/zin"

android:layout_width="40dp"

android:layout_height="wrap_content"

android:layout_marginTop="50px"

android:text="+" />

(...snipped)

</LinearLayout>

 

</RelativeLayout >

 

 


Tags: Google Maps API, Android development



Page 1 of 5



Comment and Contribute

 


(Maximum characters: 1200). You have characters left.

 

 


Sitemap | Contact Us

Rocket Fuel