有多簡單呢?看,隻是顯示瞭一下地圖而已:
想編寫android谷歌地圖應用,準備工作比編寫其他應用要麻煩一些。因為:
- android谷歌地圖API,不是開源免費的,是谷歌的私有軟件,雖然是免費的;
- 這個API,需要時刻依賴向谷歌下載地圖信息。
那麼第一條還比較好辦。我這裡用的是android 2.1,用其他版本比如1.5的,需要做的類似。需要在項目中導入google map api,默認情況下是沒有的。默認情況是android某個版本比如android 2.1,現在需要改為對應版本的google apis,版本要和android版本一致。這個google apis是同版本的android超集,包含瞭google的私有應用api。比如:
這樣就可以在項目中使用比如:
com.google.android.maps.MapActivity
這還不夠,google需要一個簽名指紋的機制,要先到google註冊,並把這個指紋包含在應用中,才可以下載到地圖信息。也就是說每次下載地圖信息要帶著這個指紋信息。
指紋信息的註冊和獲取都是免費的。
指紋有兩種:
- 用於開發的debug指紋,隻能使用在自己的debug應用程序裡;
- 正式的指紋。
這裡隻需要第一種就可以瞭。
操作步驟是,首先開發環境要有JDK,應該都有的吧。進入JDK的bin目錄,執行:
會得到類似:
把指紋部分,復制下來。
然後訪問:
http://code.google.com/intl/zh-CN/android/add-ons/google-apis/maps-api-signup.html
這裡還有個前提,就是你要有google帳號,並且登錄。
把剛才的md5指紋,復制到紅框位置:
並且勾選同意協議。
提交後,會看到:
其實主要是得到紅框的密鑰。然後在程序或者佈局文件中,凡是用到MapView的地方,加入或者設置androidLapiKey屬性,就可以瞭。
代碼其實很簡單:
public class LocationActivity 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.map_view);
Log.i("welcome", "created map activity.");
}
@Override
protected boolean isRouteDisplayed() {
return false;
}
}
使用的佈局文件:
<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:id="@+id/map_view" android:layout_width="fill_parent"
android:layout_height="fill_parent" android:enabled="true"
android:clickable="true" android:apiKey="xxxxxxxxxxxxxx" />
</LinearLayout>
摘自 虛懷若谷