Baidu Map Android Location

1. Register and get the key

First, you need to complete the developer registration work, address: https://passport.baidu.com/v2/?reg
Then you can see my application , you can create an application:
Insert picture description here
create a reference document: Android positioning SDK

Currently, Baidu Maps Android positioning SDK domestic service does not charge any fees to developers who use it for non-commercial purposes.

Since I only need this function, I choose here:
Insert picture description here
switch to the user directory .android:
Insert picture description here
input cmd, input the command:

keytool -list -v -keystore debug.keystore -alias androiddebugkey

Enter the key store password:, andoridthen you can see it SHA1, just copy it and enter it.
Next is the package name. Open the AndroidManifest.xml configuration file. The content corresponding to the package attribute is the application package name.
Then, submit it.
Insert picture description here

2. Android Studio configuration

Download the Android location SDK and decompress it, and place the jar and so in the libs to the corresponding location in the project.
Insert picture description here
I am here in the download, select the basic positioning, JAR, and get the BaiduLBS_AndroidSDK_Lib.zipfile.
Follow the instructions to decompress, and the content after decompression is as follows:
Insert picture description here
Then, follow the instructions on the official website to place the Android positioning SDK into the Android project:
switch to the projectview, and then find libs:
Insert picture description here
Insert picture description here

Then, add dependencies:

android {
    ...
    // 百度定位SDK需要
    sourceSets{
        main{
            jniLibs.srcDir  'libs'
            jni.srcDirs = []    //disable automatic ndk-build
        }
    }
}

implementation files('libs/BaiduLBS_Android.jar')

3. Add AK

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

<meta-data
    android:name="com.baidu.lbsapi.API_KEY"
    android:value="AK" >
</meta-data>

Add service:

<service android:name="com.baidu.location.f" android:enabled="true" android:process=":remote"> </service>

Add permissions:

<!-- 这个权限用于进行网络定位-->
    <uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION"/>
    <!-- 这个权限用于访问GPS定位-->
    <uses-permission android:name="android.permission.ACCESS_FINE_LOCATION"/>
    <!-- 用于访问wifi网络信息,wifi信息会用于进行网络定位-->
    <uses-permission android:name="android.permission.ACCESS_WIFI_STATE"/>
    <!-- 获取运营商信息,用于支持提供运营商信息相关的接口-->
    <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"/>
    <!-- 这个权限用于获取wifi的获取权限,wifi信息会用来进行网络定位-->
    <uses-permission android:name="android.permission.CHANGE_WIFI_STATE"/>
    <!-- 写入扩展存储,向扩展卡写入数据,用于写入离线定位数据-->
    <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
    <!-- 访问网络,网络定位需要上网-->
    <uses-permission android:name="android.permission.INTERNET"/>

4. Get the address

Just follow the official website case: http://lbsyun.baidu.com/index.php?title=android-locsdk/guide/get-location/address
After testing, you also need to call the onstopmethod yourself .

// 百度定位
public LocationClient mLocationClient = null;
private MyLocationListener myLocationListener = new MyLocationListener();
private void getLocation() {
    
    
  // 请求定位
    mLocationClient = new LocationClient(getApplicationContext());
    List<String> permissionList = new ArrayList<>();
    //如果没有启动下面权限,就询问用户让用户打开
    if (ContextCompat.checkSelfPermission(PushActivity.this, Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED) {
    
    
        permissionList.add(Manifest.permission.ACCESS_FINE_LOCATION);
    }
    if (ContextCompat.checkSelfPermission(PushActivity.this, Manifest.permission.READ_PHONE_STATE) != PackageManager.PERMISSION_GRANTED) {
    
    
        permissionList.add(Manifest.permission.READ_PHONE_STATE);
    }
    if (ContextCompat.checkSelfPermission(PushActivity.this,
            Manifest.permission.WRITE_EXTERNAL_STORAGE) != PackageManager.PERMISSION_GRANTED) {
    
    
        permissionList.add(Manifest.permission.WRITE_EXTERNAL_STORAGE);
    }
    if (!permissionList.isEmpty()) {
    
    
        String[] permissions = permissionList.toArray(new String[permissionList.size()]);
        ActivityCompat.requestPermissions(PushActivity.this, permissions, 1);
    }
    requestLocation();
}
/**
* 请求定位
 */
private void requestLocation(){
    
    
    LocationClientOption option = new LocationClientOption();
    option.setScanSpan(500);
    option.setLocationMode(LocationClientOption.LocationMode.Hight_Accuracy);
    option.setCoorType("bd09ll");
    option.setScanSpan(1000);
    option.setOpenGps(true);
    option.setIsNeedAddress(true);
    option.setLocationNotify(true);
    option.setIgnoreKillProcess(false);
    option.SetIgnoreCacheException(false);
    option.setEnableSimulateGps(false);
    option.setNeedNewVersionRgc(true);

    mLocationClient.setLocOption(option);
    mLocationClient.registerLocationListener(myLocationListener);
    mLocationClient.start();
}

private String currentPosition = "";
public class MyLocationListener extends BDAbstractLocationListener {
    
    

    @Override
    public void onReceiveLocation(BDLocation location) {
    
    
        String addr = location.getAddrStr();    //获取详细地址信息
        String country = location.getCountry();    //获取国家
        String province = location.getProvince();    //获取省份
        String city = location.getCity();    //获取城市
        String district = location.getDistrict();    //获取区县
        String street = location.getStreet();    //获取街道信息
        String adcode = location.getAdCode();    //获取adcode
        String town = location.getTown();    //获取乡镇信息

        EditText reedit_page_position = findViewById(R.id.reedit_page_position);
        String position_detail = country+province+city+district;
        if(currentPosition.equals(position_detail) && currentPosition.length()!=0){
    
    
            mLocationClient.unRegisterLocationListener(myLocationListener);
            mLocationClient.stop();
        }
        reedit_page_position.setText(position_detail);
        currentPosition = position_detail;
    }

}

Just call it getLocation.

Guess you like

Origin blog.csdn.net/qq_26460841/article/details/115261413