一、準備工作
1. 申請Android Map API Key
必要條件:google賬號以及系統的證明書。
首先找到我們的debug.keystore文件,如果您已經安裝瞭eclipse,並且配置好瞭android的開發環境(這裡不再重復環境的配置,前面的博客有詳細指導),可以通過Window -> Preference -> Android ->Build,我們可以看到Default debug keystore便是debug.keystore的路徑。
接下來我們要取得MD5的值,打開命令行,進入debug.keystore所在的目錄下,執行命令keytool -list -keystore debug.keystore,這裡會讓你輸入keystore密碼,默認是android。
接著我們要申請Android Map的API Key,打開網址:http://code.google.com/intl/zh-CN/android/maps-api-signup.html,登陸你的google賬號,輸入上步得到的MD5,生成API Key。
1. 創建基於Google APIs的AVD
Window -> AVD Manager->new,輸入AVD的名字,在Target中選擇Google APIs。
這裡需要註意的是,如果在Target選項中沒有Google APIs的選項,需要到Android SDK Manager中安裝Google APIs。
一、創建簡單基於GoogleAPIs的應用
1. 創建新的工程
前面跟創建普通android應用一樣,File -> new ->other -> Android Project,我們給工程命名googleMapApp,這裡要註意的是,選擇Target的時候要選擇Google APIs。
1. 必要的修改
打開AndroidManifest.xml文件,由於要使用Google Map APIs必須定義下面這句:
<uses-libraryandroid:name="com.google.android.maps" />
由於我們還要用到網絡,所以還要添加網絡訪問許可<uses-permissionandroid:name="android.permission.INTERNET"/>,如果不添加網絡許可,應用程序就不會顯示地圖,隻顯示一下網格線。
其次要在佈局文件main.xml中添加MapView屬性,代碼如下:
[html] <com.google.android.maps.MapView
android:id="@+id/mapView"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:apiKey="0DXjJ7k6Ul6gx2s4aQEbs8Chg43eW-dVeowPqIQ"
/>
<com.google.android.maps.MapView
android:id="@+id/mapView"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:apiKey="0DXjJ7k6Ul6gx2s4aQEbs8Chg43eW-dVeowPqIQ"
/>
其中的android:apiKey為登陸google賬號輸入MD5生成的API Key,這裡註意不要和MD5混淆!
類GoogleMapAppActivity要繼承MapActivity而不是Activity。具體代碼如下:
[java] public class GoogleMapAppActivity extends MapActivity {
public MapView mapView;
public MapController mapController;
public GeoPoint geoPoint;
/** 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.mapView);
mapView.setTraffic(true);//設置為交通模式
mapView.setClickable(true);
mapView.setBuiltInZoomControls(true);//設置可以縮放
mapController = mapView.getController();
geoPoint = new GeoPoint((int)40.38014*1000000,(int)117.00021*1000000); //設置起點為北京附近
mapController.animateTo(geoPoint);//定位到北京
mapController.setZoom(12);
}
@Override
protected boolean isRouteDisplayed() {
return false;
}
摘自 pku_android