以下內容開發環境為:Android Studio+ API 23+AMap_Location_V3.5.0_20170731.jar
真機測試環境:華為 Android7.0
要實現高德地圖API在android中實現定位需實現以下步驟:
- 獲取高德地圖應用Key及配置Key;
- 配置定位權限
- 開啟定位服務,獲取定位數據
第一:獲取高德地圖應用Key及配置Key。這個直接根據高德API說明文檔配置。
但是在獲取SHA1時,如果需要配置keystore。KeyStore配置方法如下:http://blog.csdn.net/nimasike/article/details/51457229
第二:配置定位權限。可以從高德地圖android的Demo中獲取,我給出以下配置:

<!-- 獲取手機號權限 --> <uses-permission android:name="android.permission.READ_PHONE_STATE"/> <!-- 用於進行網絡定位 --> <uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION"/> <!-- 用於訪問GPS定位 --> <uses-permission android:name="android.permission.ACCESS_FINE_LOCATION"/> <!--用於提高GPS定位速度--> <uses-permission android:name="android.permission.ACCESS_LOCATION_EXTRA_COMMANDS" /> <!-- 獲取運營商信息,用於支持提供運營商信息相關的接口 --> <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"/> <!-- 用於訪問wifi網絡信息,wifi信息會用於進行網絡定位 --> <uses-permission android:name="android.permission.ACCESS_WIFI_STATE"/> <!-- 這個權限用於獲取wifi的獲取權限,wifi信息會用來進行網絡定位 --> <uses-permission android:name="android.permission.CHANGE_WIFI_STATE"/> <uses-permission android:name="android.permission.CHANGE_CONFIGURATION" /> <!-- 用於訪問網絡,網絡定位需要上網 --> <uses-permission android:name="android.permission.INTERNET"/>
在andorid6.0以后,要求動態獲取定位權限。動態獲取定位權限如下:
1)在MyUtils類中自定義方法selfPermissionGranted,校驗是否開啟權限

1 /*類MyUtils中自定義權限是否開啟方法*/ 2 public static boolean selfPermissionGranted(String permission, Context context) { 3 // For Android < Android M, self permissions are always granted. 4 boolean result = true; 5 if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) { 6 if (Integer.valueOf(android.os.Build.VERSION.SDK) >= Build.VERSION_CODES.M) { 7 // targetSdkVersion >= Android M, we can 8 // use Context#checkSelfPermission 9 result = context.checkSelfPermission(permission) 10 == PackageManager.PERMISSION_GRANTED; 11 } else { 12 // targetSdkVersion < Android M, we have to use PermissionChecker 13 result = PermissionChecker.checkSelfPermission(context, permission) 14 == PermissionChecker.PERMISSION_GRANTED; 15 } 16 } 17 return result; 18 }
2)在進入系統第一個Activity獲取定位權限
1 if(!MyUtils.selfPermissionGranted(Manifest.permission.ACCESS_COARSE_LOCATION, getApplicationContext())){ 2 ActivityCompat.requestPermissions(this, 3 new String[]{Manifest.permission.ACCESS_COARSE_LOCATION}, 4 Constant.LOCATION_STATE); 5 /*Constant.LOCATION_STATE 為自己定義的一個常量,為權限彈窗回調時使用*/ 6 }
@Override public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) { super.onRequestPermissionsResult(requestCode, permissions, grantResults); if(grantResults.length<=0){ return;} if (requestCode == Constant.LOCATION_STATE){ if (grantResults[0] == PackageManager.PERMISSION_GRANTED) { initPermission(); } else if (grantResults[0] == PackageManager.PERMISSION_DENIED) { Toast.makeText(EntryActivity.this, "獲取位置權限被禁用", Toast.LENGTH_SHORT).show(); finish(); } } }
第三,獲取定位服務。定位服務可能是GPS定位,可能是WIFI定位及移動基站定位。定位服務開啟時,比較消耗電量,所以在需要定位的地方裁判定位服務是否開啟。沒有開啟則調用系統設置-->高級設置-->定位服務,讓用戶開啟定位服務。定位服務開啟后,可根據高德地圖Demo實現定位(高德地圖API中沒有實現調用系統設置定位服務的方法)。
以下給出定位獲取定位類完整代碼:
public class GetLocation implements AMapLocationListener { private AMapLocationClient locationClient = null; private AMapLocationClientOption locationOption = null; private Context context; public Handler mHandler=null; public GetLocation(Context context,Handler mHandle) { this.context = context; this.mHandler=mHandle; }
/*獲取定位數據*/ public void getLocation() { locationClient = new AMapLocationClient(context); locationClient.setLocationListener(this); locationOption = new AMapLocationClientOption(); // 設置定位模式為低功耗模式 locationOption.setLocationMode(AMapLocationClientOption.AMapLocationMode.Battery_Saving); locationOption.setNeedAddress(true); // 設置定位參數 locationClient.setLocationOption(locationOption); // 啟動定位 locationClient.startLocation(); } /*校驗權限*/ public boolean CheckPermission (){ if (MyUtils.selfPermissionGranted(Manifest.permission.ACCESS_COARSE_LOCATION, context)) { return true; } else { return false;} } /*j校驗定位服務是否開啟*/ public boolean CheckAPSService(){ LocationManager locationManager = (LocationManager) context.getSystemService(context.LOCATION_SERVICE); // 通過GPS衛星定位,定位級別可以精確到街(通過24顆衛星定位,在室外和空曠的地方定位准確、速度快) boolean gps = locationManager.isProviderEnabled(LocationManager.GPS_PROVIDER); // 通過WLAN或移動網絡(3G/2G)確定的位置(也稱作AGPS,輔助GPS定位。主要用於在室內或遮蓋物(建築群或茂密的深林等)密集的地方定位) boolean network = locationManager.isProviderEnabled(LocationManager.NETWORK_PROVIDER); if (gps || network) { return true; } return false; } // 定位監聽 @Override public void onLocationChanged(AMapLocation loc) { Message msg = new Message(); msg.obj = loc; msg.what = 1000; mHandler.sendMessage(msg); locationClient.stopLocation(); } }
在Activity中如果沒有開啟定位服務,開啟定位服務的的核心代碼如下:
if(!getLocation.CheckAPSService()){ new AlertDialog.Builder(this).setMessage("請打開GPS或者WIFI開關").setPositiveButton("開啟", new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialog, int which) { Intent intent = new Intent(Settings.ACTION_LOCATION_SOURCE_SETTINGS);//開定系統定位服務設置,需添加 import android.provider.Settings; startActivity(intent); } }).show(); } else { //定位 getLocation.getLocation(); }