2000字范文,分享全网优秀范文,学习好帮手!
2000字范文 > android百度定位流量定位返回空 百度地图定位后数据为null

android百度定位流量定位返回空 百度地图定位后数据为null

时间:2020-04-05 01:42:49

相关推荐

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;

}

/*String addressBig = result.getSematicDescription(); // 美华·海润大厦南135米

String addressSmall = result.getAddress() ; // 北京市海淀区海淀南路36号,

String address = result.getAddress();

String street = result.getAddressDetail().street;

String businessCircle = result.getBusinessCircle();

List poiRegionsInfoList = result.getPoiRegionsInfoList();

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

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

}

}

本内容不代表本网观点和政治立场,如有侵犯你的权益请联系我们处理。
网友评论
网友评论仅供其表达个人看法,并不表明网站立场。