BdLocationHelper.java 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229
  1. package com.xzjmyk.pm.activity;
  2. import android.content.Intent;
  3. import android.support.v4.content.LocalBroadcastManager;
  4. import com.baidu.location.BDLocation;
  5. import com.baidu.location.BDLocationListener;
  6. import com.baidu.location.LocationClient;
  7. import com.baidu.location.LocationClientOption;
  8. import com.baidu.mapapi.model.LatLng;
  9. import com.common.LogUtil;
  10. import com.common.data.StringUtil;
  11. import com.common.preferences.PreferenceUtils;
  12. public class BdLocationHelper {
  13. public static final String UPLOCATION_ACTION = "UPLOCATION_ACTION";//更新位置时候广播数据
  14. private boolean locationOk;//是否定位成功
  15. private LatLng latLng;
  16. private String location;//位置信息
  17. private String address;//详细地址
  18. private String province;// 省份
  19. private String cityName;// 城市
  20. private String district;// 街道
  21. private String errorMessage;//定位错误信息
  22. private final Intent broadcast = new Intent(UPLOCATION_ACTION);//更新后广播
  23. private LocationClient mLocationClient = null;
  24. private int mFaildCount = 0;//失败次数
  25. public BdLocationHelper() {
  26. initLocation();
  27. }
  28. private void initLocation() {
  29. mLocationClient = new LocationClient(MyApplication.getInstance().getApplicationContext()); // 声明LocationClient类
  30. mLocationClient.registerLocationListener(mLocationListener); // 注册监听函数
  31. mLocationClient.setLocOption(getOptionByGPS());
  32. // requestLocation();
  33. }
  34. /**
  35. * 获取配置
  36. *
  37. * @return
  38. */
  39. private LocationClientOption getOptionByGPS() {
  40. LocationClientOption option = new LocationClientOption();
  41. option.setLocationMode(LocationClientOption.LocationMode.Hight_Accuracy);//可选,默认高精度,设置定位模式,高精度,低功耗,仅设备
  42. option.setCoorType("bd09ll");//可选,默认gcj02,设置返回的定位结果坐标系
  43. option.setScanSpan(20 * 1000);//可选,默认0,即仅定位一次,设置发起定位请求的间隔需要大于等于1000ms才是有效的
  44. option.setIsNeedAddress(true);//可选,设置是否需要地址信息,默认不需要
  45. option.setOpenGps(true);//可选,默认false,设置是否使用gps
  46. option.setLocationNotify(false);//可选,默认false,设置是否当GPS有效时按照1S/1次频率输出GPS结果
  47. option.setIsNeedLocationDescribe(true);//可选,默认false,设置是否需要位置语义化结果,可以在BDLocation.getLocationDescribe里得到,结果类似于“在北京天安门附近”
  48. option.setIsNeedLocationPoiList(false);//可选,默认false,设置是否需要POI结果,可以在BDLocation.getPoiList里得到
  49. option.setIgnoreKillProcess(false);//可选,默认true,定位SDK内部是一个SERVICE,并放到了独立进程,设置是否在stop的时候杀死这个进程,默认不杀死
  50. option.SetIgnoreCacheException(false);//可选,默认false,设置是否收集CRASH信息,默认收集
  51. option.setEnableSimulateGps(true);//可选,默认false,设置是否需要过滤GPS仿真结果,默认需要
  52. option.setNeedDeviceDirect(false);
  53. return option;
  54. }
  55. /**
  56. * 获取配置
  57. *
  58. * @return
  59. */
  60. private LocationClientOption getOptionNotGPS() {
  61. LocationClientOption option = getOptionByGPS();
  62. option.setOpenGps(false);
  63. return option;
  64. }
  65. /**
  66. * 关闭定位
  67. **/
  68. public void release() {
  69. if (mLocationClient.isStarted()) {
  70. mLocationClient.stop();
  71. }
  72. }
  73. /**
  74. * 重新定位
  75. **/
  76. public void requestLocation() {
  77. if (mLocationClient == null) return;
  78. if (!mLocationClient.isStarted()) {
  79. mFaildCount = 0;
  80. mLocationClient.start();
  81. } else {
  82. mLocationClient.requestLocation();
  83. }
  84. }
  85. private BDLocationListener mLocationListener = new BDLocationListener() {
  86. @Override
  87. public void onReceiveLocation(BDLocation location) {
  88. try {
  89. setLocation(location);
  90. } catch (Exception e) {
  91. if (e != null)
  92. log("onReceiveLocation Exception" + e.getMessage());
  93. clearLocation();
  94. }
  95. }
  96. };
  97. private void setLocation(BDLocation location) throws Exception {
  98. if (location.getLocType() == BDLocation.TypeGpsLocation// GPS定位结果
  99. || location.getLocType() == BDLocation.TypeNetWorkLocation//网络定位
  100. || location.getLocType() == BDLocation.TypeOffLineLocation//离线定位(未验证离线定位的有效性)
  101. ) {
  102. //定位成功
  103. locationOk = true;
  104. latLng = new LatLng(location.getLatitude(), location.getLongitude());
  105. this.location = location.getLocationDescribe();
  106. province = location.getProvince();
  107. cityName = location.getCity();
  108. district = location.getDistrict();
  109. address = location.getAddrStr();
  110. if (!StringUtil.isEmpty(this.location))
  111. PreferenceUtils.putString("bdlocation", this.location);
  112. if (!StringUtil.isEmpty(address))
  113. PreferenceUtils.putString("bdaddress", address);
  114. errorMessage = "";
  115. } else {
  116. //统一为定位失败
  117. locationOk = false;
  118. log("定位失败");
  119. if (location.getLocType() == BDLocation.TypeServerError) {
  120. //服务端网络定位失败,可以反馈IMEI号和大体定位时间到loc-bugs@baidu.com,会有人追查原因
  121. errorMessage = "服务端网络定位失败";
  122. log("服务端网络定位失败");
  123. } else if (location.getLocType() == BDLocation.TypeNetWorkException) {
  124. //网络不同导致定位失败,请检查网络是否通畅
  125. errorMessage = "网络不同导致定位失败,请检查网络是否通畅";
  126. log("网络不同导致定位失败,请检查网络是否通畅");
  127. } else if (location.getLocType() == BDLocation.TypeCriteriaException) {
  128. //无法获取有效定位依据导致定位失败,一般是由于手机的原因,处于飞行模式下一般会造成这种结果,可以试着重启手机
  129. errorMessage = "无法获取有效定位依据导致定位失败";
  130. log("无法获取有效定位依据导致定位失败");
  131. } else {
  132. errorMessage = "未知错误";
  133. log("未知错误");
  134. }
  135. if (mFaildCount < 3) {
  136. mFaildCount++;
  137. requestLocation();
  138. return;
  139. }
  140. }
  141. //发送广播
  142. broadcast.putExtra(UPLOCATION_ACTION, locationOk);
  143. LocalBroadcastManager.getInstance(MyApplication.getInstance()).sendBroadcast(broadcast);
  144. }
  145. private void log(String message) {
  146. LogUtil.i(message);
  147. }
  148. public String getName() {
  149. return StringUtil.isEmpty(location) ? "" : location;
  150. }
  151. // 获取经纬度
  152. public double getLongitude() {
  153. return latLng == null ? 1 : latLng.longitude;
  154. }
  155. // 获取经纬度
  156. public double getLatitude() {
  157. return latLng == null ? 1 : latLng.latitude;
  158. }
  159. // 获取地址详情
  160. public String getAddress() {
  161. return StringUtil.isEmpty(address) ? PreferenceUtils.getString("bdaddress") : address;
  162. }
  163. public String getProvinceName() {
  164. return province;
  165. }
  166. public LatLng getLocation() {
  167. return latLng;
  168. }
  169. public String getCityName() {
  170. return cityName;
  171. }
  172. public String getDistrictName() {
  173. return district;
  174. }
  175. public boolean locationOk() {
  176. return locationOk;
  177. }
  178. public String getErrorMessage() {
  179. return errorMessage;
  180. }
  181. public boolean isLocationUpdate() {
  182. return true;
  183. }
  184. private void clearLocation() {
  185. locationOk = false;
  186. latLng = null;
  187. location = null;
  188. address = null;
  189. province = null;
  190. cityName = null;
  191. district = null;
  192. errorMessage = null;
  193. }
  194. }