Onecall Baidu Map--the first method
1. Download Baidu Map SDK
The SDK can be split to download, you need to use that part of the function to download the corresponding SDK, such as
The core jar and so package, placed in the Libs directory of the Project
2. Apply for key
Registered Baidu account Application key, the format is as follows: SHA1 value + ";" + package name.
Package name refers to the name defined in Androidmanifest.xml, not the project package name
3, Baidu Map to join the project
(1) Add a development key in application
<application <meta-data
Android:name= "Com.baidu.lbsapi.API_KEY"
Android:value= "Application Key"/>
</application>
(2) Add the required permissions
<uses-permission android:name= "Android.permission.CHANGE_WIFI_STATE"/>
<uses-permission android:name= "Android.permission.ACCESS_FINE_LOCATION"/>
<uses-permission android:name= "Android.permission.ACCESS_LOCATION_EXTRA_COMMANDS"/>
<uses-permission android:name= "Android.permission.CHANGE_NETWORK_STATE"/>
<uses-permission android:name= "Android.permission.GET_TASKS"/>
<uses-permission android:name= "Android.permission.MOUNT_UNMOUNT_FILESYSTEMS" >
</uses-permission>
<uses-permission android:name= "Android.permission.WRITE_EXTERNAL_STORAGE"/>
<uses-permission android:name= "Android.permission.RECEIVE_BOOT_COMPLETED"/>
(3) Add Baidu Map Service
Configure the Map service in the Application tab of Mainfest
<service
Android:name= "COM.BAIDU.LOCATION.F"
Android:enabled= "true"
android:process= ": Remote" >
</service>
(4) Adding a map control in the layout XML file
(5) Initializing the context global variables referenced by the SDK when the application is created
public class Mainactivity extends Activity { mapview Mmapview = null;& nbsp; @Override protected void onCreate (Bundle savedinstancestate) {super.oncreate ( Savedinstancestate); Initialize the context information before using each component of the SDK, passing in applicationcontext //Note that the method is implemented before Setcontentview the method sdkinitializer.initialize (Getapplicationcontext ()); Setcontentview (R.layout.activity_main); Get map Control Reference Mmapview = (mapview) Findviewbyid (R.id.bmapview); } @Override protected void OnDestroy () {Super.ondestroy ();// Perform Mmapview.ondestroy () when activity executes OnDestroy to achieve map lifecycle management Mmapview.ondestroy (); } @Override protected void Onresume () {super.onresume ();// Execute m when activity executes OnresumeMapview. Onresume () To achieve Map lifecycle Management mmapview.onresume (); } @Override protected void OnPause () {super.onpause ();// Executes Mmapview when the activity executes OnPause. OnPause () To achieve Map lifecycle Management mmapview.onpause (); } }
After running, you can get Baidu map layer
4. Positioning the current position
1. Turn on the positioning layer
Turn on positioning layers
Mbaidumap.setmylocationenabled (TRUE);
2. Open Positioning function
Mlocclient = new Locationclient (context);
Mlocclient.registerlocationlistener (MyListener);
locationclientoption option = new Locationclientoption ();
Option.setopengps (TRUE);//Turn on GPS
Option.setcoortype ("Bd09ll"); Set the coordinate type
Option.setscanspan (1000);
mlocclient.setlocoption (option);
Mlocclient.start ();
3. Rewrite the Listener
Inherit the Bdlocationlistener listener and rewrite the Onreceivelocation method so that you can get the location information contained in the Bdlocation, see Bdlocation class
4. Positioning the current position
/**
* Set Map Operation Point Center
*/
public void Setcenterposition (point P) {
LATLNG ll = new Latlng (double.parsedouble (P.getx ()),
Double.parsedouble (P.gety ()));
Mapstatusupdate u = mapstatusupdatefactory.newmapstatus (new Mapstatus.builder (). Target (LL). Build ()) ;
Mbaidumap.setmapstatus (U);
Animated mode Toggle
Mbaidumap.animatemapstatus (U);
}
Use the factory class to update the map status and display the current location.
5, using the Baidu icon to paint
You can view the relevant API documentation by plotting basic graphics on the Baidu map.
In this project, a circle, a line, a point are plotted. (Overlayoptions interface class, usually drawn with its subclasses)
1. Plotting lines
Overlayoptions oopolyline = new Polylineoptions (). Width (width)
. Color. points (points);
Mbaidumap.addoverlay (Oopolyline);
2. Plotting Circle
LATLNG llcircle = new Latlng (double.parsedouble (Centerpoint.getx ()),
Double.parsedouble (Centerpoint.gety ()));
Overlayoptions oocircle = new Circleoptions (). FillColor (FillColor)
. Center (llcircle). Stroke (New stroke (5, color))
. Radius (RAD);
Mbaidumap.addoverlay (oocircle);
3, plotting points (not the basic method)
LATLNG ll = new Latlng (double.parsedouble (Centerpoint.getx ()),
Double.parsedouble (Centerpoint.gety ()));
Overlayoptions OoA = new markeroptions (). Position (LL). Icon (BD)
. ZIndex (9);
Return (Marker) Mbaidumap.addoverlay (OoA);
II. Application for AK (API Key)--the second method
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.
[Java] view plain copy
- <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:
[Java] view plain copy
- <!--Baidu API requires the right---
- <uses-permission android:name= "Android.permission.GET_ACCOUNTS"/>
- <uses-permission 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-permission 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:
[Java] view plain copy
- <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.
[Java] view plain copy
- @Override
- protected 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.
[Java] view plain copy
- 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;
- @Override
- protected 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);
- }
- @Override
- protected void Onresume () {
- Super.onresume ();
- Mmapview.onresume ();
- }
- @Override
- protected void OnPause () {
- Super.onpause ();
- Mmapview.onpause ();
- }
- @Override
- protected 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.
[Java] view plain copy
- 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)
[Java] view plain copy
- Open 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:
[Java] view plain copy
- Open Heat Force diagram
- Mbaidumap.setbaiduheatmapenabled (TRUE);
Call Baidu Map in Android