Implementation of Baidu Map with Android

Keywords: Android SDK network Attribute

Regarding the registration of Baidu Open Platform, the download of SDK and the application of APK, it is almost the same as Gaode. Let's get to the point and see how to use Baidu Map SDK to achieve positioning function.

1. Configuration environment

  • Import required jar packages and so library files
    Baidu Map SDK Click to download
    Baidu Map Location SDK Click to download
    After downloading and decompressing, the jar and so needed are imported into the project, as shown in the figure:

    The jar package is in the lib folder, and the so file is in the armeabi folder. Then for each jar file, right-click - select Add As Library.
  • Configure the use of so in build.gradle:
    sourceSets {
        main() {
            jniLibs.srcDirs = ['libs']
        }
    }
  • Set up Android Manifest. XML
    • Declare service components in the application tag, each app has its own location
    • Declare permissions
    • Setting AcessKey
 <service
            android:name="com.baidu.location.f"
            android:enabled="true"
            android:process=":remote"/>
 <! - This permission is used for network location - >.
    <uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION"/>
    <! - This permission is used to access GPS positioning - >.
    <uses-permission android:name="android.permission.ACCESS_FINE_LOCATION"/>
    <! - Used for accessing wifi network information, wifi information will be used for network positioning - >
    <uses-permission android:name="android.permission.ACCESS_WIFI_STATE"/>
    <! - Access to operator information to support the provision of operator information-related interfaces - >
    <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"/>
    <! - This permission is used to gain access to wifi, and WiFi information is used to locate the network - >.
    <uses-permission android:name="android.permission.CHANGE_WIFI_STATE"/>
    <! - Used to read the current status of the mobile phone - > ___________
    <uses-permission android:name="android.permission.READ_PHONE_STATE"/>
    <! - Write to Extended Storage, Write to Extended Card, Write to Offline Location Data - > Write to Extended Card.
    <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
    <! - Access to the network, network positioning needs access to the Internet - >
    <uses-permission android:name="android.permission.INTERNET" />
    <! - SD Card Read Rights, Users Write Offline Location Data - >
    <uses-permission android:name="android.permission.MOUNT_UNMOUNT_FILESYSTEMS"/>
    <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.WRITE_EXTERNAL_STORAGE" />
    <uses-permission android:name="android.permission.WRITE_SETTINGS" />
    <uses-permission android:name="android.permission.GET_TASKS" />

[Important Reminder]
After locating SDKv3.1, the following permissions are no longer required. Please cancel the declaration, otherwise the application installation will fail due to the enhanced permission management of Android 5.0 account system. <uses-permission and roid: name="android.permission.BAIDU_LOCATION_SERVICE""/

<meta-data
            android:name="com.baidu.lbsapi.API_KEY"
            android:value="Developer's application key" />

2. Implementing Location View

Before we can get the location, we need to display the map. First add map control to layout xml file, then create map Activity to manage map life cycle. (For reference Location of Gaud Map by SDK This is not much to say, now is mainly to achieve the location function of Baidu Map SDK.
The first step is to initialize the LocationClient class

 //Get a map control reference
        mMapView = (MapView) findViewById(R.id.bmapView);
        mLocationClient = new LocationClient(getApplicationContext()); //Declare the LocationClient class
        mLocationClient.registerLocationListener(this);//Registered listener function
        initLocation();
        // Open Location Layer
        mBaiduMap = mMapView.getMap();
        mBaiduMap.setMyLocationEnabled(true);//Display the location layer and trigger the location, flase by default
        mLocationClient.start();//Open positioning

The second step is to configure the positioning SDK parameters
Setting positioning parameters include: positioning mode (high-precision positioning mode, low-power positioning mode and device-only positioning mode), return coordinate type, whether to open GPS, whether to return address information, position semantics information, POI information and so on.
LocationClientOption class, which is used to set the location of SDK:

 private void initLocation() {
        LocationClientOption option = new LocationClientOption();
        option.setLocationMode(LocationClientOption.LocationMode.Hight_Accuracy
        );//Optional, default high precision, setting location mode, high precision, low power consumption, only equipment
        option.setCoorType("bd09ll");//Optional, default gcj02, set the coordinate system of the returned location result
        int span = 1000;
        option.setScanSpan(span);//Optional, default0,That is to say, locate only once, and set the interval between requests to initiate locations to be greater than or equal to1000ms That's what works.
        option.setIsNeedAddress(true);//Optional. Set whether address information is required. No address information is required by default.
        option.setOpenGps(true);//Optional, defaultfalse,Set whether to use gps
        option.setLocationNotify(true);//Optional, defaultfalse,Whether the settings are appropriate or not gps When valid, according to1S1 Subfrequency output GPS Result
        option.setIsNeedLocationDescribe(true);//Optional, defaultfalse,Set whether location semantics results are needed, and you can BDLocation.getLocationDescribe The result is similar to "near Tian'anmen in Beijing".
        option.setIsNeedLocationPoiList(true);//Optional, defaultfalse,Set whether you need to POI As a result, the BDLocation.getPoiList Get inside
        option.setIgnoreKillProcess(false);//Optional, defaulttrue,Location SDK Inside is a SERVICE,And put it in a separate process, set whether or not it is stop When killing this process, default does not kill
        option.SetIgnoreCacheException(false);//Optional, defaultfalse,Set whether to collect CRASH Information, default collection
        option.setEnableSimulateGps(false);//Optional, defaultfalse,Set whether filtering is required gps Simulation results, default requirements
        mLocationClient.setLocOption(option);
    }

High-precision positioning mode: In this positioning mode, both network positioning and GPS positioning will be used, giving priority to returning the highest accuracy positioning results;
Low power positioning mode: In this positioning mode, GPS will not be used, but network positioning (Wi-Fi and base station positioning);
Only use the equipment positioning mode: this positioning mode does not need to connect the network, only use GPS positioning, this mode does not support indoor environment positioning.
The third step is to implement the BDLocationListener interface and implement the unfinished method.

public class MainActivity extends Activity implements BDLocationListener {

 @Override
    public void onReceiveLocation(BDLocation location) {

        MyLocationData locData = new MyLocationData.Builder()
                .accuracy(location.getRadius())
                // Here set the direction information that the developer gets, clockwise0-360
                .direction(100).latitude(location.getLatitude())
                .longitude(location.getLongitude()).build();
        // Setting location data
        mBaiduMap.setMyLocationData(locData);
        if (isFirstLoc) {
            LatLng ll = new LatLng(location.getLatitude(), location.getLongitude());
            MapStatusUpdate update = MapStatusUpdateFactory.newLatLngZoom(ll, 16);//Setting Map Center and Scaling Level
            mBaiduMap.animateMapStatus(update);
            isFirstLoc = false;
            Toast.makeText(getApplicationContext(), location.getAddrStr(), Toast.LENGTH_SHORT).show();
        }
    }

Step 4: Start positioning and close positioning

mLocationClient.start(); //Open positioning

mLocationClient.stop(); //Closed location

The results are as follows:

Posted by robster on Mon, 25 Mar 2019 07:03:29 -0700