Call Baidu Map in Android

Source: Internet
Author: User

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

    1. <application
    2. Android:allowbackup= "true"
    3. android:icon= "@drawable/ic_launcher"
    4. Android:label= "@string/app_name"
    5. Android:theme= "@style/apptheme" >
    6. <meta-data
    7. Android:name= "Com.baidu.lbsapi.API_KEY"
    8. Android:value= "Fill in your application for AK"/>

Permissions:

[Java] view plain copy

  1. <!--Baidu API requires the right---
  2. <uses-permission android:name= "Android.permission.GET_ACCOUNTS"/>
  3. <uses-permission android:name= "Android.permission.USE_CREDENTIALS"/>
  4. <uses-permission android:name= "Android.permission.MANAGE_ACCOUNTS"/>
  5. <uses-permission android:name= "Android.permission.AUTHENTICATE_ACCOUNTS"/>
  6. <uses-permission android:name= "Android.permission.ACCESS_NETWORK_STATE"/>
  7. <uses-permission android:name= "Android.permission.INTERNET"/>
  8. <uses-permission android:name= "Com.android.launcher.permission.READ_SETTINGS"/>
  9. <uses-permission android:name= "Android.permission.CHANGE_WIFI_STATE"/>
  10. <uses-permission android:name= "Android.permission.ACCESS_WIFI_STATE"/>
  11. <uses-permission android:name= "Android.permission.READ_PHONE_STATE"/>
  12. <uses-permission android:name= "Android.permission.WRITE_EXTERNAL_STORAGE"/>
  13. <uses-permission android:name= "Android.permission.BROADCAST_STICKY"/>
  14. <uses-permission android:name= "Android.permission.WRITE_SETTINGS"/>
  15. <uses-permission android:name= "Android.permission.READ_PHONE_STATE"/>

3. Add a map control to the layout file:

[Java] view plain copy

    1. <com.baidu.mapapi.map.mapview
    2. Android:id= "@+id/bmapview"
    3. Android:layout_width= "Match_parent"
    4. android:layout_height= "Match_parent"
    5. Android:clickable= "true"/>

4. Initialize the context global variable referenced by the SDK at the time of application creation.

[Java] view plain copy

    1. @Override
    2. protected void OnCreate (Bundle savedinstancestate) {
    3. Super.oncreate (savedinstancestate);
    4. Requestwindowfeature (Window.feature_no_title);
    5. //
    6. Sdkinitializer.initialize (Getapplicationcontext ());
    7. Setcontentview (R.layout.activity_main);
    8. Init ();
    9. }

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

  1. Package Com.bdmap.view;
  2. Import Com.baidu.mapapi.SDKInitializer;
  3. Import Com.baidu.mapapi.map.BaiduMap;
  4. Import Com.baidu.mapapi.map.MapView;
  5. Import android.app.Activity;
  6. Import Android.os.Bundle;
  7. Import Android.view.View;
  8. Import Android.view.Window;
  9. public class Mainactivity extends Activity {
  10. Baidu Map Control
  11. Private Mapview Mmapview = null;
  12. Baidu Map Object
  13. Private Baidumap Bdmap;
  14. @Override
  15. protected void OnCreate (Bundle savedinstancestate) {
  16. Super.oncreate (savedinstancestate);
  17. Requestwindowfeature (Window.feature_no_title);
  18. //
  19. Sdkinitializer.initialize (Getapplicationcontext ());
  20. Setcontentview (R.layout.activity_main);
  21. Init ();
  22. }
  23. /**
  24. * Initialization method
  25. */
  26. private void init () {
  27. Mmapview = (Mapview) Findviewbyid (R.id.bmapview);
  28. }
  29. @Override
  30. protected void Onresume () {
  31. Super.onresume ();
  32. Mmapview.onresume ();
  33. }
  34. @Override
  35. protected void OnPause () {
  36. Super.onpause ();
  37. Mmapview.onpause ();
  38. }
  39. @Override
  40. protected void OnDestroy () {
  41. Mmapview.ondestroy ();
  42. Mmapview = null;
  43. Super.ondestroy ();
  44. }
  45. }

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

    1. Mmapview = (Mapview) Findviewbyid (R.id.bmapview);
    2. Mbaidumap = Mmapview.getmap ();
    3. General Map
    4. Mbaidumap.setmaptype (Baidumap.map_type_normal);
    5. Satellite map
    6. Mbaidumap.setmaptype (Baidumap.map_type_satellite);
Iv. displaying real-time traffic map (road map)

[Java] view plain copy

    1. Open Traffic Map
    2. 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

    1. Open Heat Force diagram
    2. Mbaidumap.setbaiduheatmapenabled (TRUE);

Call Baidu Map in Android

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.