基于Fragment的百度地图框架的使用

博客:http://blog.csdn.net/developer_jiangqq

(一)基本介绍(Fragment和SupportMapFragment):

      Fragment的使用现在安卓APP开发中用的比较多了,Fragment名称为碎片和Activity有着相似的生命管理周期,基本作用可以进行开发兼容手机和平板的app,较少兼容分辨率版本的工作量,提高工作效率。其中Fragment还是可以包含控制代码的视图模块,可以非常方便的进行组合。使用起来很方便。具体有个Fragment的介绍和使用请点击查看(官网Fragment的介绍和基本使用),这边就不多讲了。下面我们来看下百度地图API给我们提供的地图框架SupportMapFragment。SupportMapFragment是继承于Fragment因此具备Fragment的完成的生命周期,该框架主要用来管理地图生命周期,主要有下面两个方法1:getMapView():获取MapView引用,要在onCreateView函数调用之后进行使用.2:newInstance();返回获取SupportMapFragment对象实例。

(二)使用方法: 

     使用方法和我们平时用Fragment的时候差不多,首先获取SupportMapFragment实例,然后把该Fragment添加到指定View视图图.commit一下即可.

(三)代码演示,注释基本已经写了挺详细了,直接看就OK了

     1.自定义MyApplication.java设置全局变量,并且进行校验密钥:

package com.ztt.baidumap.ui;

import android.app.Application;
import android.content.Context;
import android.util.Log;
import android.widget.Toast;

import com.baidu.mapapi.BMapManager;
import com.baidu.mapapi.MKGeneralListener;
import com.baidu.mapapi.map.MKEvent;
/**
 * 自定义Application,进行key识别验证 (使用单例)
 * @author Jiangqq
 * @time 2014/03/15 10:14
 */
public class MyApplication extends Application {
    public static MyApplication instance=null;
    BMapManager mBMapManager = null;
    public boolean m_bKeyRight = true;
    public static final String strKey = "vUAGbPwLpolIqiwWisnQPeIE";  //百度地图官网申请的密钥
    public static MyApplication getInstance(){
        return instance;
    }
    @Override
    public void onCreate() {
        super.onCreate();
        instance=this;
        //在APP应用启动的时候,进行初始化验证
        initEngineManager(this);
    }
    
    /**
     * 进行验证key
     * @param pContext
     */
    private void initEngineManager(Context pContext)
    {
         if (mBMapManager == null) {
                mBMapManager = new BMapManager(pContext);
            }

            if (!mBMapManager.init(strKey,new MyGeneralListener())) {
                Toast.makeText(MyApplication.getInstance(), 
                        "BMapManager  初始化错误!", Toast.LENGTH_LONG).show();
            }
    }
    // 常用事件监听,用来处理通常的网络错误,授权验证错误等
    static class MyGeneralListener implements MKGeneralListener {
        
        @Override
        public void onGetNetworkState(int iError) {
            if (iError == MKEvent.ERROR_NETWORK_CONNECT) {
                Toast.makeText(MyApplication.getInstance(), "您的网络出错啦!",
                    Toast.LENGTH_LONG).show();
            }
            else if (iError == MKEvent.ERROR_NETWORK_DATA) {
                Toast.makeText(MyApplication.getInstance(), "输入正确的检索条件!",
                        Toast.LENGTH_LONG).show();
            }else {
                 Log.d("zttjiangqq", "iError="+iError);
            }
            // ...
        }

        @Override
        public void onGetPermissionState(int iError) {
            //非零值表示key验证未通过
            if (iError != 0) {
                //授权Key错误:
                Toast.makeText(MyApplication.getInstance(), 
                        "请在 DemoApplication.java文件输入正确的授权Key,并检查您的网络连接是否正常!error: "+iError, Toast.LENGTH_LONG).show();
                MyApplication.getInstance().m_bKeyRight = false;
            }
            else{
                MyApplication.getInstance().m_bKeyRight = true;
                Toast.makeText(MyApplication.getInstance(), 
                        "key认证成功", Toast.LENGTH_LONG).show();
            }
        }
    }
}

   2:创建一个FragmentMapDemo.java,继承于FragmentActivity,使用newInstance()方法获取SupportMapFragment,进行显示地图:

package com.ztt.baidumap.ui;

import android.content.res.Configuration;
import android.os.Bundle;
import android.support.v4.app.FragmentActivity;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;
import android.util.Log;

import com.baidu.mapapi.BMapManager;
import com.baidu.mapapi.map.MapController;
import com.baidu.mapapi.map.SupportMapFragment;
import com.baidu.platform.comapi.basestruct.GeoPoint;

public class FragmentMapDemo extends FragmentActivity {
    SupportMapFragment map;
    @Override
    public void onCreate(Bundle arg0) {
        /**
         * 使用地图sdk前需先初始化BMapManager.
         * BMapManager是全局的,可为多个MapView共用,它需要地图模块创建前创建,
         * 并在地图地图模块销毁后销毁,只要还有地图模块在使用,BMapManager就不应该销毁
         */
        MyApplication app = (MyApplication)this.getApplication();
        if (app.mBMapManager == null) {
            app.mBMapManager = new BMapManager(getApplicationContext());
            /**
             * 如果BMapManager没有初始化则初始化BMapManager
             */
            app.mBMapManager.init(MyApplication.strKey,new MyApplication.MyGeneralListener());
        }
        Log.d("zttjiangqq", "onCreate");
        setContentView(R.layout.activity_fragment);
        map = SupportMapFragment.newInstance();
        FragmentManager manager = getSupportFragmentManager();
        FragmentTransaction ft= manager.beginTransaction();
        ft.add(R.id.map, map, "map_fragment");
        ft.commit();
    }
    
    @Override
    protected void onRestoreInstanceState(Bundle savedInstanceState) {
        super.onRestoreInstanceState(savedInstanceState);
        Log.d("zttjiangqq", "onRestoreInstanceState");
    }

    @Override
    protected void onRestart() {
        super.onRestart();
        Log.d("zttjiangqq", "onRestart");
    }

    @Override
    public void onStart() {
        super.onStart();
        Log.d("zttjiangqq", "onStart");
    }

    @Override
    public void onResume() {
        super.onResume();
        Log.d("zttjiangqq", "onResume");
        // 得到MapView的控制权,可以用它控制和驱动平移和缩放  
        MapController controller = map.getMapView().getController();
        // 用给定的经纬度构造一个GeoPoint,单位是微度 (度 * 1E6)  并且设置设置地图中心点   (这边使用天安门)
        controller.setCenter(new GeoPoint((int)(39.945 * 1E6), (int)(116.404 * 1E6)));
        // 设置地图zoom级别  
        controller.setZoom(13);
    }

    @Override
    public void onSaveInstanceState(Bundle outState) {
        super.onSaveInstanceState(outState);
        Log.d("zttjiangqq", "onSaveInstanceState");
    }

    @Override
    public void onPause() {
        super.onPause();
        Log.d("zttjiangqq", "onPause");
    }

    @Override
    public void onStop() {
        super.onStop();
        Log.d("zttjiangqq", "onStop");
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        Log.d("zttjiangqq", "onDestory");
    }

    @Override
    public void onConfigurationChanged(Configuration newConfig) {
        super.onConfigurationChanged(newConfig);
        Log.d("zttjiangqq", "onConfigurationChanged");
    }
}

  3:布局文件:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical" >

    <FrameLayout
        android:id="@+id/map"
        android:layout_width="match_parent"
        android:layout_height="match_parent" />

</LinearLayout>

  (三)注意一定要添加配置权限

<uses-permission android:name="android.permission.MOUNT_UNMOUNT_FILESYSTEMS" />
    <uses-permission android:name="android.permission.BAIDU_LOCATION_SERVICE" />
    <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
    <uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
    <uses-permission android:name="android.permission.INTERNET" />
    <uses-permission android:name="android.permission.ACCES_MOCK_LOCATION" />
    <uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
    <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.ACCESS_GPS" />
    <!-- SDK1.5需要android.permission.GET_TASKS权限判断本程序是否为当前运行的应用? -->
    <uses-permission android:name="android.permission.GET_TASKS" />
    <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
    <uses-permission android:name="android.permission.BROADCAST_STICKY" />
    <uses-permission android:name="android.permission.WRITE_SETTINGS" />
    <!-- 来电消音 -->
    <uses-permission android:name="android.permission.PROCESS_OUTGOING_CALLS" />
    <uses-permission android:name="android.permission.READ_PHONE_STATE" />
    <uses-permission android:name="android.permission.MODIFY_AUDIO_SETTINGS" />
    <uses-permission android:name="android.permission.RECORD_AUDIO" />

 

posted @ 2015-08-23 23:59  小破孩123  阅读(642)  评论(0编辑  收藏  举报