百度LBS定位使用

(题外话:up主开发android开发快三年了,以前遇到的问题都是自己解决完就可以了,不过在简书看到之前的同事,也是好哥们@Alex_Cin写了好多文章,自己也用到了一些,感觉都2017年了,也该留下自己的一些痕迹,就当是为了即将流逝的青春吧!)

  up主最近在写一个新项目,关于借款的一个app,正好更具需求用到了定位,其实对于一般程序员来说,定位可能是最基本的了吧,之前我在项目中用到了高德定位,这次根据需求选择了百度定位,话不多说,直接走起;

1.在Application标签中声明SERVICE组件。

2.声明使用权限,这里复制Api文档内容即可:


3.那就开始配置你的appkey了;

4.我是写到一个工具类里边在MainActivity里边oncreat方法下添加这样的一句话即可;


5.最后我们在log日志里看一下,可以在把全部位置信息传给后台,这里我需要的就是一个city显示。



6。这样的就完成了一个定位,这是第一次写,逻辑一定是对的,可能会表述不太明白,我会继续努力的,感谢!!!

备注:以下是我的代码

public classMyLocationUtil {

public staticLocationClientmLocationClient=null;

public staticBDLocationListenermyListener=newMyLocationListener();

public static voidgetLocation(Context context) {

mLocationClient=newLocationClient(context);//声明LocationClient类

mLocationClient.registerLocationListener(myListener);//注册监听函数

initLocation();

mLocationClient.start();

}

/**

*设置定位参数

*高精度定位模式:这种定位模式下,会同时使用网络定位和GPS定位,优先返回最高精度的定位结果;

低功耗定位模式:这种定位模式下,不会使用GPS进行定位,只会使用网络定位(WiFi定位和基站定位);

仅用设备定位模式:这种定位模式下,不需要连接网络,只使用GPS进行定位,这种模式下不支持室内环境的定位。

*/

private static voidinitLocation(){

LocationClientOption option =newLocationClientOption();

option.setLocationMode(LocationClientOption.LocationMode.Hight_Accuracy

);//可选,默认高精度,设置定位模式,高精度,低功耗,仅设备

option.setCoorType("bd09ll");//可选,默认gcj02,设置返回的定位结果坐标系

intspan=1000;

option.setScanSpan(0);//可选,默认0,即仅定位一次,设置发起定位请求的间隔需要大于等于1000ms才是有效的

option.setIsNeedAddress(true);//可选,设置是否需要地址信息,默认不需要

option.setOpenGps(true);//可选,默认false,设置是否使用gps

option.setLocationNotify(true);//可选,默认false,设置是否当GPS有效时按照1S/1次频率输出GPS结果

option.setIsNeedLocationDescribe(true);//可选,默认false,设置是否需要位置语义化结果,可以在BDLocation.getLocationDescribe里得到,结果类似于“在北京天安门附近”

option.setIsNeedLocationPoiList(true);//可选,默认false,设置是否需要POI结果,可以在BDLocation.getPoiList里得到

option.setIgnoreKillProcess(false);//可选,默认true,定位SDK内部是一个SERVICE,并放到了独立进程,设置是否在stop的时候杀死这个进程,默认不杀死

option.SetIgnoreCacheException(false);//可选,默认false,设置是否收集CRASH信息,默认收集

option.setEnableSimulateGps(false);//可选,默认false,设置是否需要过滤GPS仿真结果,默认需要

mLocationClient.setLocOption(option);

}

public static classMyLocationListenerimplementsBDLocationListener {

@Override

public voidonReceiveLocation(BDLocation location) {

mLocationClient.stop();

//Receive Location

StringBuffer sb =newStringBuffer(256);

sb.append("periods : ");

sb.append(location.getTime());

sb.append("\nerror code : ");

sb.append(location.getLocType());

sb.append("\nlatitude : ");

sb.append(location.getLatitude());

sb.append("\nlontitude : ");

sb.append(location.getLongitude());

sb.append("\nradius : ");

sb.append(location.getRadius());

if(location.getLocType() == BDLocation.TypeGpsLocation){// GPS定位结果

sb.append("\nspeed : ");

sb.append(location.getSpeed());//单位:公里每小时

sb.append("\nsatellite : ");

sb.append(location.getSatelliteNumber());

sb.append("\nheight : ");

sb.append(location.getAltitude());//单位:米

sb.append("\ndirection : ");

sb.append(location.getDirection());//单位度

sb.append("\naddr : ");

sb.append(location.getAddrStr());

sb.append("\ndescribe : ");

sb.append("gps定位成功");

}else if(location.getLocType() == BDLocation.TypeNetWorkLocation){//网络定位结果

sb.append("\naddr : ");

sb.append(location.getAddrStr());

sb.append("\ncity:");

sb.append(location.getCity());

//运营商信息

sb.append("\noperationers : ");

sb.append(location.getOperators());

sb.append("\ndescribe : ");

sb.append("网络定位成功");

}else if(location.getLocType() == BDLocation.TypeOffLineLocation) {//离线定位结果

sb.append("\ndescribe : ");

sb.append("离线定位成功,离线定位结果也是有效的");

}else if(location.getLocType() == BDLocation.TypeServerError) {

sb.append("\ndescribe : ");

sb.append("服务端网络定位失败,可以反馈IMEI号和大体定位时间到loc-bugs@baidu.com,会有人追查原因");

}else if(location.getLocType() == BDLocation.TypeNetWorkException) {

sb.append("\ndescribe : ");

sb.append("网络不同导致定位失败,请检查网络是否通畅");

}else if(location.getLocType() == BDLocation.TypeCriteriaException) {

sb.append("\ndescribe : ");

sb.append("无法获取有效定位依据导致定位失败,一般是由于手机的原因,处于飞行模式下一般会造成这种结果,可以试着重启手机");

}

sb.append("\nlocationdescribe : ");

sb.append(location.getLocationDescribe());//位置语义化信息

List list = location.getPoiList();// POI数据

if(list !=null) {

sb.append("\npoilist size = : ");

sb.append(list.size());

for(Poi p : list) {

sb.append("\npoi= : ");

sb.append(p.getId() +" "+ p.getName() +" "+ p.getRank());

}

}

Log.i("yunxiao",sb.toString());

}}

}

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

相关阅读更多精彩内容

  • packagecom.jiahao.baidulocation; importandroid.content.Co...
    海芋洋芋阅读 4,145评论 0 0
  • Spring Cloud为开发人员提供了快速构建分布式系统中一些常见模式的工具(例如配置管理,服务发现,断路器,智...
    卡卡罗2017阅读 135,871评论 19 139
  • 百度定位API使用方法 导入库文件 在下载页面下载最新的库文件。将liblocSDK2.4.so文件拷贝到libs...
    cxm11阅读 9,610评论 0 2
  • 来聊聊怎么接入百度地图吧 百度地图SDK接入 其实这个环境配置在百度官方的介绍上面已经很详细了,我也差不多是都是根...
    Discredited阅读 4,071评论 0 0
  • 说话是权利关系,求帮忙选择权给别人,反着说话(我早退—你们都醉了我还没有醉)
    陈东Growth阅读 1,387评论 0 0

友情链接更多精彩内容