前言
1、使用高德開放平臺中的Android 定位SDK最新版本:v4.6.0
2、測試機型:魅藍note2 系統5.1.1
華爲榮耀暢玩7x 系統8.0.0
一、集成高德Android定位sdk
1、高德開發平臺上註冊賬號,並且去“控制檯”在“應用管理”按照提示創建需要高德定位的應用獲取key值。
2、Android 定位SDK上在Android studio配置定位。
3、定位demo下載,將其中的定位jar包放置在你應用的libs文件夾中。
4、獲取定位數據包括:AndroidManifest.xml配置、初始化定位、啓動/停止定位、回調獲取定位結果。
5、注意Android 8.0、Android 6.0權限說明。
6、高德Android 定位SDK錯誤碼對照表。
二、正式封裝
1、初始化定位。
private AMapLocationClientOption getDefaultOption() {
AMapLocationClientOption mOption = new AMapLocationClientOption();
mOption.setLocationMode(AMapLocationClientOption.AMapLocationMode.Hight_Accuracy);//可選,設置定位模式,可選的模式有高精度、僅設備、僅網絡。默認爲高精度模式
mOption.setGpsFirst(false);//可選,設置是否gps優先,只在高精度模式下有效。默認關閉
mOption.setHttpTimeOut(30000);//可選,設置網絡請求超時時間。默認爲30秒。在僅設備模式下無效
mOption.setInterval(2000);//可選,設置定位間隔。默認爲2秒
mOption.setNeedAddress(true);//可選,設置是否返回逆地理地址信息。默認是true
mOption.setOnceLocation(false);//可選,設置是否單次定位。默認是false
mOption.setOnceLocationLatest(false);//可選,設置是否等待wifi刷新,默認爲false.如果設置爲true,會自動變爲單次定位,持續定位時不要使用
AMapLocationClientOption.setLocationProtocol(AMapLocationClientOption.AMapLocationProtocol.HTTP);//可選, 設置網絡請求的協議。可選HTTP或者HTTPS。默認爲HTTP
mOption.setSensorEnable(false);//可選,設置是否使用傳感器。默認是false
mOption.setWifiScan(true); //可選,設置是否開啓wifi掃描。默認爲true,如果設置爲false會同時停止主動刷新,停止以後完全依賴於系統刷新,定位位置可能存在誤差
mOption.setLocationCacheEnable(true); //可選,設置是否使用緩存定位,默認爲true
mOption.setGeoLanguage(AMapLocationClientOption.GeoLanguage.DEFAULT);//可選,設置逆地理信息的語言,默認值爲默認語言(根據所在地區選擇語言)
return mOption;
}
2、啓動定位,回調AMapLocationListener方法獲取經緯度,並且通過rxjava傳遞相關數據。
public void startLocalService() {
//初始化定位
mLocationClient = new AMapLocationClient(BaseApplication.getInstance());
//設置定位回調監聽
mLocationOption = getDefaultOption();
mLocationClient.setLocationOption(mLocationOption);
mLocationClient.setLocationListener(location -> {
if (null != location) {
if (location.getErrorCode() == 0) {
LocationBean locationBean = new LocationBean();
locationBean.setLongitude(String.valueOf(LngUtils.decimalSix(location.getLongitude())));
locationBean.setLatitude(String.valueOf(LngUtils.decimalSix(location.getLatitude())));
RxBus.getDefault().post("GetLocation", locationBean);
} else {
Log.i("location<<<failed", "定位失敗\n錯誤碼:" + location.getErrorCode()
+ "\n錯誤信息:" + location.getErrorInfo()
+ "\n錯誤描述:" + location.getLocationDetail());
}
} else {
ToastUtils.showMessage("定位失敗,loc is null");
}
});
mLocationClient.startLocation();
}
3、停止定位,在Activity的onDestroy中一定要執行AMapLocationClient的onDestroy
public void stopLocalService() {
if (null != mLocationClient) {
mLocationClient.onDestroy();
mLocationClient.stopLocation();
mLocationClient = null;
mLocationOption = null;
}
}
4、完整代碼
public class LocationUtils {
@SuppressLint("StaticFieldLeak")
private static AMapLocationClient mLocationClient;
private AMapLocationClientOption mLocationOption = null;
private static class LocationHolder {
private static final LocationUtils INSTANCE = new LocationUtils();
}
public static LocationUtils getInstance() {
return LocationHolder.INSTANCE;
}
public void startLocalService() {
//初始化定位
mLocationClient = new AMapLocationClient(BaseApplication.getInstance());
//設置定位回調監聽
mLocationOption = getDefaultOption();
mLocationClient.setLocationOption(mLocationOption);
mLocationClient.setLocationListener(location -> {
if (null != location) {
if (location.getErrorCode() == 0) {
LocationBean locationBean = new LocationBean();
locationBean.setLongitude(String.valueOf(LngUtils.decimalSix(location.getLongitude())));
locationBean.setLatitude(String.valueOf(LngUtils.decimalSix(location.getLatitude())));
RxBus.getDefault().post("GetLocation", locationBean);
} else {
Log.i("location<<<failed", "定位失敗\n錯誤碼:" + location.getErrorCode()
+ "\n錯誤信息:" + location.getErrorInfo()
+ "\n錯誤描述:" + location.getLocationDetail());
}
} else {
ToastUtils.showMessage("定位失敗,loc is null");
}
});
mLocationClient.startLocation();
mLocationClient.setLocationListener(new AMapLocationListener() {
@Override
public void onLocationChanged(AMapLocation aMapLocation) {
}
});
}
public void stopLocalService() {
if (null != mLocationClient) {
mLocationClient.onDestroy();
mLocationClient.stopLocation();
mLocationClient = null;
mLocationOption = null;
}
}
private AMapLocationClientOption getDefaultOption() {
AMapLocationClientOption mOption = new AMapLocationClientOption();
mOption.setLocationMode(AMapLocationClientOption.AMapLocationMode.Hight_Accuracy);//可選,設置定位模式,可選的模式有高精度、僅設備、僅網絡。默認爲高精度模式
mOption.setGpsFirst(false);//可選,設置是否gps優先,只在高精度模式下有效。默認關閉
mOption.setHttpTimeOut(30000);//可選,設置網絡請求超時時間。默認爲30秒。在僅設備模式下無效
mOption.setInterval(2000);//可選,設置定位間隔。默認爲2秒
mOption.setNeedAddress(true);//可選,設置是否返回逆地理地址信息。默認是true
mOption.setOnceLocation(false);//可選,設置是否單次定位。默認是false
mOption.setOnceLocationLatest(false);//可選,設置是否等待wifi刷新,默認爲false.如果設置爲true,會自動變爲單次定位,持續定位時不要使用
AMapLocationClientOption.setLocationProtocol(AMapLocationClientOption.AMapLocationProtocol.HTTP);//可選, 設置網絡請求的協議。可選HTTP或者HTTPS。默認爲HTTP
mOption.setSensorEnable(false);//可選,設置是否使用傳感器。默認是false
mOption.setWifiScan(true); //可選,設置是否開啓wifi掃描。默認爲true,如果設置爲false會同時停止主動刷新,停止以後完全依賴於系統刷新,定位位置可能存在誤差
mOption.setLocationCacheEnable(true); //可選,設置是否使用緩存定位,默認爲true
mOption.setGeoLanguage(AMapLocationClientOption.GeoLanguage.DEFAULT);//可選,設置逆地理信息的語言,默認值爲默認語言(根據所在地區選擇語言)
return mOption;
}
}
三、activity中調用使用
1、在activity中啓動定位。
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
LocationUtils.getInstance().startLocalService();
ButterKnife.bind(this);
}
2、在activity中停止定位。
@Override
protected void onDestroy() {
super.onDestroy();
LocationUtils.getInstance().stopLocalService();
}
四、如有疑問請留言