百度官方的开发指南: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>