1. 概述
這里記錄下自己項目中的,首頁百度地圖獲取定位后無法獲取定位后的數(shù)據(jù)。
場景:
在首頁HomeFragment中棒呛,一開始進入界面后需要獲取當(dāng)前位置,在本地接口的http環(huán)境下定位成功后域携,可以獲取到當(dāng)前位置的city簇秒、address及經(jīng)緯度等信息,但是切換到線上的 https環(huán)境后秀鞭,然后再次在首頁獲取定位趋观,這個時候定位后扛禽,貌似只能獲取到經(jīng)緯度,其余的 city皱坛、地址等所有信息全是 null编曼,自己也不知道啥原因,感覺和https環(huán)境沒關(guān)系剩辟,自己處理方式是:定位成功后掐场,獲取到經(jīng)緯度,采用反編碼抹沪,根據(jù)經(jīng)緯度獲取地址信息刻肄;
哪位知道啥原因的可以給我留言,自己不太清楚這個啥原因融欧;
2. 代碼如下
@Override
public void onResume() {
super.onResume();
initLocationOption() ;
}
private void initLocationOption() {
//定位服務(wù)的客戶端敏弃。宿主程序在客戶端聲明此類,并調(diào)用噪馏,目前只支持在主線程中啟動
LocationClient locationClient = new LocationClient(BaseApplication.getContext());
//聲明LocationClient類實例并配置定位參數(shù)
LocationClientOption locationOption = new LocationClientOption();
MyLocationListener myLocationListener = new MyLocationListener();
//注冊監(jiān)聽函數(shù)
locationClient.registerLocationListener(myLocationListener);
//可選麦到,默認(rèn)高精度,設(shè)置定位模式欠肾,高精度瓶颠,低功耗,僅設(shè)備
locationOption.setLocationMode(LocationClientOption.LocationMode.Hight_Accuracy);
//可選刺桃,默認(rèn)gcj02粹淋,設(shè)置返回的定位結(jié)果坐標(biāo)系,如果配合百度地圖使用瑟慈,建議設(shè)置為bd09ll;
locationOption.setCoorType("gcj02");
//可選桃移,默認(rèn)0,即僅定位一次葛碧,設(shè)置發(fā)起連續(xù)定位請求的間隔需要大于等于1000ms才是有效的
locationOption.setScanSpan(1000);
//可選借杰,設(shè)置是否需要地址信息,默認(rèn)不需要
locationOption.setIsNeedAddress(true);
//可選进泼,設(shè)置是否需要地址描述
locationOption.setIsNeedLocationDescribe(true);
//可選蔗衡,設(shè)置是否需要設(shè)備方向結(jié)果
locationOption.setNeedDeviceDirect(false);
//可選,默認(rèn)false乳绕,設(shè)置是否當(dāng)gps有效時按照1S1次頻率輸出GPS結(jié)果
locationOption.setLocationNotify(true);
//可選绞惦,默認(rèn)true,定位SDK內(nèi)部是一個SERVICE洋措,并放到了獨立進程济蝉,設(shè)置是否在stop的時候殺死這個進程,默認(rèn)不殺死
locationOption.setIgnoreKillProcess(true);
//可選,默認(rèn)false堆生,設(shè)置是否需要位置語義化結(jié)果专缠,可以在BDLocation.getLocationDescribe里得到,結(jié)果類似于“在北京天安門附近”
locationOption.setIsNeedLocationDescribe(true);
//可選淑仆,默認(rèn)false涝婉,設(shè)置是否需要POI結(jié)果,可以在BDLocation.getPoiList里得到
locationOption.setIsNeedLocationPoiList(true);
//可選蔗怠,默認(rèn)false墩弯,設(shè)置是否收集CRASH信息,默認(rèn)收集
locationOption.SetIgnoreCacheException(false);
//可選寞射,默認(rèn)false渔工,設(shè)置是否開啟Gps定位
locationOption.setOpenGps(true);
//可選,默認(rèn)false桥温,設(shè)置定位時是否需要海拔信息引矩,默認(rèn)不需要,除基礎(chǔ)定位版本都可用
locationOption.setIsNeedAltitude(false);
//設(shè)置打開自動回調(diào)位置模式侵浸,該開關(guān)打開后旺韭,期間只要定位SDK檢測到位置變化就會主動回調(diào)給開發(fā)者,該模式下開發(fā)者無需再關(guān)心定位間隔是多少掏觉,定位SDK本身發(fā)現(xiàn)位置變化就會及時回調(diào)給開發(fā)者
locationOption.setOpenAutoNotifyMode();
//設(shè)置打開自動回調(diào)位置模式区端,該開關(guān)打開后,期間只要定位SDK檢測到位置變化就會主動回調(diào)給開發(fā)者
locationOption.setOpenAutoNotifyMode(3000,1, LocationClientOption.LOC_SENSITIVITY_HIGHT);
//開始定位
locationClient.start();
}
/**
* 實現(xiàn)定位回調(diào)
*/
public class MyLocationListener extends BDAbstractLocationListener {
/**
* 定位
*/
@Override
public void onReceiveLocation(BDLocation location){
//此處的BDLocation為定位結(jié)果信息類澳腹,通過它的各種get方法可獲取定位相關(guān)的全部結(jié)果
//以下只列舉部分獲取經(jīng)緯度相關(guān)(常用)的結(jié)果信息
//更多結(jié)果信息獲取說明织盼,請參照類參考中BDLocation類中的說明
//獲取緯度信息
double latitude = location.getLatitude();
//獲取經(jīng)度信息
double longitude = location.getLongitude();
//獲取定位精度,默認(rèn)值為0.0f
float radius = location.getRadius();
//獲取經(jīng)緯度坐標(biāo)類型酱塔,以LocationClientOption中設(shè)置過的坐標(biāo)類型為準(zhǔn)
String coorType = location.getCoorType();
//獲取定位類型沥邻、定位錯誤返回碼,具體信息可參照類參考中BDLocation類中的說明
int errorCode = location.getLocType();
String addrStr = location.getAddrStr();
int locationWhere = location.getLocationWhere();
String buildingName = location.getBuildingName();
Address address = location.getAddress();
String street = location.getStreet();
List<Poi> poiList = location.getPoiList();
// List<Poi> poiList = location.getPoiList();
// String name = poiList.get(0).getName();
Log.e("TAG" , "定位回調(diào):latitude: "+latitude+", longitude: "+longitude+", addrStr: "+addrStr+", locationWhere: "+locationWhere+", " +
"buildingName: "+buildingName+", address: "+address+", street: "+street+", poiList: "+poiList) ;
LatLng ll = new LatLng(location.getLatitude(),
location.getLongitude());
// 定位成功后延旧,保存當(dāng)前位置經(jīng)緯度
String strLatitude = Double.toString(latitude) ; // 維度
String strLongitude = Double.toString(longitude) ;// 經(jīng)度
PrefUtils.putString(getActivity() , "strLatitude" , strLatitude);
PrefUtils.putString(getActivity() , "strLongitude" , strLongitude);
getdata(ll);
}
}
/**
* 定位后獲取當(dāng)前經(jīng)緯度谋国,根據(jù)經(jīng)緯度 采用反編碼 獲取當(dāng)前位置信息
*/
private void getdata(LatLng ll) {
GeoCoder geoCoder = GeoCoder.newInstance();
//
OnGetGeoCoderResultListener listener = new OnGetGeoCoderResultListener() {
// 反地理編碼查詢結(jié)果回調(diào)函數(shù)
@Override
public void onGetReverseGeoCodeResult(ReverseGeoCodeResult result) {
if (result == null
|| result.error != SearchResult.ERRORNO.NO_ERROR) {
// 沒有檢測到結(jié)果
Toast.makeText(getActivity() , "抱歉槽地,未能找到結(jié)果",
Toast.LENGTH_LONG).show();
return;
}
/*String addressBig = result.getSematicDescription(); // 美華·海潤大廈南135米
String addressSmall = result.getAddress() ; // 北京市海淀區(qū)海淀南路36號,
String address = result.getAddress();
String street = result.getAddressDetail().street;
String businessCircle = result.getBusinessCircle();
List<ReverseGeoCodeResult.PoiRegionsInfo> poiRegionsInfoList = result.getPoiRegionsInfoList();
List<PoiInfo> poiInfos = (ArrayList<PoiInfo>) result.getPoiList();*/
List<PoiInfo> poiInfos = (ArrayList<PoiInfo>) result.getPoiList();
address = poiInfos.get(0).address ;
// 如果定位成功迁沫,則用sp保存定位后當(dāng)前默認(rèn)的地址
PrefUtils.putString(getActivity() , DEFAULT_POSI , address);
showPosiAddress(address) ;
}
// 地理編碼查詢結(jié)果回調(diào)函數(shù)
@Override
public void onGetGeoCodeResult(GeoCodeResult result) {
if (result == null
|| result.error != SearchResult.ERRORNO.NO_ERROR) {
// 沒有檢測到結(jié)果
}
}
};
// 設(shè)置地理編碼檢索監(jiān)聽者
geoCoder.setOnGetGeoCodeResultListener(listener);
//
geoCoder.reverseGeoCode(new ReverseGeoCodeOption().location(ll));
}
private void showPosiAddress(String address) {
if (!TextUtils.isEmpty(mAddressFlag)){
tv_location.setText(!TextUtils.isEmpty(mAddressFlag) ? mAddressFlag : "");
}else if (!TextUtils.isEmpty(address)) {
tv_location.setText(!TextUtils.isEmpty(address) ? address : "");
}
}
更詳細(xì)的可以參照 下邊這篇博客:
https://blog.csdn.net/a_ycmbc/article/details/51459502