一.百度地图的用途
1.设置覆盖物
2.到某一个地方的路线(打车,打的)
3.两点间的距离
4.搜索功能
二.百度地图的搭建
1.AndroidManifest.xml 添加授权文件
<!-- 添加授权信息 --> <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> <uses-permission android:name="android.permission.CALL_PHONE"></uses-permission> <!-- 对屏幕的支持 --> <supports-screens android:largeScreens="true" android:normalScreens="false" android:smallScreens="true" android:resizeable="true" android:anyDensity="true"/>
2.导入baidumapapi.jar 包导入
3.添加百度控件
<!-- 添加百度控件 --> <com.baidu.mapapi.MapView android:id="@+id/bmapView" android:layout_width="fill_parent" android:layout_height="fill_parent" android:clickable="true" />
4.<!------------重要代码------------->
package com.lihao.mymap; import com.baidu.mapapi.BMapManager; import com.baidu.mapapi.GeoPoint; import com.baidu.mapapi.MKGeneralListener; import com.baidu.mapapi.MapActivity; import com.baidu.mapapi.MapController; import com.baidu.mapapi.MapView; import android.os.Bundle; import android.app.Activity; import android.view.Menu; import android.widget.Toast; public class MainActivity extends MapActivity { private MapView mapView; //控件 private BMapManager bMapManager; //百度引擎 private String Key = "*******"; //申请的百度key值 private MapController mapController; //百度控制器 @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); mapView = (MapView)findViewById(R.id.bmapView); bMapManager = new BMapManager(MainActivity.this); //百度地图初始化验证key值是否正确 bMapManager.init(Key, new MKGeneralListener() { @Override public void onGetPermissionState(int arg0) { // TODO Auto-generated method stub if(arg0 == 300) { Toast.makeText(MainActivity.this, "请验证key值", 3).show(); } } @Override public void onGetNetworkState(int arg0) { // TODO Auto-generated method stub } }); this.initMapActivity(bMapManager); //获取控制器 mapController = mapView.getController(); //设置可以缩放 mapView.setBuiltInZoomControls(true); // 设置地图的中心点 117.214325,31.86119 GeoPoint geoPoint = new GeoPoint((int)(31.86119 * 1E6), (int)(117.214325 * 1E6)); mapController.setCenter(geoPoint); //缩放级别 也可以为12 mapController.setZoom(15); } //关闭 protected void onDestroy() { // TODO Auto-generated method stub super.onDestroy(); if(bMapManager != null) { bMapManager.destroy(); bMapManager = null; } } @Override //当Actitity开始准备与用户交互式调用该方法 protected void onResume() { // TODO Auto-generated method stub super.onResume(); if(bMapManager != null) { bMapManager.start(); } } @Override //当系统即将启动另外一个Activity之前调用该方法 protected void onPause() { // TODO Auto-generated method stub super.onPause(); if(bMapManager != null) { bMapManager.stop(); } } @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 protected boolean isRouteDisplayed() { // TODO Auto-generated method stub return false; } }
总结 使用百度地图api 必须要调用百度地图的init(),start(),stop(),destroy()
贴图