百度地图



准备工作
1、获取API Key
2、建立工程
3、添加地图引擎到Andoid工程中
4、添加权限
5、初始化地图引擎
6、引入布局(地图控件)

先初始化地图引擎,再引入布局(地图控件)

         protected static BMapManager manager;      
	 protected MapView mapView;
	 protected MapController controller;

         @Override
	 protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		//地图引擎初始化——发送Key到百度的服务端
		initManager();
		setContentView(R.layout.common);
		init();//初始化控件
	 }
        private void initManager() {
		if (manager == null) {
			manager = new BMapManager(this);
			/* init
			   public boolean init(String strKey, MKGeneralListener listener)
			       参数:strKey - 申请的授权验证码
			        listener - 注册回调事件
			*/

			manager.init(ConstantValue.KEY, new MKGeneralListener() {

				@Override
				public void onGetPermissionState(int iError) {
					//授权验证的结果
					if (iError == MKEvent.ERROR_PERMISSION_DENIED) {
						Toast.makeText(BaseActivity.this, "未通过验证", Toast.LENGTH_LONG).show();
					}
				}

				@Override
				public void onGetNetworkState(int iError) {
					//网络监测
					if (iError == MKEvent.ERROR_NETWORK_CONNECT) {
						Toast.makeText(BaseActivity.this, "无网络", Toast.LENGTH_LONG).show();
					}
				}
			});
		}
	}
        private void init() {
		mapView = (MapView) findViewById(R.id.mapview);

		//在mapView添加一组按钮
		mapView.setBuiltInZoomControls(true);

		//控制地图的缩放(缩放、旋转、移动)
		controller = mapView.getController();

		float zoomLevel = 12; //缩放级别[3,19]
		controller.setZoom(zoomLevel);

              controller.setCenter(hmPos);
	}

 
 
 
 

         MapView部分方法需要与Activity的生命周期方法绑定在一起,否则MapView的使用会存在问题

        @Override
	protected void onResume() {
		mapView.onResume();
		super.onResume();
	}

	@Override
	protected void onPause() {
		mapView.onPause();
		super.onPause();
	}

	@Override
	protected void onDestroy() {
		mapView.destroy();
		super.onDestroy();
	}

你可能感兴趣的:(百度地图)