Files
Xuewang365OSNeutral/app/src/main/java/com/uiui/zyos/manager/AmapManager.java
tongtongstudio 8229219fc1 version:2.0.8
bugfixes:
update:更滑图标,增加获取系统设置
2025-09-08 18:19:15 +08:00

222 lines
8.1 KiB
Java
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
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";
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内精度最高的一次定位结果。
// 如果设置其为truesetOnceLocation(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();
try {
mMapBean = gson.fromJson(jsonString, type);
} catch (Exception e) {
Log.e(TAG, "initAmap: ");
mMMKV.remove(CommonConfig.MAP_LOCATION_JSON_KEY);
}
} else {
Log.e(TAG, "initAmap: jsonString is empty");
}
}
public void startLocation() {
boolean activation = ActivationUtil.isActivation();
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();
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));
}
}