前言
在我们日常生活中常常能看到手机定位的身影,比如出行线路查询,附近的人等等功能,在开发中定位也往往是必不可少的,下面我就来简单的介绍下如何集成百度定位到我们的app中。
集成百度定位插件
百度定位
- 百度地图API 首先我们需要到百度地图的官网上下载对应的jar包,如下图所示,我们可以根据自己的需要集成不同的功能,这里由于我们只需要使用定位功能,而不用地图,导航等功能,所以选择Android平台下的Android定位SDK。

- Android定位SDK 在这个页面的左边有一个开发指南,以及相关下载,我们可以在相关下载里面下载对应的jar包,然后按照开发指南的步骤一步步集成到我们的App中,由于文档非常清晰了,所以不再废话的去多说了。一共是以下几个步骤。申请密钥,配置环境,然后就是使用了,不懂的地方我们可以参看官方demo。重点的部分已经在下图标出。

在代码中使用百度地图接口获取地理信息
类似于Android原生的定位方式,一共分为三个步骤
第一步,初始化LocationClient类
此处需要注意:LocationClient类必须在主线程中声明。需要Context类型的参数。
Context需要时全进程有效的context,推荐用getApplicationConext获取全进程有效的context。
private LocationClient client = null;
client = new LocationClient(getApplicationContext());
第二步,配置定位SDK参数
设置定位参数包括:定位模式(高精度定位模式,低功耗定位模式和仅用设备定位模式),返回坐标类型,是否打开GPS,是否返回地址信息、位置语义化信息、POI信息等等。
LocationClientOption类,该类用来设置定位SDK的定位方式。
public LocationClientOption getDefaultLocationClientOption() {
if (mOption == null) {
mOption = new LocationClientOption();
mOption.setLocationMode(LocationMode.Hight_Accuracy);// 可选,默认高精度,设置定位模式,高精度,低功耗,仅设备
mOption.setCoorType("bd09ll");// 可选,默认gcj02,设置返回的定位结果坐标系,如果配合百度地图使用,建议设置为bd09ll;
mOption.setScanSpan(0);// 可选,默认0,即仅定位一次,设置发起定位请求的间隔需要大于等于1000ms才是有效的
mOption.setIsNeedAddress(true);// 可选,设置是否需要地址信息,默认不需要
mOption.setIsNeedLocationDescribe(true);// 可选,设置是否需要地址描述
mOption.setNeedDeviceDirect(false);// 可选,设置是否需要设备方向结果
mOption.setLocationNotify(false);// 可选,默认false,设置是否当gps有效时按照1S1次频率输出GPS结果
mOption.setIgnoreKillProcess(true);// 可选,默认true,定位SDK内部是一个SERVICE,并放到了独立进程,设置是否在stop的时候杀死这个进程,默认不杀死
mOption.setIsNeedLocationDescribe(true);// 可选,默认false,设置是否需要位置语义化结果,可以在BDLocation.getLocationDescribe里得到,结果类似于“在北京天安门附近”
mOption.setIsNeedLocationPoiList(true);// 可选,默认false,设置是否需要POI结果,可以在BDLocation.getPoiList里得到
mOption.SetIgnoreCacheException(false);// 可选,默认false,设置是否收集CRASH信息,默认收集
}
return mOption;
}
第三步,实现BDLocationListener接口,这个接口是接受定位结果的
BDLocationListener接口有1个方法需要实现: 1.接收异步返回的定位结果,参数是BDLocation类型参数。
mLocationService.registerListener(new BDLocationListener() {
@Override
public void onReceiveLocation(BDLocation location) {
String str = location.getCity();
// mResult.setText(str);
// Receive Location
StringBuffer sb = new StringBuffer(256);
sb.append("time : ");
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("\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<Poi> 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("BaiduLocationApiDem", sb.toString());
mResult.setText(sb.toString());
}
});
更加具体的介绍请参看官方帮助文档。
实际Demo
附上一个集成百度定位的小Demo。

上图Demo+官方Demo+SDK:360云盘 访问密码 fe0b