fix: 修改定位方式
This commit is contained in:
parent
f04b2a5892
commit
390d3d435c
@ -12,7 +12,7 @@ android {
|
||||
applicationId "com.navinfo.outdoor"
|
||||
minSdkVersion 24
|
||||
targetSdkVersion 30
|
||||
versionCode 36
|
||||
versionCode 38
|
||||
versionName "8.221207"
|
||||
testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
|
||||
}
|
||||
|
@ -196,8 +196,6 @@ public class AutoTakePictureActivity extends BaseActivity implements View.OnClic
|
||||
private TextureMapView tvMapView;
|
||||
private SystemTTS systemTTS;
|
||||
private List<Removable> trackLocRemovables/*用户轨迹图标marker*/;
|
||||
private GPSUtils gpsUtils;
|
||||
private Location gpsLocation;
|
||||
private boolean isMapSlide = false; // 地图是否为靠在侧边的小图模式
|
||||
private ImageView ivZoomAdd/*zoom放大*/, ivZoomDel/*zoom缩小*/, ivLocation/*定位按钮*/,
|
||||
ivPicRoadImage/*道路拍摄水平线*/, ivPicVideoImage/*视频拍摄水平线*/;
|
||||
@ -308,13 +306,6 @@ public class AutoTakePictureActivity extends BaseActivity implements View.OnClic
|
||||
ivPicVideoImage.setVisibility(View.GONE);
|
||||
|
||||
trackLocRemovables = new ArrayList<>();
|
||||
gpsUtils = new GPSUtils(AutoTakePictureActivity.this);
|
||||
gpsUtils.setOnClickGPSStatus(new GPSUtils.OnClickGPSStatus() {
|
||||
@Override
|
||||
public void onGpsCount(int count) {
|
||||
satelliteCount = count;
|
||||
}
|
||||
});
|
||||
|
||||
// 清空所有匹配的数据
|
||||
btnClearMatch.setOnClickListener(new View.OnClickListener() {
|
||||
@ -585,7 +576,7 @@ public class AutoTakePictureActivity extends BaseActivity implements View.OnClic
|
||||
recorder.setTime(Long.parseLong(file.getName()));
|
||||
recorder.setTencentLocationX(Constant.currentLocation.getLongitude());
|
||||
recorder.setTencentLocationY(Constant.currentLocation.getLatitude());
|
||||
gpsLocation = gpsUtils.getLocation();
|
||||
gpsLocation = GPSUtils.getInstance(AutoTakePictureActivity.this).getLocation();
|
||||
if (gpsLocation!=null) {
|
||||
recorder.setGpsLocationX(gpsLocation.getLongitude());
|
||||
recorder.setGpsLocationY(gpsLocation.getLatitude());
|
||||
@ -597,7 +588,7 @@ public class AutoTakePictureActivity extends BaseActivity implements View.OnClic
|
||||
}
|
||||
recorder.setImgFileName(file.getAbsolutePath());
|
||||
recorder.setRssi(Constant.currentLocation.getGPSRssi());
|
||||
recorder.setSatelliteCount(satelliteCount);
|
||||
recorder.setSatelliteCount(GPSUtils.getInstance(AutoTakePictureActivity.this).getSateliteCount());
|
||||
Observable.just(recorder).observeOn(Schedulers.io())
|
||||
.filter(roadMatchEntity -> roadMatchEntity!=null)
|
||||
.subscribe(new Consumer<LocationRecorder>() {
|
||||
@ -1766,7 +1757,7 @@ public class AutoTakePictureActivity extends BaseActivity implements View.OnClic
|
||||
*/
|
||||
public void addTrackMarker() {
|
||||
LatLng latLng = null;
|
||||
if (Constant.currentLocation.getGPSRssi() == 0) {
|
||||
if (Constant.currentLocation.getGPSRssi() != 0) {
|
||||
latLng = new LatLng(Constant.currentLocation.getLatitude(), Constant.currentLocation.getLongitude());
|
||||
} else {
|
||||
if (gpsLocation == null) {
|
||||
|
@ -104,8 +104,6 @@ public class HomeActivity extends BaseActivity {
|
||||
return false;
|
||||
}
|
||||
});
|
||||
private LocationLifeCycle locationLifeCycle;
|
||||
|
||||
@Override
|
||||
protected int getLayout() {
|
||||
return R.layout.activity_home;
|
||||
@ -116,9 +114,9 @@ public class HomeActivity extends BaseActivity {
|
||||
super.onCreate(savedInstanceState);
|
||||
DialogSettings.style = DialogSettings.STYLE.STYLE_IOS;
|
||||
DialogSettings.init();
|
||||
locationLifeCycle = new LocationLifeCycle(this);
|
||||
LocationLifeCycle.getInstance().init(HomeActivity.this);
|
||||
// 注册位置更新的lifeCycle
|
||||
getLifecycle().addObserver(locationLifeCycle);
|
||||
getLifecycle().addObserver(LocationLifeCycle.getInstance());
|
||||
if (Constant.USHERED != null) {
|
||||
Constant.initRootFolder(Constant.USHERED);
|
||||
// 检测用户是否为虚拟定位
|
||||
@ -565,6 +563,6 @@ public class HomeActivity extends BaseActivity {
|
||||
// 清空登录信息
|
||||
Constant.clearLoginInfo();
|
||||
// 注册位置更新的lifeCycle
|
||||
getLifecycle().removeObserver(locationLifeCycle);
|
||||
getLifecycle().removeObserver(LocationLifeCycle.getInstance());
|
||||
}
|
||||
}
|
@ -680,21 +680,21 @@ public class PictureActivity extends BaseActivity implements View.OnClickListene
|
||||
}
|
||||
}
|
||||
|
||||
@Subscribe(threadMode = ThreadMode.MAIN)
|
||||
public void onEventMessageMainThread(Message msg) {
|
||||
if (msg.what == Constant.EVENT_WHAT_LOCATION_CHANGE) { // 用户位置更新
|
||||
if (tencentMap != null && !isMapSlide) {
|
||||
TencentLocation tencentLocation = (TencentLocation) msg.obj;
|
||||
CameraUpdate cameraSigma =
|
||||
CameraUpdateFactory.newCameraPosition(new CameraPosition(
|
||||
new LatLng(Constant.currentLocation.getLatitude(), Constant.currentLocation.getLongitude()), //中心点坐标,地图目标经纬度
|
||||
tencentMap.getCameraPosition().zoom, //目标缩放级别
|
||||
0, //目标倾斜角
|
||||
0)); //目标旋转角 0~360° (正北方为0)
|
||||
tencentMap.animateCamera(cameraSigma);
|
||||
}
|
||||
}
|
||||
}
|
||||
// @Subscribe(threadMode = ThreadMode.MAIN)
|
||||
// public void onEventMessageMainThread(Message msg) {
|
||||
// if (msg.what == Constant.EVENT_WHAT_LOCATION_CHANGE) { // 用户位置更新
|
||||
// if (tencentMap != null && !isMapSlide) {
|
||||
// TencentLocation tencentLocation = (TencentLocation) msg.obj;
|
||||
// CameraUpdate cameraSigma =
|
||||
// CameraUpdateFactory.newCameraPosition(new CameraPosition(
|
||||
// new LatLng(Constant.currentLocation.getLatitude(), Constant.currentLocation.getLongitude()), //中心点坐标,地图目标经纬度
|
||||
// tencentMap.getCameraPosition().zoom, //目标缩放级别
|
||||
// 0, //目标倾斜角
|
||||
// 0)); //目标旋转角 0~360° (正北方为0)
|
||||
// tencentMap.animateCamera(cameraSigma);
|
||||
// }
|
||||
// }
|
||||
// }
|
||||
|
||||
/**
|
||||
* 记录视频拍摄的时间及经纬度
|
||||
|
@ -10,6 +10,7 @@ import android.graphics.BitmapFactory;
|
||||
import android.graphics.Color;
|
||||
import android.graphics.Matrix;
|
||||
import android.location.Location;
|
||||
import android.os.Build;
|
||||
import android.os.Bundle;
|
||||
import android.os.Handler;
|
||||
import android.os.Message;
|
||||
@ -31,6 +32,7 @@ import android.widget.Toast;
|
||||
|
||||
import androidx.annotation.NonNull;
|
||||
import androidx.annotation.Nullable;
|
||||
import androidx.annotation.RequiresApi;
|
||||
|
||||
import com.github.lazylibrary.util.FileUtils;
|
||||
import com.kongzue.dialog.interfaces.OnDialogButtonClickListener;
|
||||
@ -79,6 +81,7 @@ import com.tencent.tencentmap.mapsdk.maps.model.MarkerOptions;
|
||||
import com.tencent.tencentmap.mapsdk.maps.model.MyLocationStyle;
|
||||
import com.tencent.tencentmap.mapsdk.maps.model.Polyline;
|
||||
import com.tencent.tencentmap.mapsdk.maps.model.PolylineOptions;
|
||||
import com.tencent.tencentmap.mapsdk.maps.model.TencentMapGestureListener;
|
||||
import com.umeng.commonsdk.internal.crash.UMCrashManager;
|
||||
import com.umeng.umcrash.UMCrash;
|
||||
import org.locationtech.jts.geom.Geometry;
|
||||
@ -146,7 +149,6 @@ public class PicturesActivity extends BaseActivity implements View.OnClickListen
|
||||
private boolean isOration = true; // 当前是否为自动拍照模式
|
||||
private boolean isTerminus = false;
|
||||
private boolean isBack = true;
|
||||
private int initCount;
|
||||
private int type = 0;
|
||||
private int radioPicture = 0;
|
||||
private int videoIndex = -1;
|
||||
@ -229,12 +231,6 @@ public class PicturesActivity extends BaseActivity implements View.OnClickListen
|
||||
ivPicVideoImage = findViewById(R.id.iv_pic_video);
|
||||
ImageView btnSwitch = findViewById(R.id.btn_switch);
|
||||
btnSwitch.setOnClickListener(this);
|
||||
gpsUtils.setOnClickGPSStatus(new GPSUtils.OnClickGPSStatus() {
|
||||
@Override
|
||||
public void onGpsCount(int count) {
|
||||
initCount = count;
|
||||
}
|
||||
});
|
||||
RadioGroup radioGroupPicture = findViewById(R.id.radio_group_picture);
|
||||
RadioButton radioBtnHand = findViewById(R.id.radio_btn_hand);
|
||||
RadioButton radioBtnAuto = findViewById(R.id.radio_btn_auto);
|
||||
@ -342,7 +338,53 @@ public class PicturesActivity extends BaseActivity implements View.OnClickListen
|
||||
uiSettings.setRotateGesturesEnabled(false);//禁止地图旋转手势.
|
||||
uiSettings.setTiltGesturesEnabled(false);//禁止倾斜手势.
|
||||
setLocMarkerStyle(LOCATION_TYPE_LOCATION_ROTATE);
|
||||
tencentMap.setLocationSource(new MyTecentLocationSource(this));
|
||||
tencentMap.setMyLocationEnabled(true);
|
||||
initLine();
|
||||
|
||||
tencentMap.addTencentMapGestureListener(new TencentMapGestureListener() {
|
||||
@Override
|
||||
public boolean onDoubleTap(float v, float v1) {
|
||||
return false;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean onSingleTap(float v, float v1) {
|
||||
return false;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean onFling(float v, float v1) {
|
||||
return false;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean onScroll(float v, float v1) {
|
||||
return false;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean onLongPress(float v, float v1) {
|
||||
return false;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean onDown(float v, float v1) {
|
||||
setLocMarkerStyle(MyLocationStyle.LOCATION_TYPE_LOCATION_ROTATE_NO_CENTER);
|
||||
return false;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean onUp(float v, float v1) {
|
||||
return false;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onMapStable() {
|
||||
|
||||
}
|
||||
});
|
||||
|
||||
camera.addCameraListener(new CameraListener() {
|
||||
@Override
|
||||
public void onPictureTaken(@NonNull PictureResult result) {
|
||||
@ -633,11 +675,13 @@ public class PicturesActivity extends BaseActivity implements View.OnClickListen
|
||||
picturesBuilder.append(TimestampUtil.time()).append(",").append("onClick 点击了放大 ,");
|
||||
CameraUpdate cameraUpdateIn = CameraUpdateFactory.zoomIn();
|
||||
tencentMap.animateCamera(cameraUpdateIn);
|
||||
setLocMarkerStyle(MyLocationStyle.LOCATION_TYPE_LOCATION_ROTATE_NO_CENTER);
|
||||
break;
|
||||
case R.id.iv_zoom_del://缩小
|
||||
picturesBuilder.append(TimestampUtil.time()).append(",").append("onClick 点击了缩小 ,");
|
||||
CameraUpdate cameraUpdateOut = CameraUpdateFactory.zoomOut();
|
||||
tencentMap.animateCamera(cameraUpdateOut);
|
||||
setLocMarkerStyle(MyLocationStyle.LOCATION_TYPE_LOCATION_ROTATE_NO_CENTER);
|
||||
break;
|
||||
case R.id.iv_location://定位:
|
||||
picturesBuilder.append(TimestampUtil.time()).append(",").append("onClick 点击了定位 ,");
|
||||
@ -649,6 +693,7 @@ public class PicturesActivity extends BaseActivity implements View.OnClickListen
|
||||
0)); //目标旋转角 0~360° (正北方为0)
|
||||
tencentMap.animateCamera(cameraSigma);
|
||||
}
|
||||
setLocMarkerStyle(LOCATION_TYPE_LOCATION_ROTATE);
|
||||
break;
|
||||
}
|
||||
}
|
||||
@ -669,8 +714,6 @@ public class PicturesActivity extends BaseActivity implements View.OnClickListen
|
||||
|
||||
/*设置定位图标样式*/
|
||||
private void setLocMarkerStyle(int type) {
|
||||
tencentMap.setLocationSource(new MyTecentLocationSource(this));
|
||||
tencentMap.setMyLocationEnabled(true);
|
||||
MyLocationStyle locationStyle = new MyLocationStyle();
|
||||
//LOCATION_TYPE_LOCATION_ROTATE_NO_CENTER
|
||||
locationStyle = locationStyle.myLocationType(type);
|
||||
@ -681,6 +724,11 @@ public class PicturesActivity extends BaseActivity implements View.OnClickListen
|
||||
locationStyle.fillColor(getResources().getColor(android.R.color.transparent));
|
||||
locationStyle.strokeWidth(1);
|
||||
tencentMap.setMyLocationStyle(locationStyle);
|
||||
if (LOCATION_TYPE_LOCATION_ROTATE == type) {
|
||||
ivLocation.setEnabled(false);
|
||||
} else {
|
||||
ivLocation.setEnabled(true);
|
||||
}
|
||||
}
|
||||
|
||||
public void benSwitch() {
|
||||
@ -740,7 +788,7 @@ public class PicturesActivity extends BaseActivity implements View.OnClickListen
|
||||
ivZoomAdd.setVisibility(View.VISIBLE);
|
||||
ivZoomDel.setVisibility(View.VISIBLE);
|
||||
ivLocation.setVisibility(View.VISIBLE);
|
||||
setLocMarkerStyle(LOCATION_TYPE_LOCATION_ROTATE_NO_CENTER);
|
||||
setLocMarkerStyle(LOCATION_TYPE_LOCATION_ROTATE);
|
||||
// 道路水平方向图标显示
|
||||
ivPicRoadImage.setVisibility(View.GONE);
|
||||
}
|
||||
@ -852,26 +900,24 @@ public class PicturesActivity extends BaseActivity implements View.OnClickListen
|
||||
if (EventBus.getDefault().isRegistered(this)) {
|
||||
EventBus.getDefault().unregister(this);
|
||||
}
|
||||
if (gpsUtils!=null) {
|
||||
gpsUtils.unRegisterAllListener();
|
||||
}
|
||||
}
|
||||
|
||||
@Subscribe(threadMode = ThreadMode.MAIN)
|
||||
public void onEventMessageMainThread(Message msg) {
|
||||
if (msg.what == Constant.EVENT_WHAT_LOCATION_CHANGE) { // 用户位置更新
|
||||
if (tencentMap != null && !isMapSlide) {
|
||||
TencentLocation tencentLocation = (TencentLocation) msg.obj;
|
||||
CameraUpdate cameraSigma = CameraUpdateFactory.newCameraPosition(new CameraPosition(
|
||||
new LatLng(tencentLocation.getLatitude(), tencentLocation.getLongitude()), //中心点坐标,地图目标经纬度
|
||||
17, //目标缩放级别
|
||||
0, //目标倾斜角
|
||||
tencentLocation.getBearing())); //目标旋转角 0~360° (正北方为0)
|
||||
tencentMap.animateCamera(cameraSigma);
|
||||
}
|
||||
}
|
||||
}
|
||||
// @Subscribe(threadMode = ThreadMode.MAIN)
|
||||
// public void onEventMessageMainThread(Message msg) {
|
||||
// if (msg.what == Constant.EVENT_WHAT_LOCATION_CHANGE) { // 用户位置更新
|
||||
// if (tencentMap != null && !isMapSlide) {
|
||||
// TencentLocation tencentLocation = (TencentLocation) msg.obj;
|
||||
// CameraUpdate cameraSigma = CameraUpdateFactory.newCameraPosition(new CameraPosition(
|
||||
// new LatLng(tencentLocation.getLatitude(), tencentLocation.getLongitude()), //中心点坐标,地图目标经纬度
|
||||
// 17, //目标缩放级别
|
||||
// 0, //目标倾斜角
|
||||
// tencentLocation.getBearing())); //目标旋转角 0~360° (正北方为0)
|
||||
// tencentMap.animateCamera(cameraSigma);
|
||||
// }
|
||||
// }
|
||||
// }
|
||||
|
||||
@RequiresApi(api = Build.VERSION_CODES.N)
|
||||
@SuppressLint("SetTextI18n")
|
||||
public void initMarkerPaper(int index) {
|
||||
int endVideoIndex = index == -1 ? 0 : index;
|
||||
@ -910,9 +956,9 @@ public class PicturesActivity extends BaseActivity implements View.OnClickListen
|
||||
sb.append("强");
|
||||
sb.append(",");
|
||||
}
|
||||
sb.append(initCount);
|
||||
sb.append(GPSUtils.getInstance(PicturesActivity.this).getSateliteCount());
|
||||
sb.append(",");
|
||||
gpsLocation = gpsUtils.getLocation();
|
||||
gpsLocation = GPSUtils.getInstance(PicturesActivity.this).getLocation();
|
||||
if (gpsLocation != null) {
|
||||
// double[] doubles = Gps.toGCJ02Point(gpsLocation.getLatitude(), gpsLocation.getLongitude());
|
||||
sb.append(gpsLocation.getLatitude());
|
||||
|
@ -159,7 +159,6 @@ public class Constant {
|
||||
/*
|
||||
用户当前位置
|
||||
*/
|
||||
public static TencentLocation currentLocation;
|
||||
public static LatLng markerLacing;
|
||||
public static JobSearchBean jobSearchBean;//数
|
||||
public static MapView treasureMap;
|
||||
|
@ -7,14 +7,6 @@ import android.content.Context;
|
||||
import android.content.Intent;
|
||||
import android.os.StrictMode;
|
||||
import android.util.Log;
|
||||
import android.widget.RemoteViews;
|
||||
|
||||
import androidx.core.app.NotificationCompat;
|
||||
import androidx.lifecycle.ProcessLifecycleOwner;
|
||||
|
||||
import com.elvishew.xlog.LogLevel;
|
||||
import com.elvishew.xlog.XLog;
|
||||
import com.google.gson.Gson;
|
||||
import com.lzy.okgo.OkGo;
|
||||
import com.lzy.okgo.cache.CacheEntity;
|
||||
import com.lzy.okgo.cache.CacheMode;
|
||||
|
@ -14,18 +14,26 @@ import android.location.Location;
|
||||
import android.location.LocationListener;
|
||||
import android.location.LocationManager;
|
||||
import android.location.LocationProvider;
|
||||
import android.os.Build;
|
||||
import android.os.Bundle;
|
||||
import android.os.Message;
|
||||
import android.provider.Settings;
|
||||
import android.util.Log;
|
||||
import android.widget.Toast;
|
||||
|
||||
import androidx.annotation.NonNull;
|
||||
import androidx.annotation.RequiresApi;
|
||||
|
||||
import com.navinfo.outdoor.api.Constant;
|
||||
|
||||
import org.greenrobot.eventbus.EventBus;
|
||||
|
||||
import java.util.Iterator;
|
||||
import java.util.List;
|
||||
import java.util.Locale;
|
||||
|
||||
@SuppressLint("MissingPermission")
|
||||
@RequiresApi(api = Build.VERSION_CODES.N)
|
||||
public class GPSUtils {
|
||||
|
||||
private LocationManager mLocationManager;
|
||||
@ -38,6 +46,8 @@ public class GPSUtils {
|
||||
|
||||
private static GPSUtils instance;
|
||||
|
||||
private int sateliteCount; // 卫星颗数
|
||||
|
||||
public static GPSUtils getInstance(Activity mContext) {
|
||||
if (instance == null) {
|
||||
instance = new GPSUtils(mContext);
|
||||
@ -87,15 +97,15 @@ public class GPSUtils {
|
||||
// 设置定位精确度 Criteria.ACCURACY_COARSE比较粗略,Criteria.ACCURACY_FINE则比较精细
|
||||
criteria.setAccuracy(Criteria.ACCURACY_FINE);
|
||||
// 设置是否要求速度
|
||||
criteria.setSpeedRequired(false);
|
||||
criteria.setSpeedRequired(true);
|
||||
// 设置是否允许运营商收费
|
||||
criteria.setCostAllowed(false);
|
||||
criteria.setCostAllowed(true);
|
||||
// 设置是否需要方位信息
|
||||
criteria.setBearingRequired(false);
|
||||
criteria.setBearingRequired(true);
|
||||
// 设置是否需要海拔信息
|
||||
criteria.setAltitudeRequired(false);
|
||||
criteria.setAltitudeRequired(true);
|
||||
// 设置对电源的需求
|
||||
criteria.setPowerRequirement(Criteria.POWER_LOW);
|
||||
criteria.setPowerRequirement(Criteria.POWER_MEDIUM);
|
||||
return criteria;
|
||||
}
|
||||
|
||||
@ -153,6 +163,12 @@ public class GPSUtils {
|
||||
Log.i(TAG, "经度:" + location.getLongitude());
|
||||
Log.i(TAG, "纬度:" + location.getLatitude());
|
||||
Log.i(TAG, "海拔:" + location.getAltitude());
|
||||
|
||||
Message currentLocationMsg = Message.obtain();
|
||||
currentLocationMsg.what = Constant.EVENT_WHAT_LOCATION_CHANGE;
|
||||
currentLocationMsg.arg1 = LocationLifeCycle.LOCATION_FROM.ORIGIN.ordinal(); // 设置定位来源
|
||||
currentLocationMsg.obj = location;
|
||||
EventBus.getDefault().post(currentLocationMsg);
|
||||
}
|
||||
|
||||
//GPS状态变化时触发
|
||||
@ -228,19 +244,14 @@ public class GPSUtils {
|
||||
return;
|
||||
}
|
||||
Log.i(TAG, "卫星状态改变");
|
||||
if (onClickGPSStatus != null) {
|
||||
onClickGPSStatus.onGpsCount(status.getSatelliteCount());
|
||||
if (status != null) {
|
||||
sateliteCount = status.getSatelliteCount();
|
||||
}
|
||||
}
|
||||
};
|
||||
public OnClickGPSStatus onClickGPSStatus;
|
||||
|
||||
public void setOnClickGPSStatus(OnClickGPSStatus onClickGPSStatus) {
|
||||
this.onClickGPSStatus = onClickGPSStatus;
|
||||
}
|
||||
|
||||
public interface OnClickGPSStatus {
|
||||
void onGpsCount(int count);
|
||||
public int getSateliteCount() {
|
||||
return sateliteCount;
|
||||
}
|
||||
|
||||
public void unRegisterAllListener() {
|
||||
|
@ -2,30 +2,167 @@ package com.navinfo.outdoor.util;
|
||||
|
||||
import android.app.Activity;
|
||||
import android.content.Context;
|
||||
import android.location.Location;
|
||||
import android.os.Message;
|
||||
|
||||
import androidx.annotation.NonNull;
|
||||
import androidx.lifecycle.DefaultLifecycleObserver;
|
||||
import androidx.lifecycle.LifecycleOwner;
|
||||
|
||||
import com.navinfo.outdoor.api.Constant;
|
||||
import com.tencent.map.geolocation.TencentLocation;
|
||||
import com.tencent.tencentmap.mapsdk.maps.LocationSource;
|
||||
|
||||
import org.greenrobot.eventbus.EventBus;
|
||||
import org.greenrobot.eventbus.Subscribe;
|
||||
import org.greenrobot.eventbus.ThreadMode;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
public class LocationLifeCycle implements DefaultLifecycleObserver {
|
||||
private Activity mContext;
|
||||
private LOCATION_FROM mainLocationFrom = LOCATION_FROM.ORIGIN/*主要位置源*/, referenceLocationFrom = LOCATION_FROM.TENCENT/*参考位置源*/;
|
||||
private TencentLocation tencentLocation; // 腾讯定位获取到的最新位置信息
|
||||
private Location gpsLocation; // 原生系统获取到的最新位置
|
||||
private List<LocationSource.OnLocationChangedListener> locationChangedListenerList;
|
||||
private static LocationLifeCycle instance;
|
||||
|
||||
public LocationLifeCycle(Activity mContext) {
|
||||
public static LocationLifeCycle getInstance() {
|
||||
if (instance == null) {
|
||||
instance = new LocationLifeCycle();
|
||||
}
|
||||
return instance;
|
||||
}
|
||||
|
||||
public void init(Activity mContext) {
|
||||
this.mContext = mContext;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onCreate(@NonNull LifecycleOwner owner) {
|
||||
DefaultLifecycleObserver.super.onCreate(owner);
|
||||
locationChangedListenerList = new ArrayList<>();
|
||||
// 开启腾讯定位
|
||||
TalentLocationUtils.getInstance(mContext).startLocation(mContext);
|
||||
// 开启原生定位
|
||||
GPSUtils.getInstance(mContext).startLocation();
|
||||
if (!EventBus.getDefault().isRegistered(this)) {
|
||||
EventBus.getDefault().register(this);
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onDestroy(@NonNull LifecycleOwner owner) {
|
||||
DefaultLifecycleObserver.super.onDestroy(owner);
|
||||
locationChangedListenerList.clear();
|
||||
locationChangedListenerList = null;
|
||||
TalentLocationUtils.getInstance(mContext).stopLocation();
|
||||
GPSUtils.getInstance(mContext).unRegisterAllListener();
|
||||
if (EventBus.getDefault().isRegistered(this)) {
|
||||
EventBus.getDefault().unregister(this);
|
||||
}
|
||||
}
|
||||
|
||||
@Subscribe(threadMode = ThreadMode.MAIN)
|
||||
public void onEventMessageMainThread(Message msg) {
|
||||
Location mapLocation;
|
||||
if (msg.what == Constant.EVENT_WHAT_LOCATION_CHANGE) { // 用户位置更新
|
||||
if (msg.arg1 == LOCATION_FROM.ORIGIN.ordinal()) {
|
||||
gpsLocation = (Location) msg.obj;
|
||||
if (msg.arg1 == mainLocationFrom.ordinal()) { // 如果当前定位方式是主要定位方式
|
||||
if (locationChangedListenerList!=null) {
|
||||
for (LocationSource.OnLocationChangedListener locationChangedListener:locationChangedListenerList) {
|
||||
locationChangedListener.onLocationChanged(gpsLocation);
|
||||
}
|
||||
}
|
||||
}
|
||||
} else if (msg.arg1 == LOCATION_FROM.TENCENT.ordinal()) {
|
||||
tencentLocation = (TencentLocation) msg.obj;
|
||||
if (msg.arg1 == mainLocationFrom.ordinal()) { // 如果当前定位方式是主要定位方式
|
||||
MyLocation location = new MyLocation(tencentLocation.getProvider(), tencentLocation);
|
||||
//设置经纬度以及精度
|
||||
location.setLatitude(tencentLocation.getLatitude());
|
||||
location.setLongitude(tencentLocation.getLongitude());
|
||||
location.setAccuracy(tencentLocation.getAccuracy());
|
||||
if (locationChangedListenerList!=null) {
|
||||
for (LocationSource.OnLocationChangedListener locationChangedListener:locationChangedListenerList) {
|
||||
locationChangedListener.onLocationChanged(location);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public List<LocationSource.OnLocationChangedListener> getLocationChangedListenerList() {
|
||||
return locationChangedListenerList;
|
||||
}
|
||||
|
||||
/**
|
||||
* 设置主要可用的定位来源
|
||||
* */
|
||||
public void setMainLocationFrom(LOCATION_FROM mainLocationFrom) {
|
||||
this.mainLocationFrom = mainLocationFrom;
|
||||
}
|
||||
|
||||
/**
|
||||
* 设置次要可用的定位来源
|
||||
* */
|
||||
public void setReferenceLocationFrom(LOCATION_FROM referenceLocationFrom) {
|
||||
this.referenceLocationFrom = referenceLocationFrom;
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取主要位置信息
|
||||
* */
|
||||
public MyLocation getMainLocation() {
|
||||
switch (mainLocationFrom) {
|
||||
case ORIGIN:
|
||||
if (gpsLocation!=null) {
|
||||
return new MyLocation(gpsLocation.getProvider(), gpsLocation);
|
||||
}
|
||||
break;
|
||||
case TENCENT:
|
||||
if (tencentLocation!=null) {
|
||||
return new MyLocation(tencentLocation.getProvider(), tencentLocation);
|
||||
}
|
||||
break;
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取参考位置信息
|
||||
* */
|
||||
|
||||
/**
|
||||
* 获取主要位置信息
|
||||
* */
|
||||
public MyLocation getReferenceLocation() {
|
||||
switch (referenceLocationFrom) {
|
||||
case ORIGIN:
|
||||
if (gpsLocation!=null) {
|
||||
return new MyLocation(gpsLocation.getProvider(), gpsLocation);
|
||||
}
|
||||
break;
|
||||
case TENCENT:
|
||||
if (tencentLocation!=null) {
|
||||
return new MyLocation(tencentLocation.getProvider(), tencentLocation);
|
||||
}
|
||||
break;
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
public enum LOCATION_FROM {
|
||||
ORIGIN, TENCENT, BAIDU, AMAP
|
||||
}
|
||||
|
||||
private boolean checkInit() throws Exception {
|
||||
if (this.mContext == null) {
|
||||
throw new Exception("LocationLifeCycle 没有初始化!");
|
||||
}
|
||||
return true;
|
||||
}
|
||||
}
|
||||
|
39
app/src/main/java/com/navinfo/outdoor/util/MyLocation.java
Normal file
39
app/src/main/java/com/navinfo/outdoor/util/MyLocation.java
Normal file
@ -0,0 +1,39 @@
|
||||
package com.navinfo.outdoor.util;
|
||||
|
||||
import android.location.Address;
|
||||
import android.location.Geocoder;
|
||||
import android.location.Location;
|
||||
|
||||
import com.tencent.map.geolocation.TencentLocation;
|
||||
|
||||
import java.util.List;
|
||||
import java.util.Locale;
|
||||
|
||||
public class MyLocation extends Location {
|
||||
private String city;
|
||||
private String cityCode;
|
||||
private String cityPhoneCode;
|
||||
|
||||
public MyLocation(String provider, TencentLocation tencentLocation) {
|
||||
super(provider);
|
||||
this.city = tencentLocation.getCity();
|
||||
this.cityCode = tencentLocation.getCityCode();
|
||||
this.cityPhoneCode = tencentLocation.getCityPhoneCode();
|
||||
this.setAccuracy(tencentLocation.getAccuracy());
|
||||
this.setAltitude(tencentLocation.getAltitude());
|
||||
if (tencentLocation.getBearing()!=0) {
|
||||
this.setBearing(tencentLocation.getBearing());
|
||||
} else {
|
||||
this.setBearing((float) tencentLocation.getDirection());
|
||||
}
|
||||
this.setElapsedRealtimeNanos(tencentLocation.getElapsedRealtime());
|
||||
this.setLatitude(tencentLocation.getLatitude());
|
||||
this.setLongitude(tencentLocation.getLongitude());
|
||||
this.setSpeed(tencentLocation.getSpeed());
|
||||
this.setTime(tencentLocation.getTime());
|
||||
}
|
||||
|
||||
public MyLocation(String provider, Location location) {
|
||||
super(location);
|
||||
}
|
||||
}
|
@ -20,13 +20,13 @@ public class MyTecentLocationSource implements LocationSource{
|
||||
@Override
|
||||
public void activate(OnLocationChangedListener onLocationChangedListener) {
|
||||
locationChangedListener = onLocationChangedListener;
|
||||
TalentLocationUtils.getInstance(mContext).getLocationChangedListenerList().add(onLocationChangedListener);
|
||||
LocationLifeCycle.getInstance().getLocationChangedListenerList().add(onLocationChangedListener);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void deactivate() {
|
||||
if (locationChangedListener!=null) {
|
||||
TalentLocationUtils.getInstance(mContext).getLocationChangedListenerList().remove(locationChangedListener);
|
||||
LocationLifeCycle.getInstance().getLocationChangedListenerList().remove(locationChangedListener);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -6,6 +6,7 @@ import android.os.Looper;
|
||||
import android.os.Message;
|
||||
import android.util.Log;
|
||||
|
||||
import com.github.lazylibrary.util.ToastUtils;
|
||||
import com.navinfo.outdoor.api.Constant;
|
||||
import com.tencent.map.fusionlocation.observer.TencentGeoLocationObserver;
|
||||
import com.tencent.map.geolocation.TencentLocation;
|
||||
@ -22,7 +23,6 @@ import java.util.List;
|
||||
public class TalentLocationUtils implements TencentLocationListener {
|
||||
private TencentLocationManager locationManager;
|
||||
private TencentLocationRequest locationRequest;
|
||||
private List<LocationSource.OnLocationChangedListener> locationChangedListenerList;
|
||||
private boolean isLocationStart; // 标识是否已经启动定位
|
||||
private static TalentLocationUtils instance;
|
||||
public static TalentLocationUtils getInstance(Context mContext) {
|
||||
@ -37,7 +37,6 @@ public class TalentLocationUtils implements TencentLocationListener {
|
||||
}
|
||||
|
||||
private void init(Context mContext) {
|
||||
locationChangedListenerList = new ArrayList<>();
|
||||
//用于访问腾讯定位服务的类, 周期性向客户端提供位置更新
|
||||
locationManager = TencentLocationManager.getInstance(mContext, null);
|
||||
//设置坐标系
|
||||
@ -59,13 +58,13 @@ public class TalentLocationUtils implements TencentLocationListener {
|
||||
isLocationStart = true;
|
||||
switch (err) {
|
||||
case 1:
|
||||
ToastUtil.showShort(mContext, Constant.GET_ERR_MESSAGE1);
|
||||
ToastUtils.showToast(mContext, Constant.GET_ERR_MESSAGE1);
|
||||
break;
|
||||
case 2:
|
||||
ToastUtil.showShort(mContext, Constant.GET_ERR_MESSAGE2);
|
||||
ToastUtils.showToast(mContext, Constant.GET_ERR_MESSAGE2);
|
||||
break;
|
||||
case 3:
|
||||
ToastUtil.showShort(mContext, Constant.GET_ERR_MESSAGE3);
|
||||
ToastUtils.showToast(mContext, Constant.GET_ERR_MESSAGE3);
|
||||
break;
|
||||
default:
|
||||
break;
|
||||
@ -78,8 +77,6 @@ public class TalentLocationUtils implements TencentLocationListener {
|
||||
locationManager.removeUpdates(this);
|
||||
locationManager = null;
|
||||
locationRequest = null;
|
||||
locationChangedListenerList.clear();
|
||||
locationChangedListenerList = null;
|
||||
}
|
||||
}
|
||||
|
||||
@ -90,25 +87,10 @@ public class TalentLocationUtils implements TencentLocationListener {
|
||||
@Override
|
||||
public void onLocationChanged(TencentLocation tencentLocation, int i, String s) {
|
||||
if (i == TencentLocation.ERROR_OK) {//定位成功
|
||||
Location location = new Location(tencentLocation.getProvider());
|
||||
// //设置经纬度以及精度
|
||||
location.setLatitude(tencentLocation.getLatitude());
|
||||
location.setLongitude(tencentLocation.getLongitude());
|
||||
location.setAccuracy(tencentLocation.getAccuracy());
|
||||
if (tencentLocation.getBearing()!=0) {
|
||||
location.setBearing(tencentLocation.getBearing());
|
||||
} else {
|
||||
location.setBearing((float) tencentLocation.getDirection());
|
||||
}
|
||||
if (locationChangedListenerList!=null) {
|
||||
for (LocationSource.OnLocationChangedListener locationChangedListener:locationChangedListenerList) {
|
||||
locationChangedListener.onLocationChanged(location);
|
||||
}
|
||||
}
|
||||
Constant.currentLocation = tencentLocation;
|
||||
Message currentLocationMsg = Message.obtain();
|
||||
currentLocationMsg.what = Constant.EVENT_WHAT_LOCATION_CHANGE;
|
||||
currentLocationMsg.obj = Constant.currentLocation;
|
||||
currentLocationMsg.arg1 = LocationLifeCycle.LOCATION_FROM.TENCENT.ordinal(); // 设置定位来源
|
||||
currentLocationMsg.obj = tencentLocation;
|
||||
EventBus.getDefault().post(currentLocationMsg);
|
||||
}else if (i==TencentLocation.ERROR_NETWORK){//网络问题引起的定位失败
|
||||
Message currentLocationMsg = Message.obtain();
|
||||
@ -148,8 +130,4 @@ public class TalentLocationUtils implements TencentLocationListener {
|
||||
public void onNmeaMsgChanged(String s) {
|
||||
|
||||
}
|
||||
|
||||
public List<LocationSource.OnLocationChangedListener> getLocationChangedListenerList() {
|
||||
return locationChangedListenerList;
|
||||
}
|
||||
}
|
||||
|
Loading…
x
Reference in New Issue
Block a user