version:1.7.9
fix:修复竖屏报错 update:增加客服,适配不同版本ai精准学
This commit is contained in:
216
app/src/main/java/com/uiui/zyos/manager/AmapManager.java
Normal file
216
app/src/main/java/com/uiui/zyos/manager/AmapManager.java
Normal file
@@ -0,0 +1,216 @@
|
||||
package com.uiui.zyos.manager;
|
||||
|
||||
import android.annotation.SuppressLint;
|
||||
import android.content.Context;
|
||||
import android.text.TextUtils;
|
||||
import android.util.Log;
|
||||
|
||||
import com.amap.api.location.AMapLocation;
|
||||
import com.amap.api.location.AMapLocationClient;
|
||||
import com.amap.api.location.AMapLocationClientOption;
|
||||
import com.amap.api.location.AMapLocationListener;
|
||||
import com.google.gson.Gson;
|
||||
import com.google.gson.reflect.TypeToken;
|
||||
import com.jeremyliao.liveeventbus.LiveEventBus;
|
||||
import com.tencent.mmkv.MMKV;
|
||||
import com.uiui.zyos.bean.BaseResponse;
|
||||
import com.uiui.zyos.bean.MapBean;
|
||||
import com.uiui.zyos.config.CommonConfig;
|
||||
import com.uiui.zyos.gson.GsonUtils;
|
||||
import com.uiui.zyos.network.NetInterfaceManager;
|
||||
import com.uiui.zyos.utils.ActivationUtil;
|
||||
|
||||
import java.lang.reflect.Type;
|
||||
|
||||
import io.reactivex.rxjava3.annotations.NonNull;
|
||||
import io.reactivex.rxjava3.core.Observer;
|
||||
import io.reactivex.rxjava3.disposables.Disposable;
|
||||
|
||||
public class AmapManager {
|
||||
private static final String TAG = AmapManager.class.getSimpleName();
|
||||
|
||||
MMKV mMMKV = MMKV.mmkvWithID(CommonConfig.MMKV_ID, MMKV.MULTI_PROCESS_MODE);
|
||||
|
||||
@SuppressLint("StaticFieldLeak")
|
||||
private static AmapManager sInstance;
|
||||
private Context mContext;
|
||||
|
||||
private AMapLocationClient mAMapLocationClient;
|
||||
private AMapLocationClientOption mAMapLocationClientOption;
|
||||
private MapBean mMapBean;
|
||||
|
||||
private AmapManager(Context context) {
|
||||
this.mContext = context;
|
||||
initAmap();
|
||||
}
|
||||
|
||||
public static void init(Context context) {
|
||||
if (context == null) {
|
||||
throw new RuntimeException("Context is NULL");
|
||||
}
|
||||
if (sInstance == null) {
|
||||
sInstance = new AmapManager(context);
|
||||
}
|
||||
}
|
||||
|
||||
public static AmapManager getInstance() {
|
||||
if (sInstance == null) {
|
||||
throw new IllegalStateException("You must be init AmapManager first");
|
||||
}
|
||||
|
||||
return sInstance;
|
||||
}
|
||||
|
||||
private AMapLocationClientOption getDefaultOption() {
|
||||
if (mAMapLocationClientOption == null) {
|
||||
mAMapLocationClientOption = new AMapLocationClientOption();
|
||||
}
|
||||
mAMapLocationClientOption.setLocationPurpose(AMapLocationClientOption.AMapLocationPurpose.SignIn);
|
||||
//设置定位模式为AMapLocationMode.Hight_Accuracy,高精度模式。
|
||||
mAMapLocationClientOption.setLocationMode(AMapLocationClientOption.AMapLocationMode.Hight_Accuracy);
|
||||
mAMapLocationClientOption.setNeedAddress(true);
|
||||
//获取一次定位结果:
|
||||
//该方法默认为false。
|
||||
mAMapLocationClientOption.setOnceLocation(true);
|
||||
//获取最近3s内精度最高的一次定位结果:
|
||||
//设置setOnceLocationLatest(boolean b)接口为true,启动定位时SDK会返回最近3s内精度最高的一次定位结果。
|
||||
// 如果设置其为true,setOnceLocation(boolean b)接口也会被设置为true,反之不会,默认为false。
|
||||
mAMapLocationClientOption.setOnceLocationLatest(true);
|
||||
return mAMapLocationClientOption;
|
||||
}
|
||||
|
||||
public void initAmap() {
|
||||
if (mAMapLocationClient == null) {
|
||||
mAMapLocationClient = new AMapLocationClient(mContext);
|
||||
}
|
||||
mAMapLocationClient.setLocationOption(getDefaultOption());
|
||||
|
||||
//设置定位监听
|
||||
mAMapLocationClient.setLocationListener(mAMapLocationListener);
|
||||
//设置场景模式后最好调用一次stop,再调用start以保证场景模式生效
|
||||
|
||||
startLocation();
|
||||
|
||||
String jsonString = mMMKV.decodeString(CommonConfig.MAP_LOCATION_JSON_KEY, "");
|
||||
if (!TextUtils.isEmpty(jsonString)) {
|
||||
Gson gson = new Gson();
|
||||
Type type = new TypeToken<MapBean>() {
|
||||
}.getType();
|
||||
mMapBean = gson.fromJson(jsonString, type);
|
||||
} else {
|
||||
Log.e(TAG, "initAmap: jsonString is empty");
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
public void startLocation() {
|
||||
boolean activation = ActivationUtil.isActivation(mContext);
|
||||
if (!activation) {
|
||||
return;
|
||||
}
|
||||
mAMapLocationClient.stopLocation();
|
||||
mAMapLocationClient.startLocation();
|
||||
Log.e(TAG, "initAmap: " + "startLocation");
|
||||
}
|
||||
|
||||
private AMapLocationListener mAMapLocationListener = new AMapLocationListener() {
|
||||
@Override
|
||||
public void onLocationChanged(AMapLocation aMapLocation) {
|
||||
StringBuilder sb = new StringBuilder();
|
||||
//errCode等于0代表定位成功,其他的为定位失败,具体的可以参照官网定位错误码说明
|
||||
if (aMapLocation.getErrorCode() == 0) {
|
||||
Log.e(TAG, "onLocationChanged: " + "定位成功");
|
||||
updateAddress(aMapLocation);
|
||||
mMapBean = getMapBean(aMapLocation);
|
||||
saveMapResult(mMapBean);
|
||||
LiveEventBus.get("MapBean")
|
||||
.post(mMapBean);
|
||||
Log.e(TAG, "onLocationChanged: " + aMapLocation.getAddress());
|
||||
sb.append(aMapLocation.getAddress()).append("\n");
|
||||
|
||||
} else {
|
||||
//定位失败
|
||||
sb.append("定位失败" + "\n");
|
||||
sb.append(aMapLocation.getErrorInfo());
|
||||
Log.e(TAG, "onLocationChanged: " + "定位失败");
|
||||
}
|
||||
Log.e(TAG, "amap: " + sb.toString());
|
||||
}
|
||||
};
|
||||
|
||||
private void updateAddress(AMapLocation aMapLocation) {
|
||||
boolean activation = ActivationUtil.isActivation(mContext);
|
||||
if (!activation) {
|
||||
return;
|
||||
}
|
||||
NetInterfaceManager.getInstance().getUpdateAddressObservable(aMapLocation.getAddress()
|
||||
, aMapLocation.getLongitude(), aMapLocation.getLatitude()
|
||||
)
|
||||
.subscribe(new Observer<BaseResponse>() {
|
||||
@Override
|
||||
public void onSubscribe(@NonNull Disposable d) {
|
||||
Log.e("updateAddress", "onSubscribe: ");
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onNext(@NonNull BaseResponse baseResponse) {
|
||||
Log.e("updateAddress", "onNext: " + baseResponse);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onError(@NonNull Throwable e) {
|
||||
Log.e("updateAddress", "onError: " + e.getMessage());
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onComplete() {
|
||||
Log.e("updateAddress", "onComplete: ");
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
public String getLocationTude() {
|
||||
if (mMapBean != null) {
|
||||
return mMapBean.getLongitude() + "," + mMapBean.getLatitude();
|
||||
} else {
|
||||
startLocation();
|
||||
return CommonConfig.DEFAULT_LOCATION_TUDE;
|
||||
}
|
||||
}
|
||||
|
||||
public String getDistrict() {
|
||||
if (mMapBean != null) {
|
||||
return mMapBean.getDistrict();
|
||||
} else {
|
||||
startLocation();
|
||||
return CommonConfig.DEFAULT_LOCATION_DISTRICT;
|
||||
}
|
||||
}
|
||||
|
||||
private MapBean getMapBean(AMapLocation location) {
|
||||
MapBean mapBean = new MapBean();
|
||||
mapBean.setLongitude(location.getLongitude());
|
||||
mapBean.setLatitude(location.getLatitude());
|
||||
mapBean.setAdcode(location.getAdCode());
|
||||
mapBean.setAddress(location.getAddress());
|
||||
mapBean.setCity(location.getCity());
|
||||
mapBean.setCityCode(location.getCityCode());
|
||||
mapBean.setCountry(location.getCountry());
|
||||
mapBean.setCountryCode(location.getAdCode());
|
||||
mapBean.setDistrict(location.getDistrict());
|
||||
mapBean.setProvince(location.getProvince());
|
||||
mapBean.setStreet(location.getStreet());
|
||||
mapBean.setStreetNumber(location.getStreetNum());
|
||||
mapBean.setTown(location.getStreet());
|
||||
mapBean.setLocationDescribe(location.getLocationDetail());
|
||||
Log.e(TAG, "getMapBean: " + GsonUtils.toJSONString(mapBean));
|
||||
return mapBean;
|
||||
|
||||
}
|
||||
|
||||
private void saveMapResult(MapBean mapBean) {
|
||||
Log.e(TAG, "saveMapResult: " + GsonUtils.toJSONString(mapBean));
|
||||
mMMKV.encode(CommonConfig.MAP_LOCATION_JSON_KEY, GsonUtils.toJSONString(mapBean));
|
||||
}
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user