1)下載百度地圖移動版API(Android)開發包
要在Android應用中使用百度地圖API,就需要在工程中引用百度地圖API開發包,這個開發包包含兩個文件:baidumapapi.jar和libBMapApiEngine.so。下載地址:http://dev.baidu.com/wiki/imap/index.php?title=Android%E5%B9%B3%E5%8F%B0/%E7%9B%B8%E5%85%B3%E4%B8%8B%E8%BD%BD
2)申請API Key
和使用Google map api一樣,在使用百度地圖API之前也需要獲取相應的API Key。百度地圖API Key與你的百度賬戶相關聯,因此您必須先有百度帳戶,才能獲得API Key;並且,該Key與您引用API的程序名稱有關。
百度API Key的申請要比Google的簡單多了,其實只要你有百度帳號,應該不超過30秒就能完成API Key的申請。申請地址:http://dev.baidu.com/wiki/static/imap/key/
3)創建一個Android工程
這里需要強調一點:百度地圖移動版api支持Android 1.5及以上系統,因此我們創建的工程應基於Android SDK 1.5及以上。
工程創建完成后,將baidumapapi.jar和libBMapApiEngine.so分別拷貝到工程的根目錄及libs/armeabi目錄下,並在工程屬性->Java Build Path->Libraries中選擇“Add JARs”,選定baidumapapi.jar,這樣就可以在應用中使用百度地圖API了。工程完整的目錄結構如下圖所示:
4)在布局文件中添加地圖控件(res/layout/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"
- >
- <com.baidu.mapapi.MapView android:id="@+id/map_View"
- android:layout_width="fill_parent"
- android:layout_height="fill_parent"
- android:clickable="true"
- />
- </LinearLayout>
5)創建Activity繼承com.baidu.mapapi.MapActivity
- package com.liufeng.baidumap;
- import android.graphics.drawable.Drawable;
- import android.os.Bundle;
- import com.baidu.mapapi.BMapManager;
- import com.baidu.mapapi.GeoPoint;
- import com.baidu.mapapi.MapActivity;
- import com.baidu.mapapi.MapController;
- import com.baidu.mapapi.MapView;
- public class MainActivity extends MapActivity {
- private BMapManager mapManager;
- private MapView mapView;
- private MapController mapController;
- @Override
- public void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- setContentView(R.layout.main);
- // 初始化MapActivity
- mapManager = new BMapManager(getApplication());
- // init方法的第一個參數需填入申請的API Key
- mapManager.init("285B415EBAB2A92293E85502150ADA7F03C777C4", null);
- super.initMapActivity(mapManager);
- mapView = (MapView) findViewById(R.id.map_View);
- // 設置地圖模式為交通地圖
- mapView.setTraffic(true);
- // 設置啟用內置的縮放控件
- mapView.setBuiltInZoomControls(true);
- // 用給定的經緯度構造一個GeoPoint(緯度,經度)
- GeoPoint point = new GeoPoint((int) (47.118440 * 1E6), (int) (87.493147 * 1E6));
- // 創建標記maker
- Drawable marker = this.getResources().getDrawable(R.drawable.iconmarka);
- // 為maker定義位置和邊界
- marker.setBounds(0, 0, marker.getIntrinsicWidth(), marker.getIntrinsicHeight());
- // 取得地圖控制器對象,用於控制MapView
- mapController = mapView.getController();
- // 設置地圖的中心
- mapController.setCenter(point);
- // 設置地圖默認的縮放級別
- mapController.setZoom(12);
- }
- @Override
- protected boolean isRouteDisplayed() {
- return false;
- }
- @Override
- protected void onDestroy() {
- if (mapManager != null) {
- mapManager.destroy();
- mapManager = null;
- }
- super.onDestroy();
- }
- @Override
- protected void onPause() {
- if (mapManager != null) {
- mapManager.stop();
- }
- super.onPause();
- }
- @Override
- protected void onResume() {
- if (mapManager != null) {
- mapManager.start();
- }
- super.onResume();
- }
- }
- 6)在AndroidManifest.xml中配置
- <?xml version="1.0" encoding="utf-8"?>
- <manifest xmlns:android="http://schemas.android.com/apk/res/android"
- package="com.liufeng.baidumap"
- android:versionCode="1"
- android:versionName="1.0">
- <application android:icon="@drawable/icon" android:label="@string/app_name">
- <activity android:name=".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>
- <uses-sdk android:minSdkVersion="4" />
- <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
- <uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
- <uses-permission android:name="android.permission.INTERNET" />
- <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
- <uses-permission android:name="android.permission.ACCESS_WIFI_STATE" />
- <uses-permission android:name="android.permission.CHANGE_WIFI_STATE" />
- <uses-permission android:name="android.permission.READ_PHONE_STATE" />
- </manifest>
- 7)運行結果