Android上面的百度地圖使用步驟

來源:互聯網
上載者:User

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)
      

[xhtml] view plaincopy

  1. <?xml version="1.0" encoding="utf-8"?>  
  2. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"  
  3.     android:orientation="vertical"  
  4.     android:layout_width="fill_parent"  
  5.     android:layout_height="fill_parent"  
  6.     >  
  7.     <com.baidu.mapapi.MapView android:id="@+id/map_View"  
  8.         android:layout_width="fill_parent"  
  9.         android:layout_height="fill_parent"  
  10.         android:clickable="true"  
  11.     />  
  12. </LinearLayout>  


  5)建立Activity繼承com.baidu.mapapi.MapActivity
     

[java] view plaincopy

  1. package com.liufeng.baidumap;  
  2.   
  3. import android.graphics.drawable.Drawable;  
  4. import android.os.Bundle;  
  5.   
  6. import com.baidu.mapapi.BMapManager;  
  7. import com.baidu.mapapi.GeoPoint;  
  8. import com.baidu.mapapi.MapActivity;  
  9. import com.baidu.mapapi.MapController;  
  10. import com.baidu.mapapi.MapView;  
  11.   
  12. public class MainActivity extends MapActivity {  
  13.     private BMapManager mapManager;  
  14.     private MapView mapView;  
  15.     private MapController mapController;  
  16.   
  17.     @Override  
  18.     public void onCreate(Bundle savedInstanceState) {  
  19.         super.onCreate(savedInstanceState);  
  20.         setContentView(R.layout.main);  
  21.   
  22.         // 初始化MapActivity  
  23.         mapManager = new BMapManager(getApplication());  
  24.         // init方法的第一個參數需填入申請的API Key  
  25.         mapManager.init("285B415EBAB2A92293E85502150ADA7F03C777C4", null);  
  26.         super.initMapActivity(mapManager);  
  27.   
  28.         mapView = (MapView) findViewById(R.id.map_View);  
  29.         // 設定地圖模式為交通地圖  
  30.         mapView.setTraffic(true);  
  31.         // 設定啟用內建的縮放控制項  
  32.         mapView.setBuiltInZoomControls(true);  
  33.   
  34.         // 用給定的經緯度構造一個GeoPoint(緯度,經度)  
  35.         GeoPoint point = new GeoPoint((int) (47.118440 * 1E6), (int) (87.493147 * 1E6));  
  36.   
  37.         // 建立標記maker  
  38.         Drawable marker = this.getResources().getDrawable(R.drawable.iconmarka);  
  39.         // 為maker定義位置和邊界  
  40.         marker.setBounds(0, 0, marker.getIntrinsicWidth(), marker.getIntrinsicHeight());  
  41.   
  42.         // 取得地圖控制器對象,用於控制MapView  
  43.         mapController = mapView.getController();  
  44.         // 設定地圖的中心  
  45.         mapController.setCenter(point);  
  46.         // 設定地圖預設的縮放層級  
  47.         mapController.setZoom(12);  
  48.     }  
  49.   
  50.     @Override  
  51.     protected boolean isRouteDisplayed() {  
  52.         return false;  
  53.     }  
  54.   
  55.     @Override  
  56.     protected void onDestroy() {  
  57.         if (mapManager != null) {  
  58.             mapManager.destroy();  
  59.             mapManager = null;  
  60.         }  
  61.         super.onDestroy();  
  62.     }  
  63.   
  64.     @Override  
  65.     protected void onPause() {  
  66.         if (mapManager != null) {  
  67.             mapManager.stop();  
  68.         }  
  69.         super.onPause();  
  70.     }  
  71.   
  72.     @Override  
  73.     protected void onResume() {  
  74.         if (mapManager != null) {  
  75.             mapManager.start();  
  76.         }  
  77.         super.onResume();  
  78.     }  
  79. }  
  80. 6)在AndroidManifest.xml中配置
         [xhtml] view plaincopy

    1. <?xml version="1.0" encoding="utf-8"?>  
    2. <manifest xmlns:android="http://schemas.android.com/apk/res/android"  
    3.       package="com.liufeng.baidumap"  
    4.       android:versionCode="1"  
    5.       android:versionName="1.0">  
    6.     <application android:icon="@drawable/icon" android:label="@string/app_name">  
    7.         <activity android:name=".MainActivity" android:label="@string/app_name">  
    8.             <intent-filter>  
    9.                 <action android:name="android.intent.action.MAIN" />  
    10.                 <category android:name="android.intent.category.LAUNCHER" />  
    11.             </intent-filter>  
    12.         </activity>  
    13.     </application>  
    14.   
    15.     <uses-sdk android:minSdkVersion="4" />  
    16.     <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />  
    17.     <uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />  
    18.     <uses-permission android:name="android.permission.INTERNET" />  
    19.     <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />  
    20.     <uses-permission android:name="android.permission.ACCESS_WIFI_STATE" />  
    21.     <uses-permission android:name="android.permission.CHANGE_WIFI_STATE" />  
    22.     <uses-permission android:name="android.permission.READ_PHONE_STATE" />  
    23. </manifest>   
    24. 7)運行結果
            


相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.