BdLocationHelper.java 9.4 KB

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