|
|
@@ -1,7 +1,7 @@
|
|
|
package com.xzjmyk.pm.activity;
|
|
|
|
|
|
import android.content.Intent;
|
|
|
-import android.os.Bundle;
|
|
|
+import android.support.v4.content.LocalBroadcastManager;
|
|
|
import android.util.Log;
|
|
|
|
|
|
import com.baidu.location.BDLocation;
|
|
|
@@ -9,19 +9,24 @@ import com.baidu.location.BDLocationListener;
|
|
|
import com.baidu.location.LocationClient;
|
|
|
import com.baidu.location.LocationClientOption;
|
|
|
import com.baidu.mapapi.model.LatLng;
|
|
|
-import com.xzjmyk.pm.activity.bean.LocationEntity;
|
|
|
import com.xzjmyk.pm.activity.ui.erp.util.StringUtils;
|
|
|
-import com.xzjmyk.pm.activity.ui.tool.ThreadUtil;
|
|
|
import com.xzjmyk.pm.activity.util.PreferenceUtils;
|
|
|
|
|
|
public class BdLocationHelper {
|
|
|
public static final String UPLOCATION_ACTION = "UPLOCATION_ACTION";//更新位置时候广播数据
|
|
|
- private LocationEntity locationEntity;//位置对象
|
|
|
+
|
|
|
+ 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;//失败次数
|
|
|
- private boolean runingLocation = false;
|
|
|
- private long oldTime;//上一次获取定位时间
|
|
|
|
|
|
public BdLocationHelper() {
|
|
|
initLocation();
|
|
|
@@ -42,7 +47,7 @@ public class BdLocationHelper {
|
|
|
LocationClientOption option = new LocationClientOption();
|
|
|
option.setLocationMode(LocationClientOption.LocationMode.Hight_Accuracy);//可选,默认高精度,设置定位模式,高精度,低功耗,仅设备
|
|
|
option.setCoorType("bd09ll");//可选,默认gcj02,设置返回的定位结果坐标系
|
|
|
- option.setScanSpan(0);//可选,默认0,即仅定位一次,设置发起定位请求的间隔需要大于等于1000ms才是有效的
|
|
|
+ option.setScanSpan(20 * 1000);//可选,默认0,即仅定位一次,设置发起定位请求的间隔需要大于等于1000ms才是有效的
|
|
|
option.setIsNeedAddress(true);//可选,设置是否需要地址信息,默认不需要
|
|
|
option.setOpenGps(true);//可选,默认false,设置是否使用gps
|
|
|
option.setLocationNotify(false);//可选,默认false,设置是否当GPS有效时按照1S/1次频率输出GPS结果
|
|
|
@@ -90,29 +95,6 @@ public class BdLocationHelper {
|
|
|
}
|
|
|
}
|
|
|
|
|
|
- /**
|
|
|
- * 重新定位,循环
|
|
|
- **/
|
|
|
- public void requestLocation(final long time) {
|
|
|
- if (runingLocation || mLocationClient == null) {//如果已经启动了
|
|
|
- return;
|
|
|
- }
|
|
|
- runingLocation = true;
|
|
|
- ThreadUtil.getInstance().addLoopTask(new Runnable() {
|
|
|
- @Override
|
|
|
- public void run() {
|
|
|
- while (runingLocation) {
|
|
|
- log("requestLocation");
|
|
|
- mLocationClient.requestNotifyLocation();
|
|
|
- try {
|
|
|
- Thread.sleep(time);
|
|
|
- } catch (InterruptedException e) {
|
|
|
- e.printStackTrace();
|
|
|
- }
|
|
|
- }
|
|
|
- }
|
|
|
- });
|
|
|
- }
|
|
|
|
|
|
private BDLocationListener mLocationListener = new BDLocationListener() {
|
|
|
@Override
|
|
|
@@ -122,54 +104,48 @@ public class BdLocationHelper {
|
|
|
} catch (Exception e) {
|
|
|
if (e != null)
|
|
|
log("onReceiveLocation Exception" + e.getMessage());
|
|
|
- if (location == null)
|
|
|
- locationEntity = new LocationEntity();
|
|
|
- locationEntity.clear();
|
|
|
+ clearLocation();
|
|
|
}
|
|
|
}
|
|
|
};
|
|
|
|
|
|
+
|
|
|
private void setLocation(BDLocation location) throws Exception {
|
|
|
- if (System.currentTimeMillis() - oldTime < 10000) return;//防止过度
|
|
|
- if (locationEntity == null)
|
|
|
- locationEntity = new LocationEntity();
|
|
|
- locationEntity.clear();
|
|
|
if (location.getLocType() == BDLocation.TypeGpsLocation// GPS定位结果
|
|
|
|| location.getLocType() == BDLocation.TypeNetWorkLocation//网络定位
|
|
|
|| location.getLocType() == BDLocation.TypeOffLineLocation//离线定位(未验证离线定位的有效性)
|
|
|
) {
|
|
|
//定位成功
|
|
|
- oldTime = System.currentTimeMillis();
|
|
|
- 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());
|
|
|
- if (StringUtils.isEmpty(location.getLocationDescribe()))
|
|
|
- PreferenceUtils.putString("bdlocation", location.getLocationDescribe());
|
|
|
- if (StringUtils.isEmpty(location.getAddrStr()))
|
|
|
- PreferenceUtils.putString("bdaddress", location.getAddrStr());
|
|
|
+ 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 (!StringUtils.isEmpty(this.location))
|
|
|
+ PreferenceUtils.putString("bdlocation", this.location);
|
|
|
+ if (!StringUtils.isEmpty(address))
|
|
|
+ PreferenceUtils.putString("bdaddress", address);
|
|
|
+ errorMessage = "";
|
|
|
} else {
|
|
|
//统一为定位失败
|
|
|
- locationEntity.setLocationOk(false);
|
|
|
+ locationOk = false;
|
|
|
log("定位失败");
|
|
|
if (location.getLocType() == BDLocation.TypeServerError) {
|
|
|
//服务端网络定位失败,可以反馈IMEI号和大体定位时间到loc-bugs@baidu.com,会有人追查原因
|
|
|
- locationEntity.setErrorMessage("服务端网络定位失败");
|
|
|
+ errorMessage = "服务端网络定位失败";
|
|
|
log("服务端网络定位失败");
|
|
|
} else if (location.getLocType() == BDLocation.TypeNetWorkException) {
|
|
|
//网络不同导致定位失败,请检查网络是否通畅
|
|
|
- locationEntity.setErrorMessage("网络不同导致定位失败,请检查网络是否通畅");
|
|
|
+ errorMessage = "网络不同导致定位失败,请检查网络是否通畅";
|
|
|
log("网络不同导致定位失败,请检查网络是否通畅");
|
|
|
} else if (location.getLocType() == BDLocation.TypeCriteriaException) {
|
|
|
//无法获取有效定位依据导致定位失败,一般是由于手机的原因,处于飞行模式下一般会造成这种结果,可以试着重启手机
|
|
|
- locationEntity.setErrorMessage("无法获取有效定位依据导致定位失败");
|
|
|
+ errorMessage = "无法获取有效定位依据导致定位失败";
|
|
|
log("无法获取有效定位依据导致定位失败");
|
|
|
} else {
|
|
|
- locationEntity.setErrorMessage("未知错误");
|
|
|
+ errorMessage = "未知错误";
|
|
|
log("未知错误");
|
|
|
}
|
|
|
if (mFaildCount < 3) {
|
|
|
@@ -178,13 +154,9 @@ public class BdLocationHelper {
|
|
|
return;
|
|
|
}
|
|
|
}
|
|
|
- //TODO 发送广播
|
|
|
- Bundle bundle = broadcast.getExtras();
|
|
|
- if (bundle == null) bundle = new Bundle();
|
|
|
- bundle.putParcelable("data", locationEntity);
|
|
|
- broadcast.putExtras(bundle);
|
|
|
- MyApplication.getInstance().sendBroadcast(broadcast);
|
|
|
-
|
|
|
+ //发送广播
|
|
|
+ broadcast.putExtra(UPLOCATION_ACTION, locationOk);
|
|
|
+ LocalBroadcastManager.getInstance(MyApplication.getInstance()).sendBroadcast(broadcast);
|
|
|
}
|
|
|
|
|
|
private void log(String message) {
|
|
|
@@ -198,52 +170,64 @@ public class BdLocationHelper {
|
|
|
|
|
|
|
|
|
public String getName() {
|
|
|
- return locationEntity == null ? "" : StringUtils.isEmpty(locationEntity.getLocation()) ? "" : locationEntity.getLocation();
|
|
|
+ return StringUtils.isEmpty(location) ? "" : location;
|
|
|
}
|
|
|
|
|
|
|
|
|
// 获取经纬度
|
|
|
public double getLongitude() {
|
|
|
- return locationEntity == null ? 0 : locationEntity.getLongitude();
|
|
|
+ return latLng == null ? 1 : latLng.longitude;
|
|
|
}
|
|
|
|
|
|
// 获取经纬度
|
|
|
public double getLatitude() {
|
|
|
- return locationEntity == null ? 0 : locationEntity.getLatitude();
|
|
|
+ return latLng == null ? 1 : latLng.latitude;
|
|
|
}
|
|
|
|
|
|
// 获取地址详情
|
|
|
public String getAddress() {
|
|
|
- String addr = locationEntity == null ? "" : locationEntity.getAddress();
|
|
|
- if (StringUtils.isEmpty(addr)) ;
|
|
|
- addr = PreferenceUtils.getString("bdaddress");
|
|
|
- return addr;
|
|
|
+ return StringUtils.isEmpty(address) ? PreferenceUtils.getString("bdaddress") : address;
|
|
|
}
|
|
|
|
|
|
+
|
|
|
public String getProvinceName() {
|
|
|
- return locationEntity == null ? "" : locationEntity.getProvince();
|
|
|
+ return province;
|
|
|
}
|
|
|
|
|
|
public LatLng getLocation() {
|
|
|
- return new LatLng(getLatitude(), getLongitude());
|
|
|
+ return latLng;
|
|
|
}
|
|
|
|
|
|
public String getCityName() {
|
|
|
- return locationEntity == null ? "" : locationEntity.getCityName();
|
|
|
+ return cityName;
|
|
|
}
|
|
|
|
|
|
public String getDistrictName() {
|
|
|
- return locationEntity == null ? "" : locationEntity.getDistrict();
|
|
|
+ return district;
|
|
|
}
|
|
|
|
|
|
public boolean locationOk() {
|
|
|
- return locationEntity == null ? false : locationEntity.isLocationOk();
|
|
|
+ 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;
|
|
|
+ }
|
|
|
|
|
|
}
|
|
|
|