Android Studio displays Baidu map (detailed explanation)

Keywords: Android Studio

  1. Get Baidu Key
  2. Resource introduction
  3. Integrate Baidu map SDK in the project
  4. Display map

1.1. Register baidu account and log in

1.2. Create application

Write SHA1, select 1 from 2 for release version and development version, or select all. The acquisition method of development version SHA1 is provided here

(1) Open Terminal under Android Studio
(2) Enter command c: enter Disk c;
(3) Then enter the command cd Users\Administrator, where Administrator is your own user name. I use 64311 here. There is an. adnroid file under the Administrator file. Enter the command CD. android, and finally find. android (if. android is not found, it may have been set and changed during installation)
(4) Enter the command keytool -list -v -keystore debug.keystore, where debug.keystore is the default keystore of studio. Press enter, and then enter the secret key: android (system default) enter (the secret keystore password is invisible), as shown in the following figure:

After input, the web page will automatically check whether the format is correct
If an error is reported, check whether there are spaces before and after

Then find the project package name com.smmer.test

In this way, the application is successfully established

2.1 resource download

The download is complete, and the contents of the file are as follows

2.2 resource import
(1) First change Android to project view

(2) Open the unzipped development package folder, find the file and copy it to the app/libs directory of the project, as shown in the figure
(3) In the libs directory, select each jar file (there is only one BaiduLbs_Android.jar here), right-click and select Add As Library..., as shown in the figure:

(4) At this time, you will find that the corresponding description of the jar file that the project depends on is generated in the dependencies block of build.gradle in the app directory, as shown below:

3.1 configuring the AndroidManifest.xml file
Add the following code to configure the development key (AK):

        android:value="developer key" />  

Here, the name is changed to the package name
value is changed to the key (AK) applied by Baidu map open platform

Add the following permission statement outside the application:

<!-- Access the network and request map related business data, including map data, route planning, POI Search, etc -->
<uses-permission android:name="android.permission.INTERNET" />
<!-- Obtain the network state, and perform data request network conversion according to the network state switching -->
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<!-- Read external storage. If developers use so Dynamic loading function and so If the file is placed in an external storage area, you need to apply for the permission, otherwise you don't need it -->
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
<!-- Write external storage. If the developer uses an offline map and the data is written in the external storage area, you need to apply for this permission -->
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />

Thus, the AndroidManifest.xml file configuration is complete

3.2 adding a map container to a layout file

        android:clickable="true" />

3.3 map initialization
Create a custom Application and complete SDK initialization in its onCreate method.

public class DemoApplication extends Application {
    public void onCreate() {
        //Before using SDK components, initialize the context information and pass in ApplicationContext
        //Since 4.3.0, all interfaces of Baidu map SDK support Baidu coordinates and National Survey Bureau coordinates. Use this method to set the coordinate type you use
        //It includes BD09LL and GCJ02 coordinates. The default is BD09LL coordinates.

Declare the Application in the AndroidManifest.xml file

3.4 create map Activity

public class MainActivity extends Activity {
    private MapView mMapView = null;

    protected void onCreate(Bundle savedInstanceState) {
        mMapView = (MapView) findViewById(;

    protected void onResume() {
        //When the activity executes onResume, execute mMapView. onResume() to realize map life cycle management
    protected void onPause() {
        //When the activity executes onPause, execute mMapView. onPause() to realize map life cycle management
    protected void onDestroy() {
        //When the activity executes onDestroy, execute mMapView.onDestroy() to realize map life cycle management

If an error is reported, you need to


In this way, the function is realized.

Posted by jumpingmatflash on Mon, 29 Nov 2021 13:24:02 -0800