|
|
@@ -1,74 +1,78 @@
|
|
|
package com.xzjmyk.pm.activity;
|
|
|
|
|
|
-import android.content.Intent;
|
|
|
+import android.content.Context;
|
|
|
import android.util.Log;
|
|
|
|
|
|
import com.baidu.location.BDLocation;
|
|
|
import com.baidu.location.BDLocationListener;
|
|
|
import com.baidu.location.LocationClient;
|
|
|
import com.baidu.location.LocationClientOption;
|
|
|
-import com.xzjmyk.pm.activity.bean.LocationEntity;
|
|
|
+import com.baidu.location.Poi;
|
|
|
+import com.xzjmyk.pm.activity.sp.LocationSp;
|
|
|
import com.xzjmyk.pm.activity.ui.erp.util.StringUtils;
|
|
|
-import com.xzjmyk.pm.activity.ui.tool.ThreadUtil;
|
|
|
|
|
|
-import static com.xzjmyk.pm.activity.ui.erp.activity.oa.WorkLogsActivity.resultCode;
|
|
|
+import java.util.List;
|
|
|
|
|
|
public class BdLocationHelper {
|
|
|
- public static final String UPLOCATION_ACTION = "UPLOCATION_ACTION";//更新位置时候广播数据
|
|
|
- private LocationEntity locationEntity;//位置对象
|
|
|
- private final Intent broadcast = new Intent(UPLOCATION_ACTION);//更新后广播
|
|
|
- private LocationClient mLocationClient = null;
|
|
|
- private int mFaildCount = 0;//失败次数
|
|
|
- private int scanSpan = 0;
|
|
|
- private boolean runingLocation = false;
|
|
|
-
|
|
|
- BdLocationHelper() {
|
|
|
- initLocation();
|
|
|
- }
|
|
|
|
|
|
- private void initLocation() {
|
|
|
- mLocationClient = new LocationClient(MyApplication.getInstance().getApplicationContext()); // 声明LocationClient类
|
|
|
- mLocationClient.registerLocationListener(mLocationListener); // 注册监听函数
|
|
|
- mLocationClient.setLocOption(getOptionByGPS());
|
|
|
- }
|
|
|
-
|
|
|
- /**
|
|
|
- * 获取配置
|
|
|
- *
|
|
|
- * @return
|
|
|
- */
|
|
|
- private LocationClientOption getOptionByGPS() {
|
|
|
+ private Context mContext;
|
|
|
+ private double mLongitude;
|
|
|
+ private double mLatitude;
|
|
|
+ private String mAddress;
|
|
|
+ private String name;
|
|
|
+ private String mProvinceName;// 省份
|
|
|
+ private String mCityName;// 城市
|
|
|
+ private String mDistrictName;// 街道
|
|
|
+ private boolean isLocationUpdate;// 本次程序启动后,位置有没有成功更新一次
|
|
|
+ private LocationClient mLocationClient = null;
|
|
|
+ private int mFaildCount = 0;
|
|
|
+ private int resultCode;
|
|
|
+ private List<Poi> pois;
|
|
|
+ private int scanSpan = 20 * 1000;
|
|
|
+
|
|
|
+ BdLocationHelper(Context context) {
|
|
|
+ mContext = context;
|
|
|
+ initLocation(context);
|
|
|
+ }
|
|
|
+
|
|
|
+ private void initLocation(Context context) {
|
|
|
+
|
|
|
+ // 获取上一次的定位数据
|
|
|
+// mLongitude = LocationSp.getInstance(context).getLongitude(0);
|
|
|
+// mLatitude = LocationSp.getInstance(context).getLatitude(0);
|
|
|
+// mAddress = LocationSp.getInstance(context).getAddress("");
|
|
|
+// mProvinceName = LocationSp.getInstance(context).getProvinceName("");
|
|
|
+// mCityName = LocationSp.getInstance(context).getCityName("");
|
|
|
+// mDistrictName = LocationSp.getInstance(context).getDistrictName("");
|
|
|
+ //关闭缓存
|
|
|
+ mLongitude = 0;
|
|
|
+ mLatitude = 0;
|
|
|
+ mAddress = "";
|
|
|
+ mProvinceName = "";
|
|
|
+ mCityName = "";
|
|
|
+ mDistrictName = "";
|
|
|
+
|
|
|
+ mLocationClient = new LocationClient(context.getApplicationContext()); // 声明LocationClient类
|
|
|
+ mLocationClient.registerLocationListener(mMyLocationListener); // 注册监听函数
|
|
|
LocationClientOption option = new LocationClientOption();
|
|
|
option.setLocationMode(LocationClientOption.LocationMode.Hight_Accuracy);//可选,默认高精度,设置定位模式,高精度,低功耗,仅设备
|
|
|
- option.setCoorType("bd09ll");//可选,默认gcj02,设置返回的定位结果坐标系
|
|
|
- option.setScanSpan(0);//可选,默认0,即仅定位一次,设置发起定位请求的间隔需要大于等于1000ms才是有效的
|
|
|
+ option.setCoorType("bd09ll");//可选,默认gcj02 bd09ll,设置返回的定位结果坐标系
|
|
|
+ option.setScanSpan(scanSpan);// 设置发起定位请求的间隔时间为10s;//可选,默认0,即仅定位一次,设置发起定位请求的间隔需要大于等于1000ms才是有效的
|
|
|
option.setIsNeedAddress(true);//可选,设置是否需要地址信息,默认不需要
|
|
|
option.setOpenGps(true);//可选,默认false,设置是否使用gps
|
|
|
- option.setLocationNotify(false);//可选,默认false,设置是否当GPS有效时按照1S/1次频率输出GPS结果
|
|
|
+ option.setLocationNotify(true);//可选,默认false,设置是否当gps有效时按照1S1次频率输出GPS结果
|
|
|
option.setIsNeedLocationDescribe(true);//可选,默认false,设置是否需要位置语义化结果,可以在BDLocation.getLocationDescribe里得到,结果类似于“在北京天安门附近”
|
|
|
- option.setIsNeedLocationPoiList(false);//可选,默认false,设置是否需要POI结果,可以在BDLocation.getPoiList里得到
|
|
|
+ option.setIsNeedLocationPoiList(true);//可选,默认false,设置是否需要POI结果,可以在BDLocation.getPoiList里得到
|
|
|
option.setIgnoreKillProcess(false);//可选,默认true,定位SDK内部是一个SERVICE,并放到了独立进程,设置是否在stop的时候杀死这个进程,默认不杀死
|
|
|
option.SetIgnoreCacheException(false);//可选,默认false,设置是否收集CRASH信息,默认收集
|
|
|
- option.setEnableSimulateGps(true);//可选,默认false,设置是否需要过滤GPS仿真结果,默认需要
|
|
|
+ option.setEnableSimulateGps(true);//可选,默认false,设置是否需要过滤gps仿真结果,默认需要
|
|
|
option.setNeedDeviceDirect(false);
|
|
|
- return option;
|
|
|
+// option.disableCache(true);//是否禁用缓存数据
|
|
|
+ mLocationClient.setLocOption(option);
|
|
|
+ //requestLocation();//重新定位
|
|
|
}
|
|
|
|
|
|
- /**
|
|
|
- * 获取配置
|
|
|
- *
|
|
|
- * @return
|
|
|
- */
|
|
|
- private LocationClientOption getOptionNotGPS() {
|
|
|
- LocationClientOption option = getOptionByGPS();
|
|
|
- option.setOpenGps(false);
|
|
|
- return option;
|
|
|
- }
|
|
|
|
|
|
-
|
|
|
- /**
|
|
|
- * 关闭定位
|
|
|
- **/
|
|
|
public void release() {
|
|
|
if (mLocationClient.isStarted()) {
|
|
|
mLocationClient.stop();
|
|
|
@@ -77,142 +81,133 @@ public class BdLocationHelper {
|
|
|
|
|
|
private static final String TAG = "BdLocationHelper";
|
|
|
|
|
|
- /**
|
|
|
- * 重新定位
|
|
|
- **/
|
|
|
public void requestLocation() {
|
|
|
- if (mLocationClient == null) return;
|
|
|
- if (!mLocationClient.isStarted()) {
|
|
|
+ if (mLocationClient != null && !mLocationClient.isStarted()) {
|
|
|
mFaildCount = 0;
|
|
|
mLocationClient.start();
|
|
|
+ Log.i(TAG, "requestLocation:start()");
|
|
|
} else {
|
|
|
- mLocationClient.requestLocation();
|
|
|
- }
|
|
|
- }
|
|
|
-
|
|
|
- /**
|
|
|
- * 重新定位,循环
|
|
|
- **/
|
|
|
- public void requestLocation(final long time) {
|
|
|
- if (runingLocation || mLocationClient == null) {//如果已经启动了
|
|
|
- return;
|
|
|
- }
|
|
|
- ThreadUtil.getInstance().addLoopTask(new Runnable() {
|
|
|
- @Override
|
|
|
- public void run() {
|
|
|
- while (runingLocation) {
|
|
|
- mLocationClient.requestNotifyLocation();
|
|
|
- try {
|
|
|
- Thread.sleep(time);
|
|
|
- } catch (InterruptedException e) {
|
|
|
- e.printStackTrace();
|
|
|
- }
|
|
|
- }
|
|
|
+ int scanSpan = mLocationClient.getLocOption().getScanSpan();
|
|
|
+ if (scanSpan < 1000) {
|
|
|
+ mLocationClient.getLocOption().setScanSpan(5000);
|
|
|
}
|
|
|
- });
|
|
|
+ Log.i(TAG, "requestLocation:request()");
|
|
|
+ }
|
|
|
}
|
|
|
|
|
|
- private BDLocationListener mLocationListener = new BDLocationListener() {
|
|
|
+ private BDLocationListener mMyLocationListener = new BDLocationListener() {
|
|
|
@Override
|
|
|
public void onReceiveLocation(BDLocation location) {
|
|
|
- try {
|
|
|
- if (location == null)
|
|
|
- locationEntity = new LocationEntity();
|
|
|
- if (location.getLocType() == BDLocation.TypeGpsLocation// GPS定位结果
|
|
|
- || location.getLocType() == BDLocation.TypeNetWorkLocation//网络定位
|
|
|
- || location.getLocType() == BDLocation.TypeOffLineLocation//离线定位(未验证离线定位的有效性)
|
|
|
- ) {
|
|
|
- //定位成功
|
|
|
- log("定位成功");
|
|
|
- log("getLatitude()" + location.getLatitude());
|
|
|
- log("getLongitude" + location.getLongitude());
|
|
|
- log("getAddrStr" + location.getAddrStr());
|
|
|
- log("getLocationDescribe" + location.getLocationDescribe());
|
|
|
- log("getProvince" + location.getProvince());
|
|
|
- log("getCity" + location.getCity());
|
|
|
- log("getDistrict" + location.getDistrict());
|
|
|
- locationEntity.setLocationOk(true);
|
|
|
- locationEntity.setLatitude(location.getLatitude());
|
|
|
- locationEntity.setLongitude(location.getLongitude());
|
|
|
- locationEntity.setAddress(location.getAddrStr());
|
|
|
- locationEntity.setLocation(location.getLocationDescribe());
|
|
|
- locationEntity.setProvince(location.getProvince());
|
|
|
- locationEntity.setCityName(location.getCity());
|
|
|
- locationEntity.setDistrict(location.getDistrict());
|
|
|
- } else {
|
|
|
- //统一未定位失败
|
|
|
- locationEntity.setLocationOk(false);
|
|
|
- log("定位失败");
|
|
|
- if (location.getLocType() == BDLocation.TypeServerError) {
|
|
|
- //服务端网络定位失败,可以反馈IMEI号和大体定位时间到loc-bugs@baidu.com,会有人追查原因
|
|
|
- locationEntity.setErrorMessage("服务端网络定位失败");
|
|
|
- log("服务端网络定位失败");
|
|
|
- } else if (location.getLocType() == BDLocation.TypeNetWorkException) {
|
|
|
- //网络不同导致定位失败,请检查网络是否通畅
|
|
|
- locationEntity.setErrorMessage("网络不同导致定位失败,请检查网络是否通畅");
|
|
|
- log("网络不同导致定位失败,请检查网络是否通畅");
|
|
|
- } else if (location.getLocType() == BDLocation.TypeCriteriaException) {
|
|
|
- //无法获取有效定位依据导致定位失败,一般是由于手机的原因,处于飞行模式下一般会造成这种结果,可以试着重启手机
|
|
|
- locationEntity.setErrorMessage("无法获取有效定位依据导致定位失败");
|
|
|
- log("无法获取有效定位依据导致定位失败");
|
|
|
- } else {
|
|
|
- locationEntity.setErrorMessage("未知错误");
|
|
|
- log("未知错误");
|
|
|
- }
|
|
|
+ int resultCode = 0;
|
|
|
+ if (location != null) {
|
|
|
+ resultCode = location.getLocType();
|
|
|
+ }
|
|
|
+ Log.i("gongpengming", "resultCode=" + resultCode);
|
|
|
+ BdLocationHelper.this.resultCode = resultCode;
|
|
|
+ // 百度定位失败,不符合下列定位返回类型 TODO 应该把缓存定位去除
|
|
|
+ if (resultCode != BDLocation.TypeGpsLocation //GPS定位成功
|
|
|
+// && resultCode != BDLocation.TypeCacheLocation//定位缓存结果
|
|
|
+// && resultCode != BDLocation.TypeOffLineLocation//离线定位结果。通过requestOfflineLocaiton调用时对应的返回结果。
|
|
|
+ && resultCode != BDLocation.TypeNetWorkLocation// 网络定位结果,网络定位成功。
|
|
|
+ ) {
|
|
|
+ if (AppConfig.DEBUG) {
|
|
|
+ Log.d(AppConfig.TAG, "百度定位失败");
|
|
|
+ }
|
|
|
+ mFaildCount++;
|
|
|
+ if (mFaildCount > 3) {// 停止定位
|
|
|
+ mLocationClient.stop();
|
|
|
+ }
|
|
|
+ if (listener != null) {
|
|
|
+ listener.result(false);
|
|
|
}
|
|
|
- //TODO 发送广播
|
|
|
- MyApplication.getInstance().sendBroadcast(broadcast);
|
|
|
- } catch (Exception e) {
|
|
|
+ return;
|
|
|
+ }
|
|
|
+ // 百度定位成功
|
|
|
+ mLongitude = location.getLongitude();
|
|
|
+ mLatitude = location.getLatitude();
|
|
|
+ name = location.getLocationDescribe();
|
|
|
+ pois = location.getPoiList();
|
|
|
+ if (location.getLocType() == BDLocation.TypeNetWorkLocation) {
|
|
|
+ mAddress = location.getAddrStr();
|
|
|
+ mProvinceName = location.getProvince();
|
|
|
+ mCityName = location.getCity();
|
|
|
+ mDistrictName = location.getDistrict();
|
|
|
+ if (AppConfig.DEBUG) {
|
|
|
+ Log.d(AppConfig.TAG,
|
|
|
+ "百度定位信息 City:" + location.getCity() + " CityCode:" + location.getCityCode() + " 区:" + location.getDistrict());
|
|
|
+ }
|
|
|
+ }
|
|
|
+ if (!isLocationUpdate) {
|
|
|
+ LocationSp.getInstance(mContext).setLongitude((float) mLongitude);
|
|
|
+ LocationSp.getInstance(mContext).setLatitude((float) mLatitude);
|
|
|
+ LocationSp.getInstance(mContext).setAddress(mAddress);
|
|
|
+ LocationSp.getInstance(mContext).setProvinceName(mProvinceName);
|
|
|
+ LocationSp.getInstance(mContext).setCityName(mCityName);
|
|
|
+ LocationSp.getInstance(mContext).setDistrictName(mDistrictName);
|
|
|
+ isLocationUpdate = true;
|
|
|
+ }
|
|
|
+ if (AppConfig.DEBUG) {
|
|
|
+ Log.d(AppConfig.TAG, "百度定位信息 mLongitude:" + mLongitude + " mLatitude:" + mLatitude + " mAddressDetail:" + mAddress);
|
|
|
+ Log.d(AppConfig.TAG, "resultCode:" + resultCode);
|
|
|
+ }
|
|
|
+// release();
|
|
|
+ // if (isTimingScan()) {// 停止定时定位
|
|
|
+ // mLocationClient.getLocOption().setScanSpan(100);
|
|
|
+ // }
|
|
|
+ if (listener != null) {
|
|
|
+ listener.result(true);
|
|
|
}
|
|
|
}
|
|
|
};
|
|
|
|
|
|
- private void log(String message) {
|
|
|
- try {
|
|
|
- if (!AppConfig.DEBUG && StringUtils.isEmpty(message)) return;
|
|
|
- Log.i("gongpengming", message);
|
|
|
- } catch (Exception e) {
|
|
|
|
|
|
- }
|
|
|
+ //是否定位成功
|
|
|
+ public boolean locationOk() {
|
|
|
+ return resultCode == BDLocation.TypeGpsLocation || resultCode == BDLocation.TypeNetWorkLocation;
|
|
|
}
|
|
|
|
|
|
-
|
|
|
public String getName() {
|
|
|
- return locationEntity == null ? "" : StringUtils.isEmpty(locationEntity.getLocation()) ? "" : locationEntity.getLocation();
|
|
|
+ return pois == null ? (StringUtils.isEmail(name) ? "当前位置" : name) : (pois.get(0) == null ? (name == null ? "当前位置" : name) : pois.get(0).getName());
|
|
|
}
|
|
|
|
|
|
+ public void setName(String name) {
|
|
|
+ this.name = name;
|
|
|
+ }
|
|
|
|
|
|
// 获取经纬度
|
|
|
public double getLongitude() {
|
|
|
- return locationEntity == null ? 0 : locationEntity.getLongitude();
|
|
|
+ return mLongitude;
|
|
|
}
|
|
|
|
|
|
// 获取经纬度
|
|
|
public double getLatitude() {
|
|
|
- return locationEntity == null ? 0 : locationEntity.getLatitude();
|
|
|
+ return mLatitude;
|
|
|
}
|
|
|
|
|
|
// 获取地址详情
|
|
|
public String getAddress() {
|
|
|
- return locationEntity == null ? "" : locationEntity.getAddress();
|
|
|
+ return mAddress;
|
|
|
}
|
|
|
|
|
|
public String getProvinceName() {
|
|
|
- return locationEntity == null ? "" : locationEntity.getProvince();
|
|
|
+ return mProvinceName;
|
|
|
}
|
|
|
|
|
|
public String getCityName() {
|
|
|
- return locationEntity == null ? "" : locationEntity.getCityName();
|
|
|
+ return mCityName;
|
|
|
}
|
|
|
|
|
|
public String getDistrictName() {
|
|
|
- return locationEntity == null ? "" : locationEntity.getDistrict();
|
|
|
+ return mDistrictName;
|
|
|
}
|
|
|
|
|
|
public boolean isLocationUpdate() {
|
|
|
- return true;
|
|
|
+ return isLocationUpdate;
|
|
|
}
|
|
|
|
|
|
+ public boolean hasData() {
|
|
|
+ return mLatitude != 0 && mLongitude != 0;
|
|
|
+ }
|
|
|
|
|
|
private OnBaiduResultListener listener = null;
|
|
|
|