Reprint Please specify Source: http://blog.csdn.net/crazy1235/article/details/42614603
Do about the location or positioning of the app can not avoid the use of map function, I recently because of the needs of the project needs to use some of the features of Baidu map, so these days to study a bit, now write a blog record, welcome everyone to comment!
first, apply for AK (API Key)
To use the Baidu Map SDK, you must apply for a Baidu Map API key. The application process is quite simple.
First register as a developer of Baidu, then open Http://lbsyun.baidu.com/apiconsole/key this URL, add the application:
The most important step in creating an app is the "Security Code". The security code is composed of a "digital signature" and a ";" and a "package name". The package name is the structure of the package for the project you are creating, which refers to the value of the packages under the manifest tag in androidmanifest.xml.
The digital signature refers to the SHA1 value of the Android signing certificate.
There are two ways to get a digital signature:
1. The first method: use Eclipse to view.
Open Eclipse's Preferences menu and you can see the values of SHA1 in "Build" under Android, such as:
2. The second method: Use the Keytool tool (JDK comes with) to view.
Under Console, enter "CD. Android" and enter "Keytool-list-v-keystore debug.keystore" To enter, then prompt you for "keystore password", enter "Android" and then the SHA1 value will be displayed.
The digital signature is done, and then the app is created OK. Once created, the application list will display the corresponding AK, which is the API key.
ii. Download SDK Development Kit
Open the Http://developer.baidu.com/map/index.php?title=androidsdk/sdkandev-download URL Download SDK, you can download it all, or you can customize the download. From the post-V2.3.0 version, SDK development packages are available for download in a customizable form, and users can download the SDK development package according to their own project needs by ticking the appropriate features.
third, in the Android project to cite the Baidu SDK
1. Add the jar packages and so files from the development package to the Libs file.
2. Add the development key and required permissions in Androidmanifest.xml.
<application android:allowbackup= "true" android:icon= "@drawable/ic_launcher" android:label= "@ String/app_name " android:theme=" @style/apptheme "> <meta-data android:name=" Com.baidu.lbsapi.API_KEY " android:value=" fill in your application for AK "/>
Permissions:
<!--Baidu API required---<uses-permission android:name= "Android.permission.GET_ACCOUNTS"/> <uses-permissio n android:name= "Android.permission.USE_CREDENTIALS"/> <uses-permission android:name= " Android.permission.MANAGE_ACCOUNTS "/> <uses-permission android:name=" android.permission.AUTHENTICATE_ ACCOUNTS "/> <uses-permission android:name=" Android.permission.ACCESS_NETWORK_STATE "/> <uses-permissio n android:name= "Android.permission.INTERNET"/> <uses-permission android:name= " Com.android.launcher.permission.READ_SETTINGS "/> <uses-permission android:name=" android.permission.CHANGE_ Wifi_state "/> <uses-permission android:name=" Android.permission.ACCESS_WIFI_STATE "/> <uses-permission Android:name= "Android.permission.READ_PHONE_STATE"/> <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.READ_PHONE_STATE "/>
3. Add a map control to the layout file:
<com.baidu.mapapi.map.mapview android:id= "@+id/bmapview" android:layout_width= "Match_parent " android:layout_height= "Match_parent" android:clickable= "true"/>
4. Initialize the context global variable referenced by the SDK at the time of application creation.
@Overrideprotected void OnCreate (Bundle savedinstancestate) {super.oncreate (savedinstancestate); Requestwindowfeature (Window.feature_no_title);//sdkinitializer.initialize (Getapplicationcontext ()); Setcontentview (R.layout.activity_main); init ();}
Note here: The Initialize method must pass in the ApplicationContext, passing in this, Or mainactivity.this can not, or will report run-time exception, so Baidu proposed to put this method in application initialization method.
then rewrite the life cycle of the activity by several methods to manage the life cycle of the map. The Onreusme, OnPause and Ondestory methods of Mapview are implemented in the Onresume, OnPause and Ondestory methods of the activity respectively.
Package Com.bdmap.view;import Com.baidu.mapapi.sdkinitializer;import Com.baidu.mapapi.map.baidumap;import Com.baidu.mapapi.map.mapview;import Android.app.activity;import Android.os.bundle;import Android.view.View;import Android.view.window;public class Mainactivity extends Activity {//Baidu Map control private Mapview Mmapview = null;//Baidu Map Object Private Baidumap bdmap; @Overrideprotected void OnCreate (Bundle savedinstancestate) {super.oncreate (savedinstancestate); Requestwindowfeature (Window.feature_no_title);//sdkinitializer.initialize (Getapplicationcontext ()); Setcontentview (R.layout.activity_main); init ();} /** * Initialization method */private void init () {Mmapview = (Mapview) Findviewbyid (R.id.bmapview);} @Overrideprotected void Onresume () {super.onresume (); Mmapview.onresume ();} @Overrideprotected void OnPause () {super.onpause (); Mmapview.onpause ();} @Overrideprotected void OnDestroy () {Mmapview.ondestroy (); Mmapview = Null;super.ondestroy ();}}
complete the above steps to complete a simple "Hello Map" program at this point.
third, general map and satellite map switch
Baidu maps the types of maps into two categories: normal vector maps and satellite maps.
Mmapview = (Mapview) Findviewbyid (R.id.bmapview); Mbaidumap = Mmapview.getmap (); General map Mbaidumap.setmaptype (baidumap.map_type_normal); Satellite map Mbaidumap.setmaptype (baidumap.map_type_satellite);
Iv. displaying real-time traffic map (road map)
Turn on traffic map mbaidumap.settrafficenabled (true);
V. Display of heat Force diagram
The Heat force diagram is a graphical representation of the area of the page that the visitor is passionate about and the geographic area where the visitor is located in a special highlight. In layman's terms is the intensity of the person who shows the area on the map. Similar to the following:
Turn on thermal diagram mbaidumap.setbaiduheatmapenabled (true);
Android Baidu Map Development (a) the first experience