百度地图

来源:互联网 发布:java 反射 参数 null 编辑:程序博客网 时间:2024/06/11 00:15


准备工作
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() {@Overridepublic void onGetPermissionState(int iError) {//授权验证的结果if (iError == MKEvent.ERROR_PERMISSION_DENIED) {Toast.makeText(BaseActivity.this, "未通过验证", Toast.LENGTH_LONG).show();}}@Overridepublic 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的使用会存在问题

        @Overrideprotected void onResume() {mapView.onResume();super.onResume();}@Overrideprotected void onPause() {mapView.onPause();super.onPause();}@Overrideprotected void onDestroy() {mapView.destroy();super.onDestroy();}

0 0
原创粉丝点击