寫在前面的話: 【百度地圖開發之二】基於Fragment的地圖框架的使用(博客地址:http://blog.csdn.net/developer_jiangqq),轉載請註明。html
Author:hmjiangqq Email:jiangqqlmj@163.com
上一篇咱們學習了百度地圖最基礎的內容(申請Key和配置初覽顯示地圖),今天繼續來看一下基於Fragment的地圖框架的使用。java
(一)基本介紹(Fragment和SupportMapFragment): Fragment的使用如今安卓APP開發中用的比較多了,Fragment名稱爲碎片和Activity有着類似的生命管理週期,基本做用能夠進行開發兼容手機和平板的app,較少兼容分辨率版本的工做量,提升工做效率。其中Fragment仍是能夠包含控制代碼的視圖模塊,能夠很是方便的進行組合。使用起來很方便。具體有個Fragment的介紹和使用請點擊查看(官網Fragment的介紹和基本使用),這邊就很少講了。下面咱們來看下百度地圖API給咱們提供的地圖框架SupportMapFragment。SupportMapFragment是繼承於Fragment所以具有Fragment的完成的生命週期,該框架主要用來管理地圖生命週期,主要有下面兩個方法1:getMapView():獲取MapView引用,要在onCreateView函數調用以後進行使用.2:newInstance();返回獲取SupportMapFragment對象實例。android
(二)使用方法: 使用方法和咱們平時用Fragment的時候差很少,首先獲取SupportMapFragment實例,而後把該Fragment添加到指定View視圖圖.commit一下便可.api
(三)代碼演示,註釋基本已經寫了挺詳細了,直接看就OK了 1.自定義MyApplication.java設置全局變量,而且進行校驗密鑰:網絡
[java] view plain copy package com.ztt.baidumap.ui;app
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;
/**ide
自定義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,進行顯示地圖:
[java] view plain copy 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:佈局文件:
[html] view plain copy
<?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>
(三)注意必定要添加配置權限
[html] view plain copy <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" />