| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231 |
- package com.xzjmyk.pm.activity;
- import android.content.Intent;
- import android.os.Bundle;
- 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.xzjmyk.pm.activity.ui.erp.util.StringUtils;
- import com.xzjmyk.pm.activity.ui.tool.ThreadUtil;
- 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 boolean runingLocation = false;
- private long oldTime;//上一次获取定位时间
- public BdLocationHelper() {
- initLocation();
- }
- private void initLocation() {
- mLocationClient = new LocationClient(MyApplication.getInstance().getApplicationContext()); // 声明LocationClient类
- mLocationClient.registerLocationListener(mLocationListener); // 注册监听函数
- mLocationClient.setLocOption(getOptionByGPS());
- }
- /**
- * 获取配置
- *
- * @return
- */
- private LocationClientOption getOptionByGPS() {
- LocationClientOption option = new LocationClientOption();
- option.setLocationMode(LocationClientOption.LocationMode.Hight_Accuracy);//可选,默认高精度,设置定位模式,高精度,低功耗,仅设备
- option.setCoorType("bd09ll");//可选,默认gcj02,设置返回的定位结果坐标系
- option.setScanSpan(0);//可选,默认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();
- }
- }
- /**
- * 重新定位,循环
- **/
- 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
- public void onReceiveLocation(BDLocation location) {
- try {
- if (System.currentTimeMillis() - oldTime < 50000) 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();
- log("定位成功");
- 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("未知错误");
- }
- if (mFaildCount < 3) {
- mFaildCount++;
- requestLocation();
- }
- }
- //TODO 发送广播
- Bundle bundle = broadcast.getExtras();
- if (bundle == null) bundle = new Bundle();
- bundle.putParcelable("data", locationEntity);
- broadcast.putExtras(bundle);
- MyApplication.getInstance().sendBroadcast(broadcast);
- } catch (Exception e) {
- if (e != null)
- log("onReceiveLocation Exception" + e.getMessage());
- if (location == null)
- locationEntity = new LocationEntity();
- locationEntity.clear();
- }
- }
- };
- private void log(String message) {
- try {
- if (!AppConfig.DEBUG && StringUtils.isEmpty(message)) return;
- Log.i("gongpengming", message);
- } catch (Exception e) {
- }
- }
- public String getName() {
- return locationEntity == null ? "" : StringUtils.isEmpty(locationEntity.getLocation()) ? "" : locationEntity.getLocation();
- }
- // 获取经纬度
- public double getLongitude() {
- return locationEntity == null ? 0 : locationEntity.getLongitude();
- }
- // 获取经纬度
- public double getLatitude() {
- return locationEntity == null ? 0 : locationEntity.getLatitude();
- }
- // 获取地址详情
- public String getAddress() {
- return locationEntity == null ? "" : locationEntity.getAddress();
- }
- public String getProvinceName() {
- return locationEntity == null ? "" : locationEntity.getProvince();
- }
- public String getCityName() {
- return locationEntity == null ? "" : locationEntity.getCityName();
- }
- public String getDistrictName() {
- return locationEntity == null ? "" : locationEntity.getDistrict();
- }
- public boolean locationOk() {
- return locationEntity == null ? false : locationEntity.isLocationOk();
- }
- public boolean isLocationUpdate() {
- return true;
- }
- }
|