百度地图定位功能的封装

packagecom.jiahao.baidulocation;

importandroid.content.Context;

importandroid.os.Handler;

importandroid.os.Looper;

importandroid.util.Log;

importcom.baidu.location.BDLocation;

importcom.baidu.location.BDLocationListener;

importcom.baidu.location.LocationClient;

importcom.baidu.location.LocationClientOption;

importcom.baidu.location.Poi;

importjava.util.List;

/**

* Created by Administrator on 2017/5/25.

*/

public classBaiduLocationManager {

private staticBaiduLocationManagermInstance;

privateLocationClientlocationClient;

private staticBDLocationListenermyListener;

privateLocationBeanmLocationBean;

privateHandlermHandler;

privateonLocationListenermLocationListener;

privateBaiduLocationManager(Context context){

//声明LocationClient类

locationClient=newLocationClient(context);

//注册监听函数

myListener=newMyLocationListener();

locationClient.registerLocationListener(myListener);

mHandler=newHandler(Looper.getMainLooper());

}

public staticBaiduLocationManager getInstance(Context context){

if(mInstance==null&&myListener==null){

mInstance=newBaiduLocationManager(context);

}

returnmInstance;

}

private classMyLocationListenerimplementsBDLocationListener {

@Override

public voidonReceiveLocation(BDLocation location) {

mLocationBean=newLocationBean();

//获取定位结果

StringBuffer sb =newStringBuffer(256);

sb.append("time : ");

sb.append(location.getTime());//获取定位时间

mLocationBean.setLocationTime(location.getTime());

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

sb.append(location.getLocType());//获取类型类型

sb.append("\nlatitude : ");

sb.append(location.getLatitude());//获取纬度信息

mLocationBean.setLocationLatitude(location.getLatitude());

sb.append("\nlontitude : ");

sb.append(location.getLongitude());//获取经度信息

mLocationBean.setLocationLongitude(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());//获取地址信息

mLocationBean.setLocationAddress(location.getAddrStr());

sb.append("\ndescribe : ");

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

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

// 网络定位结果

sb.append("\naddr : ");

sb.append(location.getAddrStr());//获取地址信息

mLocationBean.setLocationAddress(location.getAddrStr());

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());//位置语义化信息

mLocationBean.setLocationDescribe(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());

}

mLocationListener.onSuccess(mLocationBean);

}else{

mLocationListener.onFile();

}

// Log.i("BaiduLocationApiDem", sb.toString());

}

@Override

public voidonConnectHotSpotMessage(String s,inti) {

}

}

/**

* 开始定位

*/

public voidstartBaiduLocation(){

locationClient.start();

initLocation();

}

public voidgetmLocationBean(onLocationListener listener){

this.mLocationListener= listener;

}

/**

* 结束定位

*/

public voidstopBaiduLocation(){

locationClient.stop();

}

/**

* 设置参数

*/

private voidinitLocation(){

LocationClientOption option =newLocationClientOption();

option.setLocationMode(LocationClientOption.LocationMode.Hight_Accuracy);

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

option.setCoorType("bd09ll");

//可选,默认gcj02,设置返回的定位结果坐标系

intspan=1000;

option.setScanSpan(span);

//可选,默认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仿真结果,默认需要

locationClient.setLocOption(option);

}

/**

* 自己定义的bean类

*/

public classLocationBean {

//定位时间

privateStringlocationTime;

//纬度

privateDoublelocationLatitude;

//经度

privateDoublelocationLongitude;

//定位位置

privateStringlocationAddress;

//语义化

privateStringlocationDescribe;

publicString getLocationTime() {

returnlocationTime;

}

public voidsetLocationTime(String locationTime) {

this.locationTime= locationTime;

}

publicDouble getLocationLatitude() {

returnlocationLatitude;

}

public voidsetLocationLatitude(Double locationLatitude) {

this.locationLatitude= locationLatitude;

}

publicDouble getLocationLongitude() {

returnlocationLongitude;

}

public voidsetLocationLongitude(Double locationLongitude) {

this.locationLongitude= locationLongitude;

}

publicString getLocationAddress() {

returnlocationAddress;

}

public voidsetLocationAddress(String locationAddress) {

this.locationAddress= locationAddress;

}

publicString getLocationDescribe() {

returnlocationDescribe;

}

public voidsetLocationDescribe(String locationDescribe) {

this.locationDescribe= locationDescribe;

}

@Override

publicString toString() {

return"LocationBean{"+

"locationTime='"+locationTime+'\''+

", locationLatitude="+locationLatitude+

", locationLongitude="+locationLongitude+

", locationAddress='"+locationAddress+'\''+

", locationDescribe='"+locationDescribe+'\''+

'}';

}

}

interfaceonLocationListener{

voidonSuccess(LocationBean locationBean);

voidonFile();

}

}



使用:

packagecom.jiahao.baidulocation;

importandroid.support.v7.app.AppCompatActivity;

importandroid.os.Bundle;

importandroid.util.Log;

importcom.baidu.location.BDLocation;

importcom.baidu.location.BDLocationListener;

importcom.baidu.location.LocationClient;

importcom.baidu.location.LocationClientOption;

importcom.baidu.location.Poi;

importjava.util.List;

public classMainActivityextendsAppCompatActivity {

privateBaiduLocationManagerbaiduLocationManager;

@Override

protected voidonCreate(Bundle savedInstanceState) {

super.onCreate(savedInstanceState);

setContentView(R.layout.activity_main);

baiduLocationManager=  BaiduLocationManager.getInstance(getApplicationContext());

baiduLocationManager.startBaiduLocation();

baiduLocationManager.getmLocationBean(newBaiduLocationManager.onLocationListener() {

@Override

public voidonSuccess(BaiduLocationManager.LocationBean locationBean) {

Log.i("BaiduLocationApiDem", locationBean.toString());

baiduLocationManager.stopBaiduLocation();

}

@Override

public voidonFile() {

Log.i("BaiduLocationApiDem","失败了");

}

});

}

@Override

protected voidonDestroy() {

super.onDestroy();

baiduLocationManager.stopBaiduLocation();

}

}

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 156,265评论 4 359
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 66,274评论 1 288
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 106,087评论 0 237
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 43,479评论 0 203
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 51,782评论 3 285
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 40,218评论 1 207
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 31,594评论 2 309
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 30,316评论 0 194
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 33,955评论 1 237
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 30,274评论 2 240
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 31,803评论 1 255
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 28,177评论 2 250
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 32,732评论 3 229
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 25,953评论 0 8
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 26,687评论 0 192
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 35,263评论 2 267
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 35,189评论 2 258

推荐阅读更多精彩内容

  • (题外话:up主开发android开发快三年了,以前遇到的问题都是自己解决完就可以了,不过在简书看到之前的同事,也...
    爱笑的臭淇阅读 2,398评论 1 1
  • Spring Cloud为开发人员提供了快速构建分布式系统中一些常见模式的工具(例如配置管理,服务发现,断路器,智...
    卡卡罗2017阅读 134,083评论 18 139
  • 来聊聊怎么接入百度地图吧 百度地图SDK接入 其实这个环境配置在百度官方的介绍上面已经很详细了,我也差不多是都是根...
    Discredited阅读 652评论 0 0
  • 【 申请密钥AndroidSDK:应用程序包名+数字签名 As查看数字签名:cmd-----cd .android...
    征程_Journey阅读 2,190评论 0 5
  • 01 现在大家都知道罗恩和羽的关系了。 虽然他们在公众面前从来都显得互不相识一样。 只是这种事就像一块臭豆腐...
    龙女卷风阅读 640评论 0 0