开发工具:eclipse(Version: Mars.2 Release (4.5.2))
实现目标:
第一:先申请Key
网址:Key的申请地址为:http://lbsyun.baidu.com/apiconsole/key
第二:先导入百度开发包:
基本包
第三:在androidmanifest.xlm中加入访问权限和KEY信息
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"/> <uses-permission android:name="android.permission.INTERNET"/> <uses-permission android:name="com.android.launcher.permission.READ_SETTINGS" /> <uses-permission android:name="android.permission.WAKE_LOCK"/> <uses-permission android:name="android.permission.CHANGE_WIFI_STATE" /> <uses-permission android:name="android.permission.ACCESS_WIFI_STATE" /> <uses-permission android:name="android.permission.GET_TASKS" /> <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/> <uses-permission android:name="android.permission.WRITE_SETTINGS" />
<meta-data android:name="com.baidu.lbsapi.API_KEY" android:value="73hpce2WCOCE6gA89ZQ2Z0HdR8HYPkIG" />
第四:在mainActivity.java中加入主代码
</pre><pre name="code" class="java">MapView mMapView = null; private BaiduMap mBaiduMap; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); //在使用SDK各组件之前初始化context信息,传入ApplicationContext //注意该方法要再setContentView方法之前实现 SDKInitializer.initialize(getApplicationContext()); setContentView(R.layout.activity_main); //获取地图控件引用 initView(); } private void initView() { mMapView = (MapView) findViewById(R.id.bmapView); mBaiduMap = mMapView.getMap(); MapStatusUpdate msu = MapStatusUpdateFactory.zoomTo(15.0f); mBaiduMap.setMapStatus(msu); //设置地图的放大倍数 } @Override protected void onDestroy() { super.onDestroy(); //在activity执行onDestroy时执行mMapView.onDestroy(),实现地图生命周期管理 mMapView.onDestroy(); } @Override protected void onResume() { super.onResume(); //在activity执行onResume时执行mMapView. onResume (),实现地图生命周期管理 mMapView.onResume(); } @Override protected void onPause() { super.onPause(); //在activity执行onPause时执行mMapView. onPause (),实现地图生命周期管理 mMapView.onPause(); } @Override public boolean onCreateOptionsMenu(Menu menu) { // Inflate the menu; this adds items to the action bar if it is present. getMenuInflater().inflate(R.menu.main, menu); return true; } @Override public boolean onOptionsItemSelected(MenuItem item) { switch (item.getItemId()) { case R.id.id_map_common: mBaiduMap.setMapType(BaiduMap.MAP_TYPE_NORMAL); break; case R.id.id_map_site: mBaiduMap.setMapType(BaiduMap.MAP_TYPE_SATELLITE); //设置地图的类型 break; case R.id.id_map_traffic: if (mBaiduMap.isTrafficEnabled()) { mBaiduMap.setTrafficEnabled(false); //设置地图的类型 item.setTitle("实时交通(off)"); } else { mBaiduMap.setTrafficEnabled(true); item.setTitle("实时交通(on)"); } break; case R.id.id_map_heat: if (mBaiduMap.isBaiduHeatMapEnabled()) { mBaiduMap.setBaiduHeatMapEnabled(false);; //设置地图的类型 item.setTitle("热力地图(off)"); } else { mBaiduMap.setBaiduHeatMapEnabled(true); item.setTitle("热力地图(on)"); } break; default: break; } return super.onOptionsItemSelected(item); }
第五:加入布局activiy_main.xml文件
<com.baidu.mapapi.map.MapView android:id="@+id/bmapView" android:layout_width="fill_parent" android:layout_height="fill_parent" android:clickable="true" />
第六:加入目录main.xml文件
<com.baidu.mapapi.map.MapView android:id="@+id/bmapView" android:layout_width="fill_parent" android:layout_height="fill_parent" android:clickable="true" />