Android stduio develops Baidu map API version 4.5 (1)

First of all, I won't write about the application key. The Baidu api documentation explains it very clearly.

This is the first time I have come into contact with Baidu's api. To be honest, it took 3 days to get it done, and then I just finished it today. I thought it was a simple matter. Who knows that there are more problems with Android stduio than one, and it was completed today. .

It took 3 days to create a usable one.

Now the detailed methods and problems that arise are written to facilitate everyone's learning.

First we need to import the package


Then right-click AS Libaray and add this package to use it.


You also need to import the jniLibs file, but this file is not created by yourself, it can be created through gradle.


enter code

The following code requires

  sourceSets {
        main {
            jniLibs.srcDir 'libs'
        }
It must be written in android, and it needs to be written in build.gradle(Module:app).
android {
    signingConfigs {
        config {
        }
        mykey {
            keyAlias ​​'****'
            keyPassword '**'
            storePassword '***'
        }
    }
    sourceSets {
        main {
            jniLibs.srcDir 'libs'
        }
    }


In order to create the file jiniLibs, and then put the Baidu API except the jar package into jniLibs, this problem has troubled me for a long time.

Registration is required initially

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.minicard.myapplication">
    <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.WAKE_LOCK"/>
    <uses-permission android:name="android.permission.CHANGE_WIFI_STATE" />
    <uses-permission android:name="android.permission.ACCESS_WIFI_STATE" />
    <uses-permission android:name="android.permission.GET_TASKS" />
    <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
    <uses-permission android:name="android.permission.WRITE_SETTINGS" />
    <!-- Permission to access precise location -->
    <uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
    <!-- This permission is used for network positioning-->
    <uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION"/>
    <!-- Get operator information to support the interface related to providing operator information-->
    <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"/>
    <application
        android:allowBackup="true"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:roundIcon="@mipmap/ic_launcher_round"
        android:supportsRtl="true"
        android:theme="@style/AppTheme">
        <meta-data
            android:name="com.baidu.lbsapi.API_KEY"
            android:value="your Baidu key" />
        <activity android:name=".MainActivity">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
    </application>



The first step is to write a layout xml UI diagram to facilitate the mapview

<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context="com.minicard.myapplication.MainActivity">

    <com.baidu.mapapi.map.MapView
        android:id="@+id/baiduMapView"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:clickable="true"
        />

</android.support.constraint.ConstraintLayout>
ConstraintLayout is used here if it appears
   tools:layout_editor_absoluteY="82dp"
        tools:layout_editor_absoluteX="16dp"
The above two lines of code are used for absolute positioning, but we don't need to use it for the time being, and an error will be reported. Although it will not work after calling others, let's delete these two lines of code first.

Then let's write the main code of Mainactivity

package com.minicard.myapplication;

import android.app.Activity;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;

import com.baidu.mapapi.SDKInitializer;
import com.baidu.mapapi.map.MapView;

public class MainActivity extends Activity {
    private MapView mBaiduMapView = null;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate (savedInstanceState);
        SDKInitializer.initialize(getApplicationContext());
        setContentView(R.layout.activity_main);
        initBaiduMap();
    }

    private void initBaiduMap(){
        mBaiduMapView = (MapView) findViewById(R.id.baiduMapView);
    }
    @Override
    protected void onResume() {
        super.onResume();
        mBaiduMapView.onResume();
    }
    @Override
    protected void onPause() {
        super.onPause();
        mBaiduMapView.onPause();
    }
    @Override
    protected void onDestroy() {
        mBaiduMapView.onDestroy ();
        mBaiduMapView = null;
        super.onDestroy ();
    }
}
Note two points,
1. The ApplicationContext must be passed in the initialize method. If this or MAinActivity.this is passed in, a runtime exception will be reported.
2. The initialize method must be written before the setContentView method, and may be written in the result report xml later mistake.


Then our Baidu map api can be used, but there are still some things that have not come out.


After inspection, it was found that the key and the corresponding SHA1 did not correspond to Baidu. The reason is that during the writing process, the SHA1 suddenly became different from the one you created before, resulting in the map being invisible and grid-like. Another point is that the virtual machine may not be available, and it needs to be tested with a real machine.


Alright, so we're done with the first phase.


Below is the source code for you to download

http://download.csdn.net/download/qq_16519957/10040465


Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325571630&siteId=291194637