前期准備:
跟着百度開放平台指導完成AK的申請以及Android Studio的配置
*開發版的SHA1參見於 pxM_Wxd 的 [Android]Android Studio獲取開發版和發布版的MD5和SHA1
*發布版SHA1則 keytool -list -v -keystore 文件目錄\自己的簽名文件 (需要事先創建密鑰)
開發:
由於我只需要顯示定位位置信息,xml中只有一個TextView
Activity文件:
public class AttendanceActivity extends AppCompatActivity { public LocationClient mLocationClient; private TextView positionText; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); //在使用SDK各組件之前初始化context信息,傳入ApplicationContext SDKInitializer.initialize(getApplicationContext()); setContentView(R.layout.activity_attendance); //創建一個LocationClient的實例,接受的context通過getApplicationContext()方法獲取。 mLocationClient = new LocationClient(getApplicationContext()); //調用LocationClient的registerLocationListener()方法來注冊一個監聽器 當獲取到位置信息的時候,就會回調這個定位監聽器 mLocationClient.registerLocationListener(new MyLocationListener()); positionText = findViewById(R.id.textView); /* * 之前在AndroidManifest.xml內聲明了很多權限。 * 其中有4個是危險權限。不過ACCESS_COARSE_LOCATION 和 ACCESS_FINE_LOCATION都屬於一個權限組,所以兩者只需要申請其中一個就可以了。 * 如何在運行時一次申請三個權限呢? * 首先創建一個空的List集合,然后依次判斷這三個權限有沒有被授權,如果沒有授權就添加到List集合中,最后將List集合轉化成數組,在調用ActivityCompat.requestPermissions()方法就可以一次性申請。 */ List<String> permissionList = new ArrayList<>(); if(ContextCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED){ permissionList.add(Manifest.permission.ACCESS_FINE_LOCATION); } if(ContextCompat.checkSelfPermission(this,Manifest.permission.READ_PHONE_STATE) != PackageManager.PERMISSION_GRANTED){ permissionList.add(Manifest.permission.READ_PHONE_STATE); } if(ContextCompat.checkSelfPermission(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(this,permissions,1); }else{ requestLocation(); } } private void requestLocation(){ //實時更新 initLocation(); //調用start方法會回調到我們注冊的監聽器上面 mLocationClient.start(); } private void initLocation() { LocationClientOption option = new LocationClientOption(); //option.setLocationMode(LocationClientOption.LocationMode.Hight_Accuracy); option.setScanSpan(4000); option.setCoorType("bd09ll"); option.setIsNeedAddress(true); mLocationClient.setLocOption(option); } @Override public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) { switch (requestCode){ case 1: if(grantResults.length > 0){ //將每個申請的權限都進行判斷 如果存在一個沒有被授權,那么就調用finish()方法關閉程序。 for(int result : grantResults){ if(result != PackageManager.PERMISSION_GRANTED){ Toast.makeText(AttendanceActivity.this,"必須同意所有權限才能使用本程序",Toast.LENGTH_SHORT).show(); finish(); return ; } } //所有權限都已經授權,那么直接調用requestLocation()方法開始定位。 requestLocation(); }else{ Toast.makeText(AttendanceActivity.this,"發生未知錯誤", Toast.LENGTH_SHORT).show(); finish(); } break; default: break; } } public class MyLocationListener implements BDLocationListener { @Override public void onReceiveLocation(final BDLocation location) { runOnUiThread(new Runnable() { @Override public void run() { StringBuilder currentPosition = new StringBuilder(); //通過BDLocation的getLatitude()方法獲取當前位置的緯度 currentPosition.append("緯度").append(location.getLatitude()).append("\n"); //通過BDLocation的getLongitude()方法獲取當前位置的經度。 currentPosition.append("經線").append(location.getLongitude()).append("\n"); currentPosition.append("地址 ").append(location.getAddrStr()).append("\n"); LatLng locNow = new LatLng(location.getLatitude(),location.getLongitude()); double lat = 31.239150,longi=121.499862; //自行修改終點的 維度 和 精度 LatLng locComy = new LatLng(lat,longi); int dis = (int) DistanceUtil. getDistance(locNow, locComy); currentPosition.append("距離:").append(dis+"米").append("\n"); // currentPosition.append("國家").append(location.getCountry()).append("\n"); // currentPosition.append("省").append(location.getProvince()).append("\n"); // currentPosition.append("市").append(location.getCity()).append("\n"); // currentPosition.append("區").append(location.getDistrict()).append("\n"); // currentPosition.append("街道").append(location.getStreet()).append("\n"); // //getLocType()方法獲取當前的定位方式。 // if(location.getLocType() == BDLocation.TypeGpsLocation){ // currentPosition.append("GPS"); // }else if(location.getLocType() == BDLocation.TypeNetWorkLocation){ // currentPosition.append("網絡"); // } Log.i("pos",currentPosition+""); positionText.setText(currentPosition); } }); } } @Override protected void onDestroy(){ super.onDestroy(); mLocationClient.stop(); } }
就搞定啦!