Android整合百度定位SDK實現獲取定位地址
阿新 • • 發佈:2018-12-20
應用場景:實現APP的網路定位功能
1.整合SDK
去百度建立應用,獲取到AK,下載定位SDK,把裡面的jar包新增到libs下。其他os檔案就放在main下新建的jniLilbs裡,如圖:
2.程式碼貼出
package com.jin.baidudemo; import android.Manifest; import android.content.pm.PackageManager; import android.support.annotation.NonNull; import android.support.v4.app.ActivityCompat; import android.support.v4.content.ContextCompat; import android.support.v7.app.AppCompatActivity; import android.os.Bundle; import android.widget.TextView; import android.widget.Toast; import com.baidu.location.BDAbstractLocationListener; import com.baidu.location.BDLocation; import com.baidu.location.LocationClient; import com.baidu.location.LocationClientOption; import java.util.ArrayList; import java.util.List; public class MainActivity extends AppCompatActivity { public LocationClient mLocationClient = null; private MyLocationListener myListener = new MyLocationListener(); TextView tv_location; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); tv_location = (TextView) findViewById(R.id.tv_location); //宣告LocationClient類 mLocationClient = new LocationClient(getApplicationContext()); //註冊監聽函式 mLocationClient.registerLocationListener(myListener); init(); // 初始化配置 //存放許可權的集合 List<String> permissionList=new ArrayList<>(); //判斷是否有許可權 if (ContextCompat.checkSelfPermission(MainActivity.this, Manifest.permission.ACCESS_FINE_LOCATION)!= PackageManager.PERMISSION_GRANTED){ permissionList.add(Manifest.permission.ACCESS_FINE_LOCATION); } if (ContextCompat.checkSelfPermission(MainActivity.this,Manifest.permission.READ_PHONE_STATE)!=PackageManager.PERMISSION_GRANTED){ permissionList.add(Manifest.permission.READ_PHONE_STATE); } if (ContextCompat.checkSelfPermission(MainActivity.this,Manifest.permission.WRITE_EXTERNAL_STORAGE)!=PackageManager.PERMISSION_GRANTED){ permissionList.add(Manifest.permission.WRITE_EXTERNAL_STORAGE); } if (!permissionList.isEmpty()){ String[] permission = permissionList.toArray(new String[permissionList.size()]); ActivityCompat.requestPermissions(MainActivity.this,permission,1); }else { request(); } } /** * 初始化配置 */ private void init() { LocationClientOption option = new LocationClientOption(); option.setIsNeedAddress(true); mLocationClient.setLocOption(option); } /** * 開始請求 */ public void request(){ mLocationClient.start(); } /** * 百度定位的響應介面 */ 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(); //獲取街道資訊 tv_location.setText("address==" + addr + " 國家=" + country + " 省=" + province + " 市=" + city + " 區=" + district + " 街道=" + street); } } /** * 獲取動態許可權的回撥 * @param requestCode * @param permissions * @param grantResults */ @Override public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) { switch (requestCode){ case 1: if (grantResults.length>0){ for (int results : grantResults){ if (results!=PackageManager.PERMISSION_GRANTED){ Toast.makeText(MainActivity.this,"必須開啟許可權才能使用!",Toast.LENGTH_SHORT).show(); finish(); return; } } request(); }else { Toast.makeText(MainActivity.this,"未知錯誤!!",Toast.LENGTH_SHORT).show(); finish(); } break; default:break; } } }
3.許可權
<?xml version="1.0" encoding="utf-8"?> <manifest xmlns:android="http://schemas.android.com/apk/res/android" package="com.jin.baidudemo"> <!-- 這個許可權用於進行網路定位--> <uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION"></uses-permission> <!-- 這個許可權用於訪問GPS定位--> <uses-permission android:name="android.permission.ACCESS_FINE_LOCATION"></uses-permission> <!-- 用於訪問wifi網路資訊,wifi資訊會用於進行網路定位--> <uses-permission android:name="android.permission.ACCESS_WIFI_STATE"></uses-permission> <!-- 獲取運營商資訊,用於支援提供運營商資訊相關的介面--> <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"></uses-permission> <!-- 這個許可權用於獲取wifi的獲取許可權,wifi資訊會用來進行網路定位--> <uses-permission android:name="android.permission.CHANGE_WIFI_STATE"></uses-permission> <!-- 用於讀取手機當前的狀態--> <uses-permission android:name="android.permission.READ_PHONE_STATE"></uses-permission> <!-- 寫入擴充套件儲存,向擴充套件卡寫入資料,用於寫入離線定位資料--> <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"></uses-permission> <!-- 訪問網路,網路定位需要上網--> <uses-permission android:name="android.permission.INTERNET" /> <!-- SD卡讀取許可權,使用者寫入離線定位資料--> <uses-permission android:name="android.permission.MOUNT_UNMOUNT_FILESYSTEMS"></uses-permission> <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="TrYMeBYEYL0GtRbTLAqNInyhdCgfcw1y" > </meta-data> <activity android:name=".MainActivity"> <intent-filter> <action android:name="android.intent.action.MAIN" /> <category android:name="android.intent.category.LAUNCHER" /> </intent-filter> </activity> <service android:name="com.baidu.location.f" android:enabled="true" android:process=":remote"> </service> </application> </manifest>
4.效果圖: