BdLocationHelper.java 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183
  1. package com.xzjmyk.pm.activity;
  2. import android.content.Context;
  3. import android.content.Intent;
  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.baidu.location.Poi;
  10. import com.xzjmyk.pm.activity.sp.LocationSp;
  11. import java.util.List;
  12. public class BdLocationHelper {
  13. public static final String ACTION_LOCATION_UPDATE = AppConfig.sPackageName + ".action.location_update";
  14. private Context mContext;
  15. private double mLongitude;
  16. private double mLatitude;
  17. private String mAddress;
  18. private String name;
  19. private String mProvinceName;// 省份
  20. private String mCityName;// 城市
  21. private String mDistrictName;// 街道
  22. private boolean isLocationUpdate;// 本次程序启动后,位置有没有成功更新一次
  23. private LocationClient mLocationClient = null;
  24. private int mFaildCount = 0;
  25. private List<Poi> pois;
  26. BdLocationHelper(Context context) {
  27. mContext = context;
  28. // 获取上一次的定位数据
  29. mLongitude = LocationSp.getInstance(context).getLongitude(0);
  30. mLatitude = LocationSp.getInstance(context).getLatitude(0);
  31. mAddress = LocationSp.getInstance(context).getAddress("");
  32. mProvinceName = LocationSp.getInstance(context).getProvinceName("");
  33. mCityName = LocationSp.getInstance(context).getCityName("");
  34. mDistrictName = LocationSp.getInstance(context).getDistrictName("");
  35. mLocationClient = new LocationClient(context); // 声明LocationClient类
  36. mLocationClient.registerLocationListener(mMyLocationListener); // 注册监听函数
  37. LocationClientOption option = new LocationClientOption();
  38. option.setLocationMode(LocationClientOption.LocationMode.Hight_Accuracy);//可选,默认高精度,设置定位模式,高精度,低功耗,仅设备
  39. option.setCoorType("bd09ll");//可选,默认gcj02,设置返回的定位结果坐标系
  40. option.setScanSpan(5000);// 设置发起定位请求的间隔时间为10s;//可选,默认0,即仅定位一次,设置发起定位请求的间隔需要大于等于1000ms才是有效的
  41. option.setIsNeedAddress(true);//可选,设置是否需要地址信息,默认不需要
  42. option.setOpenGps(true);//可选,默认false,设置是否使用gps
  43. option.setLocationNotify(true);//可选,默认false,设置是否当gps有效时按照1S1次频率输出GPS结果
  44. option.setIsNeedLocationDescribe(true);//可选,默认false,设置是否需要位置语义化结果,可以在BDLocation.getLocationDescribe里得到,结果类似于“在北京天安门附近”
  45. option.setIsNeedLocationPoiList(true);//可选,默认false,设置是否需要POI结果,可以在BDLocation.getPoiList里得到
  46. option.setIgnoreKillProcess(false);//可选,默认true,定位SDK内部是一个SERVICE,并放到了独立进程,设置是否在stop的时候杀死这个进程,默认不杀死
  47. option.SetIgnoreCacheException(false);//可选,默认false,设置是否收集CRASH信息,默认收集
  48. option.setEnableSimulateGps(false);//可选,默认false,设置是否需要过滤gps仿真结果,默认需要
  49. option.setNeedDeviceDirect(false);
  50. mLocationClient.setLocOption(option);
  51. requestLocation();//重新定位
  52. }
  53. public void release() {
  54. if (mLocationClient.isStarted()) {
  55. mLocationClient.stop();
  56. }
  57. }
  58. public void requestLocation() {
  59. if (!mLocationClient.isStarted()) {
  60. mFaildCount = 0;
  61. mLocationClient.start();
  62. } else {
  63. int scanSpan = mLocationClient.getLocOption().getScanSpan();
  64. if (scanSpan < 1000) {
  65. mLocationClient.getLocOption().setScanSpan(5000);
  66. }
  67. }
  68. }
  69. private BDLocationListener mMyLocationListener = new BDLocationListener() {
  70. @Override
  71. public void onReceiveLocation(BDLocation location) {
  72. int resultCode = 0;
  73. if (location != null) {
  74. resultCode = location.getLocType();
  75. }
  76. // 百度定位失败
  77. if (resultCode != BDLocation.TypeGpsLocation && resultCode != BDLocation.TypeCacheLocation
  78. && resultCode != BDLocation.TypeOffLineLocation && resultCode != BDLocation.TypeNetWorkLocation) {
  79. if (AppConfig.DEBUG) {
  80. Log.d(AppConfig.TAG, "百度定位失败");
  81. }
  82. mFaildCount++;
  83. if (mFaildCount > 3) {// 停止定位
  84. mLocationClient.stop();
  85. }
  86. return;
  87. }
  88. Log.i("gongpengming", "进来的");
  89. // 百度定位成功
  90. mLongitude = location.getLongitude();
  91. mLatitude = location.getLatitude();
  92. name = location.getLocationDescribe();
  93. pois = location.getPoiList();
  94. if (location.getLocType() == BDLocation.TypeNetWorkLocation) {
  95. mAddress = location.getAddrStr();
  96. mProvinceName = location.getProvince();
  97. mCityName = location.getCity();
  98. mDistrictName = location.getDistrict();
  99. if (AppConfig.DEBUG) {
  100. Log.d(AppConfig.TAG,
  101. "百度定位信息 City:" + location.getCity() + " CityCode:" + location.getCityCode() + " 区:" + location.getDistrict());
  102. }
  103. }
  104. if (!isLocationUpdate) {
  105. LocationSp.getInstance(mContext).setLongitude((float) mLongitude);
  106. LocationSp.getInstance(mContext).setLatitude((float) mLatitude);
  107. LocationSp.getInstance(mContext).setAddress(mAddress);
  108. LocationSp.getInstance(mContext).setProvinceName(mProvinceName);
  109. LocationSp.getInstance(mContext).setCityName(mCityName);
  110. LocationSp.getInstance(mContext).setDistrictName(mDistrictName);
  111. isLocationUpdate = true;
  112. }
  113. if (AppConfig.DEBUG) {
  114. Log.d(AppConfig.TAG, "百度定位信息 mLongitude:" + mLongitude + " mLatitude:" + mLatitude + " mAddressDetail:" + mAddress);
  115. }
  116. mLocationClient.stop();
  117. // if (isTimingScan()) {// 停止定时定位
  118. // mLocationClient.getLocOption().setScanSpan(100);
  119. // }
  120. mContext.sendBroadcast(new Intent(ACTION_LOCATION_UPDATE));// 发送广播
  121. }
  122. };
  123. public String getName() {
  124. return pois == null ? (name == null ? "当前位置" : name) : (pois.get(0) == null ? (name == null ? "当前位置" : name) : pois.get(0).getName());
  125. }
  126. public void setName(String name) {
  127. this.name = name;
  128. }
  129. // 获取经纬度
  130. public double getLongitude() {
  131. return mLongitude;
  132. }
  133. // 获取经纬度
  134. public double getLatitude() {
  135. return mLatitude;
  136. }
  137. // 获取地址详情
  138. public String getAddress() {
  139. return mAddress;
  140. }
  141. public String getProvinceName() {
  142. return mProvinceName;
  143. }
  144. public String getCityName() {
  145. return mCityName;
  146. }
  147. public String getDistrictName() {
  148. return mDistrictName;
  149. }
  150. public boolean isLocationUpdate() {
  151. return isLocationUpdate;
  152. }
  153. public boolean hasData() {
  154. return mLatitude != 0 && mLongitude != 0;
  155. }
  156. }