刚学完android,决定来个小小应用。做什么呢?之前在公司看人家用ios做的地图不错,干脆我就做个android版吧。
百度了一下它的api,很简单。花了半天解决。首先新建一个工程,然后把baidumapapi.jar拷贝到工程下的lib文件夹里,同时在里面建一个文件夹armeabi,再把libBMapApiEngine_v1_3_3.so动态库文件拷进去,打开eclipse,右键点击工程的Properties,进入Java Build Path,打开Libraries,点击Add External JARS,把baidumapapi.jar添加进去,ok,配置好了。
自定义一个Aplication类,WhereMapApplication,继承自Application(拷贝自官方百度):
public class WhereMapApplication extends Application { static WhereMapApplication mDemoApp; //百度MapAPI的管理类 BMapManager mBMapMan = null; // 授权Key // TODO: 请输入您的Key, // 申请地址:http://dev.baidu.com/wiki/static/imap/key/ String mStrKey = "申请的KEY值"; boolean m_bKeyRight = true; // 授权Key正确,验证通过 // 常用事件监听,用来处理通常的网络错误,授权验证错误等 static class MyGeneralListener implements MKGeneralListener { @Override public void onGetNetworkState(int iError) { Log.d("MyGeneralListener", "onGetNetworkState error is "+ iError); Toast.makeText(WhereMapApplication.mDemoApp.getApplicationContext(), "您的网络出错啦!", Toast.LENGTH_LONG).show(); } @Override public void onGetPermissionState(int iError) { Log.d("MyGeneralListener", "onGetPermissionState error is "+ iError); if (iError == MKEvent.ERROR_PERMISSION_DENIED) { // 授权Key错误: Toast.makeText(WhereMapApplication.mDemoApp.getApplicationContext(), "请在BMapApiDemoApp.java文件输入正确的授权Key!", Toast.LENGTH_LONG).show(); WhereMapApplication.mDemoApp.m_bKeyRight = false; } } } @Override public void onCreate() { Log.v("BMapApiDemoApp", "onCreate"); mDemoApp = this; mBMapMan = new BMapManager(this); mBMapMan.init(this.mStrKey, new MyGeneralListener()); mBMapMan.getLocationManager().setNotifyInternal(10, 5); // if (mBMapMan != null) { // mBMapMan.destroy(); // mBMapMan = null; // } super.onCreate(); } @Override //建议在您app的退出之前调用mapadpi的destroy()函数,避免重复初始化带来的时间消耗 public void onTerminate() { // TODO Auto-generated method stub if (mBMapMan != null) { mBMapMan.destroy(); mBMapMan = null; } super.onTerminate(); } }
百度申请key地址:http://dev.baidu.com/wiki/static/imap/key/
注册百度用户,再申请key值,把key值替换以上代码中的"申请的KEY值"。
修改AndroidManifest.xml文件中application的属性,添加android:name=".WhereMapApplication"如下:
<application android:name=".WhereMapApplication" android:icon="@drawable/ic_launcher" android:label="@string/app_name" >
<uses-sdk android:minSdkVersion="3" /> <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" > </uses-permission> <uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" > </uses-permission> <uses-permission android:name="android.permission.INTERNET" > </uses-permission> <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" > </uses-permission> <uses-permission android:name="android.permission.ACCESS_WIFI_STATE" > </uses-permission> <uses-permission android:name="android.permission.CHANGE_WIFI_STATE" > </uses-permission> <uses-permission android:name="android.permission.READ_PHONE_STATE" > </uses-permission> <supports-screens android:anyDensity="true" android:largeScreens="true" android:normalScreens="true" android:resizeable="true" android:smallScreens="true" />
public class MainActivity extends MapActivity { @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); WhereMapApplication app = (WhereMapApplication)this.getApplication(); if (app.mBMapMan == null) { app.mBMapMan = new BMapManager(getApplication()); app.mBMapMan.init(app.mStrKey, new WhereMapApplication.MyGeneralListener()); } app.mBMapMan.start(); // 如果使用地图SDK,请初始化地图Activity // long iTime = System.nanoTime(); super.initMapActivity(app.mBMapMan); // iTime = System.nanoTime() - iTime; // Log.d("MapViewDemo", "the init time is " + iTime); MapView mapView = (MapView)findViewById(R.id.bmapView); mapView.setBuiltInZoomControls(true); } @Override public boolean onCreateOptionsMenu(Menu menu) { getMenuInflater().inflate(R.menu.activity_main, menu); return true; } @Override protected boolean isRouteDisplayed() { // TODO Auto-generated method stub return false; } @Override protected void onPause() { WhereMapApplication app = (WhereMapApplication)this.getApplication(); if(app.mBMapMan != null ) app.mBMapMan.stop(); super.onPause(); } @Override protected void onResume() { WhereMapApplication app = (WhereMapApplication)this.getApplication(); app.mBMapMan.start(); super.onResume(); } }
public class MainActivity extends MapActivity { @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); WhereMapApplication app = (WhereMapApplication)this.getApplication(); if (app.mBMapMan == null) { app.mBMapMan = new BMapManager(getApplication()); app.mBMapMan.init(app.mStrKey, new WhereMapApplication.MyGeneralListener()); } app.mBMapMan.start(); // 如果使用地图SDK,请初始化地图Activity // long iTime = System.nanoTime(); super.initMapActivity(app.mBMapMan); // iTime = System.nanoTime() - iTime; // Log.d("MapViewDemo", "the init time is " + iTime); MapView mapView = (MapView)findViewById(R.id.bmapView); mapView.setBuiltInZoomControls(true); } @Override public boolean onCreateOptionsMenu(Menu menu) { getMenuInflater().inflate(R.menu.activity_main, menu); return true; } @Override protected boolean isRouteDisplayed() { // TODO Auto-generated method stub return false; } @Override protected void onPause() { WhereMapApplication app = (WhereMapApplication)this.getApplication(); if(app.mBMapMan != null ) app.mBMapMan.stop(); super.onPause(); } @Override protected void onResume() { WhereMapApplication app = (WhereMapApplication)this.getApplication(); app.mBMapMan.start(); super.onResume(); } }
这个时候已经能显示出地图了,不过是在模拟器上测试的,没有真机,只能先写在这里了。其余如打点,搜索等功能在官方代码中均有详细说明,就不一一描述了。