Preface:
[Baidu map development II] Fragment-Based Map Framework use (blog address: http://blog.csdn.net/developer_jiangqq), reprint please note.
Author: hmjiangqq
Email: jiangqqlmj@163.com
In the previous article, we learned the most basic content of Baidu map (applying for the Key and configuring the preview map). Today we will continue to look at the use of the Fragment-Based Map Framework.
(1) Basic Introduction (Fragment and SupportMapFragment ):
The use of Fragment is now widely used in Android APP development. The Fragment name is "Fragment" and "Activity" has a similar lifecycle. It can be used to develop apps compatible with mobile phones and tablets, it is less compatible with the resolution version, improving work efficiency. Fragment can also contain view modules that control code and can be easily combined. Easy to use. For details about Fragment, click here (Introduction and basic use of Fragment on the official website. Next, let's take a look at the SupportMapFragment map framework provided by Baidu map API. SupportMapFragment is inherited from Fragment, so it has a complete lifecycle of Fragment. This framework is mainly used to manage the map lifecycle. There are two main methods: getMapView (): Get MapView reference, it must be used after the onCreateView function is called. 2: newInstance (); returns the SupportMapFragment object instance.
(2) usage:
The usage is similar to that when Fragment is used. First, obtain the SupportMapFragment instance and add the Fragment to the specified View. commit.
(3) code demonstration. The comments have been written in quite detail. You can check them directly.
1. Customize MyApplication. java to set global variables and verify the key:
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;/*** Custom Application for key identification and verification (using a singleton) * @ author Jiangqq * @ time 2014/03/15 */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 () applied for on the Baidu map Official Website () {return instance;} @ Overridepublic void onCreate () {super. onCreate (); instance = this; // initialize and verify the initEngineManager (this) when the APP starts );} /*** verify key * @ param pContext */private void initEngineM Anager (Context pContext) {if (mBMapManager = null) {mBMapManager = new BMapManager (pContext);} if (! MBMapManager. init (strKey, new MyGeneralListener () {Toast. makeText (MyApplication. getInstance (), "BMapManager initialization error! ", Toast. LENGTH_LONG ). show () ;}// common event listening, used to handle common network errors, authorization verification errors, and other static class MyGeneralListener implements MKGeneralListener {@ Override public void onGetNetworkState (int iError) {if (iError = MKEvent. ERROR_NETWORK_CONNECT) {Toast. makeText (MyApplication. getInstance (), "your network has an error! ", Toast. LENGTH_LONG). show ();} else if (iError = MKEvent. ERROR_NETWORK_DATA) {Toast. makeText (MyApplication. getInstance ()," enter the correct search conditions! ", Toast. LENGTH_LONG ). show ();} else {Log. d ("zttjiangqq", "iError =" + iError );}//...} @ Override public void onGetPermissionState (int iError) {// a non-zero value indicates that the key verification fails if (iError! = 0) {// Authorization Key error: Toast. makeText (MyApplication. getInstance (), "Please go to DemoApplication. enter the correct authorization Key in the java file and check whether your network connection is normal! Error: "+ iError, Toast. LENGTH_LONG ). show (); MyApplication. getInstance (). m_bKeyRight = false;} else {MyApplication. getInstance (). m_bKeyRight = true; Toast. makeText (MyApplication. getInstance (), "key Authentication successful", Toast. LENGTH_LONG ). show ();}}}}
2: Create a FragmentMapDemo. java inherited from FragmentActivity. Use the newInstance () method to obtain SupportMapFragment and display the map:
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; @ Overridepublic void onCreate (Bundle arg0) {/*** BMapManager needs to be initialized before using the map sdk. * BMapManager is global and can be shared by multiple mapviews. It needs to be created before the map module is created. * If the map module is still in use, BMapManager should not destroy */MyApplication app = (MyApplication) this. getApplication (); if (app. mBMapManager = null) {app. mBMapManager = new BMapManager (getApplicationContext ();/*** initialize BMapManager */app if BMapManager is not initialized. 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 () ;}@ Overrideprotected void onRestoreInstanceState (Bundle savedInstanceState) {super. onRestoreInstanceState (savedInstanceState); Log. d ("zttjiangqq", "onRestoreInstanceState") ;}@ Overrideprotected void onRestart () {super. onRestart (); Log. d ("zttjiangqq", "onRestart") ;}@ Overridepublic void onStart () {super. onStart (); Log. d ("zttjiangqq", "onStart") ;}@ Overridepublic void onResume () {super. onResume (); Log. d ("zttjiangqq", "onResume"); // obtain control of MapView, which can be used to control and drive translation and zoom MapController controller = map. getMapView (). getController (); // construct a GeoPoint with the given longitude and latitude, in the unit of (degree * 1E6) and set the map center point (Tiananmen Square here) controller. setCenter (new GeoPoint (int) (39.945*1E6), (int) (116.404*1E6); // sets the map zoom level controller. setZoom (13) ;}@ Overridepublic void onSaveInstanceState (Bundle outState) {super. onSaveInstanceState (outState); Log. d ("zttjiangqq", "onSaveInstanceState") ;}@ Overridepublic void onPause () {super. onPause (); Log. d ("zttjiangqq", "onPause") ;}@ Overridepublic void onStop () {super. onStop (); Log. d ("zttjiangqq", "onStop") ;}@ Overridepublic void onDestroy () {super. onDestroy (); Log. d ("zttjiangqq", "onDestory") ;}@ Overridepublic void onConfigurationChanged (Configuration newConfig) {super. onConfigurationChanged (newConfig); Log. d ("zttjiangqq", "onConfigurationChanged ");}}
3: Layout file:
<?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>
(3) be sure to add the configuration permission.
<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_S TATE "/> <uses-permission android: name =" android. permission. ACCESS_GPS "/> <! -- SDK1.5 requires the permission of android. permission. GET_TASKS to determine whether the program is the currently running application? --> <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 "/> <! -- Call cancel --> <uses-permission android: name = "android. permission. process_outgoing_cils "/> <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 "/>