天天看點

android百度定位流量定位傳回空,百度地圖定位後資料為null

1. 概述

這裡記錄下自己項目中的,首頁百度地圖擷取定位後無法擷取定位後的資料。

場景:

在首頁HomeFragment中,一開始進入界面後需要擷取目前位置,在本地接口的http環境下定位成功後,可以擷取到目前位置的city、address及經緯度等資訊,但是切換到線上的 https環境後,然後再次在首頁擷取定位,這個時候定位後,貌似隻能擷取到經緯度,其餘的 city、位址等所有資訊全是 null,自己也不知道啥原因,感覺和https環境沒關系,自己處理方式是:定位成功後,擷取到經緯度,采用反編碼,根據經緯度擷取位址資訊;

哪位知道啥原因的可以給我留言,自己不太清楚這個啥原因;

2. 代碼如下

@Override

public void onResume() {

super.onResume();

initLocationOption() ;

}

private void initLocationOption() {

//定位服務的用戶端。宿主程式在用戶端聲明此類,并調用,目前隻支援在主線程中啟動

LocationClient locationClient = new LocationClient(BaseApplication.getContext());

//聲明LocationClient類執行個體并配置定位參數

LocationClientOption locationOption = new LocationClientOption();

MyLocationListener myLocationListener = new MyLocationListener();

//注冊監聽函數

locationClient.registerLocationListener(myLocationListener);

//可選,預設高精度,設定定位模式,高精度,低功耗,僅裝置

locationOption.setLocationMode(LocationClientOption.LocationMode.Hight_Accuracy);

//可選,預設gcj02,設定傳回的定位結果坐标系,如果配合百度地圖使用,建議設定為bd09ll;

locationOption.setCoorType("gcj02");

//可選,預設0,即僅定位一次,設定發起連續定位請求的間隔需要大于等于1000ms才是有效的

locationOption.setScanSpan(1000);

//可選,設定是否需要位址資訊,預設不需要

locationOption.setIsNeedAddress(true);

//可選,設定是否需要位址描述

locationOption.setIsNeedLocationDescribe(true);

//可選,設定是否需要裝置方向結果

locationOption.setNeedDeviceDirect(false);

//可選,預設false,設定是否當gps有效時按照1S1次頻率輸出GPS結果

locationOption.setLocationNotify(true);

//可選,預設true,定位SDK内部是一個SERVICE,并放到了獨立程序,設定是否在stop的時候殺死這個程序,預設不殺死

locationOption.setIgnoreKillProcess(true);

//可選,預設false,設定是否需要位置語義化結果,可以在BDLocation.getLocationDescribe裡得到,結果類似于“在北京天安門附近”

locationOption.setIsNeedLocationDescribe(true);

//可選,預設false,設定是否需要POI結果,可以在BDLocation.getPoiList裡得到

locationOption.setIsNeedLocationPoiList(true);

//可選,預設false,設定是否收集CRASH資訊,預設收集

locationOption.SetIgnoreCacheException(false);

//可選,預設false,設定是否開啟Gps定位

locationOption.setOpenGps(true);

//可選,預設false,設定定位時是否需要海拔資訊,預設不需要,除基礎定位版本都可用

locationOption.setIsNeedAltitude(false);

//設定打開自動回調位置模式,該開關打開後,期間隻要定位SDK檢測到位置變化就會主動回調給開發者,該模式下開發者無需再關心定位間隔是多少,定位SDK本身發現位置變化就會及時回調給開發者

locationOption.setOpenAutoNotifyMode();

//設定打開自動回調位置模式,該開關打開後,期間隻要定位SDK檢測到位置變化就會主動回調給開發者

locationOption.setOpenAutoNotifyMode(3000,1, LocationClientOption.LOC_SENSITIVITY_HIGHT);

//開始定位

locationClient.start();

}

public class MyLocationListener extends BDAbstractLocationListener {

@Override

public void onReceiveLocation(BDLocation location){

//此處的BDLocation為定位結果資訊類,通過它的各種get方法可擷取定位相關的全部結果

//以下隻列舉部分擷取經緯度相關(常用)的結果資訊

//更多結果資訊擷取說明,請參照類參考中BDLocation類中的說明

//擷取緯度資訊

double latitude = location.getLatitude();

//擷取經度資訊

double longitude = location.getLongitude();

//擷取定位精度,預設值為0.0f

float radius = location.getRadius();

//擷取經緯度坐标類型,以LocationClientOption中設定過的坐标類型為準

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 poiList = location.getPoiList();

// List poiList = location.getPoiList();

// String name = poiList.get(0).getName();

Log.e("TAG" , "定位回調:latitude: "+latitude+", longitude: "+longitude+", addrStr: "+addrStr+", locationWhere: "+locationWhere+", " +

"buildingName: "+buildingName+", address: "+address+", street: "+street+", poiList: "+poiList) ;

LatLng ll = new LatLng(location.getLatitude(),

location.getLongitude());

// 定位成功後,儲存目前位置經緯度

String strLatitude = Double.toString(latitude) ; // 次元

String strLongitude = Double.toString(longitude) ;// 經度

PrefUtils.putString(getActivity() , "strLatitude" , strLatitude);

PrefUtils.putString(getActivity() , "strLongitude" , strLongitude);

getdata(ll);

}

}

private void getdata(LatLng ll) {

GeoCoder geoCoder = GeoCoder.newInstance();

//

OnGetGeoCoderResultListener listener = new OnGetGeoCoderResultListener() {

// 反地理編碼查詢結果回調函數

@Override

public void onGetReverseGeoCodeResult(ReverseGeoCodeResult result) {

if (result == null

|| result.error != SearchResult.ERRORNO.NO_ERROR) {

// 沒有檢測到結果

Toast.makeText(getActivity() , "抱歉,未能找到結果",

Toast.LENGTH_LONG).show();

return;

}

List poiInfos = (ArrayList) result.getPoiList();

address = poiInfos.get(0).address ;

// 如果定位成功,則用sp儲存定位後目前預設的位址

PrefUtils.putString(getActivity() , DEFAULT_POSI , address);

showPosiAddress(address) ;

}

// 地理編碼查詢結果回調函數

@Override

public void onGetGeoCodeResult(GeoCodeResult result) {

if (result == null

|| result.error != SearchResult.ERRORNO.NO_ERROR) {

// 沒有檢測到結果

}

}

};

// 設定地理編碼檢索監聽者

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 : "");

}

}