Show Baidu map in Android project

Maps are used in many apps. The function of Baidu Maps has been used in recent projects. Below I will share the steps to load Baidu Maps in the App.

1. First log in to the Baidu map open platform, if not, register a Baidu account first.

2. After logging in successfully, click on the top of the Android SDK section in the development document and click "Use Now".

3. Click to create an application, as shown in the following figure Insert picture description here: 1. Fill in the application name, 2. Select the Android SDK for the application type, check the services you need to use, 3. Fill in the package name: (Note that it is in the applicationId in build.gradle Package name), 4. Fill in SHA1, if you don’t know how to get it, please refer to: https://blog.csdn.net/qq77485042/article/details/82218933

4. After clicking submit, the security code is generated, and the AK in the application list can be copied.

5. Click the product download: as shown in the figure below:, Insert picture description hereselect the function you need to download the corresponding SDK, and then click the development package. Waiting to download.

6. After decompressing the compressed package, the directory is as follows: Insert picture description here1. Copy the jar package to the project libs, 2. Create a folder called jniLibs at the same level as the java directory in the main directory, and install the rest Copy all the folders in the so library. (Note: But this project will not automatically load the so file under libs, you need to configure the path of the so file to the libs path, associate all the so files of the map SDK, that is, add the code in the bulid.gradle under the app folder: jniLibs .srcDir'libs', the detailed code is as follows:

sourceSets{
  main{
       jniLibs.srcDir 'libs'  
       //说明so的路径为该libs路径,关联所有地图SDK的so文件
      }
}

7. Add in the build.gradle folder:

dependencies{
  compile files('libs/BaiduLBS_Android.jar')
}

8. Add the secret key to the application in the manifest file (AndroidManifest) as follows:

<application>  
    <meta-data  
        android:name="com.baidu.lbsapi.API_KEY"  
        android:value="刚在百度开放平台生成的安全码" />  
</application>

9. Add the required permissions:

<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"/> 
//获取设备网络状态,禁用后无法获取网络状态
<uses-permission android:name="android.permission.INTERNET"/> 
//网络权限,当禁用后,无法进行检索等相关业务
<uses-permission android:name="android.permission.READ_PHONE_STATE" /> 
//读取设备硬件信息,统计数据
<uses-permission android:name="com.android.launcher.permission.READ_SETTINGS" /> 
//读取系统信息,包含系统版本等信息,用作统计
<uses-permission android:name="android.permission.ACCESS_WIFI_STATE" /> 
//获取设备的网络状态,鉴权所需网络代理
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/> 
//允许sd卡写权限,需写入地图数据,禁用后无法显示地图
<uses-permission android:name="android.permission.WRITE_SETTINGS" /> 
//获取统计数据
<uses-permission android:name="android.permission.CAMERA" />  
//使用步行AR导航,配置Camera权限

10. Reference MapView in the layout:

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

11. Add in the initialization method of Application:

//在使用SDK各组件之前初始化context信息,传入ApplicationContext   
        SDKInitializer.initialize(this);
        //自4.3.0起,百度地图SDK所有接口均支持百度坐标和国测局坐标,用此方法设置您使用的坐标类型.
        //包括BD09LL和GCJ02两种坐标,默认是BD09LL坐标。
        SDKInitializer.setCoordType(CoordType.BD09LL);

12. Display the map in Activity:

private MapView mMapView;
private BaiduMap mBaiduMap;
mMapView = (MapView) findViewById(R.id.mmap);
mBaiduMap = mMapView.getMap();

In this way, the map is successfully displayed.

13. Optimization, you can call the same life cycle method of the map in each corresponding life cycle method of Activity. such as:

mMapView.onDestroy();  
 mMapView.onResume();  
 mMapView.onPause();

If you need to use code obfuscation, add the following code obfuscation:

-keep class com.baidu.** {*;}
-keep class mapsdkvi.com.** {*;}    
-dontwarn com.baidu.**

OK, after completing the above steps, you can see the map displayed on the mobile phone after running the project. Some further operations on the map will be discussed later.

Guess you like

Origin blog.csdn.net/qq77485042/article/details/83624877
Recommended