|
|
@@ -0,0 +1,229 @@
|
|
|
+package com.xzjmyk.pm.activity;
|
|
|
+
|
|
|
+import android.content.Intent;
|
|
|
+import android.support.v4.content.LocalBroadcastManager;
|
|
|
+
|
|
|
+import com.baidu.location.BDLocation;
|
|
|
+import com.baidu.location.BDLocationListener;
|
|
|
+import com.baidu.location.LocationClient;
|
|
|
+import com.baidu.location.LocationClientOption;
|
|
|
+import com.baidu.mapapi.model.LatLng;
|
|
|
+import com.common.LogUtil;
|
|
|
+import com.common.data.StringUtil;
|
|
|
+import com.common.preferences.PreferenceUtils;
|
|
|
+
|
|
|
+public class BdLocationHelper {
|
|
|
+ public static final String UPLOCATION_ACTION = "UPLOCATION_ACTION";//更新位置时候广播数据
|
|
|
+
|
|
|
+ private boolean locationOk;//是否定位成功
|
|
|
+ private LatLng latLng;
|
|
|
+ private String location;//位置信息
|
|
|
+ private String address;//详细地址
|
|
|
+ private String province;// 省份
|
|
|
+ private String cityName;// 城市
|
|
|
+ private String district;// 街道
|
|
|
+ private String errorMessage;//定位错误信息
|
|
|
+
|
|
|
+ private final Intent broadcast = new Intent(UPLOCATION_ACTION);//更新后广播
|
|
|
+ private LocationClient mLocationClient = null;
|
|
|
+ private int mFaildCount = 0;//失败次数
|
|
|
+
|
|
|
+ public BdLocationHelper() {
|
|
|
+ initLocation();
|
|
|
+ }
|
|
|
+
|
|
|
+ private void initLocation() {
|
|
|
+ mLocationClient = new LocationClient(MyApplication.getInstance().getApplicationContext()); // 声明LocationClient类
|
|
|
+ mLocationClient.registerLocationListener(mLocationListener); // 注册监听函数
|
|
|
+ mLocationClient.setLocOption(getOptionByGPS());
|
|
|
+// requestLocation();
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 获取配置
|
|
|
+ *
|
|
|
+ * @return
|
|
|
+ */
|
|
|
+ private LocationClientOption getOptionByGPS() {
|
|
|
+ LocationClientOption option = new LocationClientOption();
|
|
|
+ option.setLocationMode(LocationClientOption.LocationMode.Hight_Accuracy);//可选,默认高精度,设置定位模式,高精度,低功耗,仅设备
|
|
|
+ option.setCoorType("bd09ll");//可选,默认gcj02,设置返回的定位结果坐标系
|
|
|
+ option.setScanSpan(20 * 1000);//可选,默认0,即仅定位一次,设置发起定位请求的间隔需要大于等于1000ms才是有效的
|
|
|
+ option.setIsNeedAddress(true);//可选,设置是否需要地址信息,默认不需要
|
|
|
+ option.setOpenGps(true);//可选,默认false,设置是否使用gps
|
|
|
+ option.setLocationNotify(false);//可选,默认false,设置是否当GPS有效时按照1S/1次频率输出GPS结果
|
|
|
+ option.setIsNeedLocationDescribe(true);//可选,默认false,设置是否需要位置语义化结果,可以在BDLocation.getLocationDescribe里得到,结果类似于“在北京天安门附近”
|
|
|
+ option.setIsNeedLocationPoiList(false);//可选,默认false,设置是否需要POI结果,可以在BDLocation.getPoiList里得到
|
|
|
+ option.setIgnoreKillProcess(false);//可选,默认true,定位SDK内部是一个SERVICE,并放到了独立进程,设置是否在stop的时候杀死这个进程,默认不杀死
|
|
|
+ option.SetIgnoreCacheException(false);//可选,默认false,设置是否收集CRASH信息,默认收集
|
|
|
+ option.setEnableSimulateGps(true);//可选,默认false,设置是否需要过滤GPS仿真结果,默认需要
|
|
|
+ option.setNeedDeviceDirect(false);
|
|
|
+ return option;
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 获取配置
|
|
|
+ *
|
|
|
+ * @return
|
|
|
+ */
|
|
|
+ private LocationClientOption getOptionNotGPS() {
|
|
|
+ LocationClientOption option = getOptionByGPS();
|
|
|
+ option.setOpenGps(false);
|
|
|
+ return option;
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 关闭定位
|
|
|
+ **/
|
|
|
+ public void release() {
|
|
|
+ if (mLocationClient.isStarted()) {
|
|
|
+ mLocationClient.stop();
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 重新定位
|
|
|
+ **/
|
|
|
+ public void requestLocation() {
|
|
|
+ if (mLocationClient == null) return;
|
|
|
+ if (!mLocationClient.isStarted()) {
|
|
|
+ mFaildCount = 0;
|
|
|
+ mLocationClient.start();
|
|
|
+ } else {
|
|
|
+ mLocationClient.requestLocation();
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+ private BDLocationListener mLocationListener = new BDLocationListener() {
|
|
|
+ @Override
|
|
|
+ public void onReceiveLocation(BDLocation location) {
|
|
|
+ try {
|
|
|
+ setLocation(location);
|
|
|
+ } catch (Exception e) {
|
|
|
+ if (e != null)
|
|
|
+ log("onReceiveLocation Exception" + e.getMessage());
|
|
|
+ clearLocation();
|
|
|
+ }
|
|
|
+ }
|
|
|
+ };
|
|
|
+
|
|
|
+
|
|
|
+ private void setLocation(BDLocation location) throws Exception {
|
|
|
+ if (location.getLocType() == BDLocation.TypeGpsLocation// GPS定位结果
|
|
|
+ || location.getLocType() == BDLocation.TypeNetWorkLocation//网络定位
|
|
|
+ || location.getLocType() == BDLocation.TypeOffLineLocation//离线定位(未验证离线定位的有效性)
|
|
|
+ ) {
|
|
|
+ //定位成功
|
|
|
+ locationOk = true;
|
|
|
+ latLng = new LatLng(location.getLatitude(), location.getLongitude());
|
|
|
+ this.location = location.getLocationDescribe();
|
|
|
+ province = location.getProvince();
|
|
|
+ cityName = location.getCity();
|
|
|
+ district = location.getDistrict();
|
|
|
+ address = location.getAddrStr();
|
|
|
+ if (!StringUtil.isEmpty(this.location))
|
|
|
+ PreferenceUtils.putString("bdlocation", this.location);
|
|
|
+ if (!StringUtil.isEmpty(address))
|
|
|
+ PreferenceUtils.putString("bdaddress", address);
|
|
|
+ errorMessage = "";
|
|
|
+ } else {
|
|
|
+ //统一为定位失败
|
|
|
+ locationOk = false;
|
|
|
+ log("定位失败");
|
|
|
+ if (location.getLocType() == BDLocation.TypeServerError) {
|
|
|
+ //服务端网络定位失败,可以反馈IMEI号和大体定位时间到loc-bugs@baidu.com,会有人追查原因
|
|
|
+ errorMessage = "服务端网络定位失败";
|
|
|
+ log("服务端网络定位失败");
|
|
|
+ } else if (location.getLocType() == BDLocation.TypeNetWorkException) {
|
|
|
+ //网络不同导致定位失败,请检查网络是否通畅
|
|
|
+ errorMessage = "网络不同导致定位失败,请检查网络是否通畅";
|
|
|
+ log("网络不同导致定位失败,请检查网络是否通畅");
|
|
|
+ } else if (location.getLocType() == BDLocation.TypeCriteriaException) {
|
|
|
+ //无法获取有效定位依据导致定位失败,一般是由于手机的原因,处于飞行模式下一般会造成这种结果,可以试着重启手机
|
|
|
+ errorMessage = "无法获取有效定位依据导致定位失败";
|
|
|
+ log("无法获取有效定位依据导致定位失败");
|
|
|
+ } else {
|
|
|
+ errorMessage = "未知错误";
|
|
|
+ log("未知错误");
|
|
|
+ }
|
|
|
+ if (mFaildCount < 3) {
|
|
|
+ mFaildCount++;
|
|
|
+ requestLocation();
|
|
|
+ return;
|
|
|
+ }
|
|
|
+ }
|
|
|
+ //发送广播
|
|
|
+ broadcast.putExtra(UPLOCATION_ACTION, locationOk);
|
|
|
+ LocalBroadcastManager.getInstance(MyApplication.getInstance()).sendBroadcast(broadcast);
|
|
|
+ }
|
|
|
+
|
|
|
+ private void log(String message) {
|
|
|
+ LogUtil.i(message);
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+ public String getName() {
|
|
|
+ return StringUtil.isEmpty(location) ? "" : location;
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+ // 获取经纬度
|
|
|
+ public double getLongitude() {
|
|
|
+ return latLng == null ? 1 : latLng.longitude;
|
|
|
+ }
|
|
|
+
|
|
|
+ // 获取经纬度
|
|
|
+ public double getLatitude() {
|
|
|
+ return latLng == null ? 1 : latLng.latitude;
|
|
|
+ }
|
|
|
+
|
|
|
+ // 获取地址详情
|
|
|
+ public String getAddress() {
|
|
|
+ return StringUtil.isEmpty(address) ? PreferenceUtils.getString("bdaddress") : address;
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+ public String getProvinceName() {
|
|
|
+ return province;
|
|
|
+ }
|
|
|
+
|
|
|
+ public LatLng getLocation() {
|
|
|
+ return latLng;
|
|
|
+ }
|
|
|
+
|
|
|
+ public String getCityName() {
|
|
|
+ return cityName;
|
|
|
+ }
|
|
|
+
|
|
|
+ public String getDistrictName() {
|
|
|
+ return district;
|
|
|
+ }
|
|
|
+
|
|
|
+ public boolean locationOk() {
|
|
|
+ return locationOk;
|
|
|
+ }
|
|
|
+
|
|
|
+ public String getErrorMessage() {
|
|
|
+ return errorMessage;
|
|
|
+ }
|
|
|
+
|
|
|
+ public boolean isLocationUpdate() {
|
|
|
+ return true;
|
|
|
+ }
|
|
|
+
|
|
|
+ private void clearLocation() {
|
|
|
+ locationOk = false;
|
|
|
+ latLng = null;
|
|
|
+ location = null;
|
|
|
+ address = null;
|
|
|
+ province = null;
|
|
|
+ cityName = null;
|
|
|
+ district = null;
|
|
|
+ errorMessage = null;
|
|
|
+ }
|
|
|
+
|
|
|
+}
|
|
|
+
|