百度官方的開發指南:http://developer.baidu.com/map/sdkandev-2.htm
今天搞了一下午,總報錯error inflating class com.baidu.mapapi.map.MapView
后來翻看開發指南才解決了問題:要在setContentView前初始化BMapManager對象,否則會報錯
貌似剛好碰上開發指南的更新。。幾個鍾就這樣沒了。。
而且今天申請的key貌似也用不了。。用以前的就可以。。還有就是我的key列表上所有的key都沒了。。
浪費了我這么多時間。。
簡單的demo:
1 package my.daokantushuo; 2 3 import com.baidu.mapapi.BMapManager; 4 import com.baidu.mapapi.MKGeneralListener; 5 import com.baidu.mapapi.map.MKEvent; 6 import com.baidu.mapapi.map.MapController; 7 import com.baidu.mapapi.map.MapView; 8 import com.baidu.platform.comapi.basestruct.GeoPoint; 9 10 import android.os.Bundle; 11 import android.app.Activity; 12 import android.content.Context; 13 import android.view.Menu; 14 import android.view.Window; 15 import android.widget.Toast; 16 17 public class MyMapActivity extends Activity { 18 private BMapManager mBMapMan=null; 19 private MapView mMapView=null; 20 private static final String BMAPKEY = "你的key"; 21 private static Context mapContext; 22 @Override 23 protected void onCreate(Bundle savedInstanceState) { 24 super.onCreate(savedInstanceState); 25 //去掉標題 26 requestWindowFeature(Window.FEATURE_NO_TITLE); 27 28 mBMapMan = new BMapManager(getApplication()); 29 mBMapMan.init(BMAPKEY, new MyGeneralListener()); 30 31 //注意:請在用使用setContentView前初始化BMapManager對象,否則會報錯 32 setContentView(R.layout.activity_my_map); 33 34 mapContext = getApplicationContext(); 35 mMapView = (MapView) findViewById(R.id.bmapsView); 36 mMapView.setBuiltInZoomControls(true); //設置啟用內置的縮放控件 37 38 MapController mMapController = mMapView.getController(); // 得到mMapView的控制權,可以用它控制和驅動平移和縮放 39 GeoPoint point = new GeoPoint((int) (39.915 * 1E6), (int) (116.404 * 1E6)); //用給定的經緯度構造一個GeoPoint,單位是微度 (度 * 1E6) 40 mMapController.setCenter(point); //設置地圖中心點 41 mMapController.setZoom(12); //設置地圖zoom級別 42 //雙擊地圖放大 43 mMapView.setDoubleClickZooming(true); 44 } 45 46 @Override 47 public boolean onCreateOptionsMenu(Menu menu) { 48 // Inflate the menu; this adds items to the action bar if it is present. 49 getMenuInflater().inflate(R.menu.activity_my_map, menu); 50 return true; 51 } 52 53 @Override 54 protected void onDestroy() { 55 mMapView.destroy(); 56 if (mBMapMan != null) { 57 mBMapMan.destroy(); 58 mBMapMan = null; 59 } 60 super.onDestroy(); 61 } 62 63 @Override 64 protected void onPause() { 65 mMapView.onPause(); 66 if (mBMapMan != null) { 67 mBMapMan.stop(); 68 } 69 super.onPause(); 70 } 71 72 @Override 73 protected void onResume() { 74 mMapView.onResume(); 75 if (mBMapMan != null) { 76 mBMapMan.start(); 77 } 78 super.onResume(); 79 } 80 81 // 常用事件監聽,用來處理通常的網絡錯誤,授權驗證錯誤等 82 static class MyGeneralListener implements MKGeneralListener { 83 84 @Override 85 public void onGetNetworkState(int iError) { 86 if (iError == MKEvent.ERROR_NETWORK_CONNECT) { 87 Toast.makeText(mapContext, "您的網絡出錯啦!", Toast.LENGTH_LONG).show(); 88 } 89 else if (iError == MKEvent.ERROR_NETWORK_DATA) { 90 Toast.makeText(mapContext, "輸入正確的檢索條件!", Toast.LENGTH_LONG).show(); 91 } 92 // ... 93 } 94 95 @Override 96 public void onGetPermissionState(int iError) { 97 if (iError == MKEvent.ERROR_PERMISSION_DENIED) { 98 //授權Key錯誤: 99 Toast.makeText(mapContext, "授權Key錯誤", Toast.LENGTH_LONG).show(); 100 } 101 } 102 } 103 }
布局文件activity_my_map.xml:
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 <com.baidu.mapapi.map.MapView 7 android:id="@+id/bmapsView" 8 android:layout_width="match_parent" 9 android:layout_height="match_parent" 10 android:clickable="true" /> 11 </LinearLayout>