使用Google地圖需要使用到Android定義的com.google.android.maps包,其中包含了一系列用于Google Map上顯示、控制和層疊信息的功能類。該類主要包括以下幾個(gè)類。
□ MapActivity:這個(gè)類用語顯示Google Map的Activity類,它需要連接底層網(wǎng)絡(luò)。任何想要顯示MapView的Activity都需要繼承MapActivity,并在其onCreate()方法中創(chuàng)建一個(gè)MapView對(duì)象。
□ MapView的地圖的顯示控件,可以設(shè)置不同的顯示模式,例如衛(wèi)星模式、街道模式或交通模式。
□ MapController則是MapView的控制器,可以控制MapView的顯示中心和縮放級(jí)別等功能。
□ Overlay:可顯示于地圖之上的可繪制對(duì)象。
□ GeoPoint:包含經(jīng)緯度位置的對(duì)象。
以下內(nèi)容以GoogleMapDemo為例,說明如何在Android系統(tǒng)中開發(fā)Google地圖程序。圖1是GoogleMapDemo示例的效果圖。

圖1 地圖模式、衛(wèi)星模式效果圖
在這個(gè)示例中,將在程序內(nèi)部設(shè)置一個(gè)坐標(biāo)點(diǎn),然后在程序啟動(dòng)時(shí),使用MapView控件在地圖上顯示這個(gè)坐標(biāo)點(diǎn)的位置。
在建立工程時(shí)將com.google.android.maps的擴(kuò)展庫(kù)添加到工程中,這樣就可以使用Google地圖的所有功能。在創(chuàng)建工程時(shí),在Build Target項(xiàng)中選擇Google APIs;創(chuàng)建工程后,修改/res/layout/main.xml文件,在布局中加入一個(gè)MapView控件,并設(shè)置剛獲取的“地圖密鑰”。main.xml文件的完整代碼如代碼清單1所示。
代碼清單1 main.xml
<?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">
<TextView android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="@string/hello"/>
<com.google.android.maps.MapView
android:id="@+id/mapview"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:enabled="true"
android:clickable="true"
android:apiKey="0mVK8GeO6WUz4S94z52CIGSSlvlTwnrE4DsiA"/>
</LinearLayout>
僅在布局中添加MapView控件,還不能夠直接在程序中調(diào)用這個(gè)控件,還需要將程序本身設(shè)置成MapActivity(com.google.android.maps.MapActivity)。MapActivity類負(fù)責(zé)處理顯示Google地圖所需的生命周期和后臺(tái)服務(wù)管理。
下面先給出整個(gè)GoogleMapDemo.java文件的完整代碼,如代碼清單2所示。
代碼清單2 GoogleMapDemo.java
package cn.com.farsight.GoogleMapDemo;
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 GoogleMapDemo extends MapActivity {
private MapView mapView;
private MapController mapController;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
mapView = (MapView)findViewById(R.id.mapview);
//獲取了MapController
mapController = mapView.getController();
//設(shè)定經(jīng)度、緯度的地理坐標(biāo)點(diǎn)
Double lng = 116.391483 * 1E6;
Double lat = 39.9055472 * 1E6;
GeoPoint point = new GeoPoint(lat.intValue(), lng.intValue());
//將這個(gè)坐標(biāo)轉(zhuǎn)化為GeoPoint再使用;設(shè)置MapView的“顯示中點(diǎn)”
mapController.setCenter(point);
//設(shè)置放大層級(jí)
mapController.setZoom(11);
//將MapView顯示區(qū)域的中心移動(dòng)到“顯示中心”
mapController.animateTo(point);
//設(shè)定MapView的地圖顯示模式。true則為衛(wèi)星模式,設(shè)置false則為普通模式
mapView.setSatellite(false);
}
//是統(tǒng)計(jì)程序是否顯示在Google地圖中顯示路徑信息,默認(rèn)為不顯示
@Override
protected boolean isRouteDisplayed() {
return false;
}
}
由于獲取Google地圖是需要使用互聯(lián)網(wǎng)的,所以在運(yùn)行前還需要在AndroidManifest.xml文件中添加允許訪問互聯(lián)網(wǎng)的許可。AndroidManifest.xml文件的完整代碼如代碼清單3所示。
代碼清單3 AndroidManifest.xml
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="cn.com.farsight.GoogleMapDemo"
android:versionCode="1"
android:versionName="1.0">
<application android:icon="@drawable/icon"
android:label="@string/app_name">
<activity android:name=".GoogleMapDemo"
android:label="@string/app_name">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<uses-library android:name="com.google.android.maps"></uses-library>
</application>
<uses-sdk android:minSdkVersion="7" />
<uses-permission android:name="android.permission.INTERNET"/>
</manifest>