version:1.1

fix:
update:迁移到UIUISOS
This commit is contained in:
2022-10-21 14:18:49 +08:00
parent 95f7d25307
commit ba540d4689
157 changed files with 1301 additions and 633 deletions

View File

@@ -0,0 +1,141 @@
package com.uiuios.aios.manager;
import android.annotation.SuppressLint;
import android.content.Context;
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.tencent.mmkv.MMKV;
import java.lang.reflect.Type;
public class AmapManager {
private String TAG = AmapManager.class.getSimpleName();
private static final String AMAPLOCATION_JSON_KEY = "AMAPLOCATION_JSON_STRING";
public static final String LONGITUDE_KEY = "amap_longitude_key";
public static final String LATITUDE_KEY = "amap_latitude_key";
public static final String ADDRESS_KEY = "amap_address_key";
@SuppressLint("StaticFieldLeak")
private static AmapManager sInstance;
private Context mContext;
private MMKV mMMKV = MMKV.defaultMMKV();
private AMapLocationClient locationClient = null;
private AMapLocation nowAMapLocation;
private AmapManager(Context context) {
this.mContext = context;
}
public static void init(Context context) {
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;
}
public AMapLocationClient getLocationClient() {
if (null == locationClient) {
initAmap();
}
return locationClient;
}
public void initAmap() {
locationClient = new AMapLocationClient(mContext);
AMapLocationClientOption option = new AMapLocationClientOption();
option.setLocationPurpose(AMapLocationClientOption.AMapLocationPurpose.SignIn);
option.setLocationMode(AMapLocationClientOption.AMapLocationMode.Hight_Accuracy);
option.setNeedAddress(true);
//获取一次定位结果:
//该方法默认为false。
option.setOnceLocation(true);
//获取最近3s内精度最高的一次定位结果
//设置setOnceLocationLatest(boolean b)接口为true启动定位时SDK会返回最近3s内精度最高的一次定位结果。
// 如果设置其为truesetOnceLocation(boolean b)接口也会被设置为true反之不会默认为false。
option.setOnceLocationLatest(true);
locationClient.setLocationOption(option);
//设置定位模式为AMapLocationMode.Hight_Accuracy高精度模式。
//设置定位监听
locationClient.setLocationListener(new AMapLocationListener() {
@Override
public void onLocationChanged(AMapLocation aMapLocation) {
//errCode等于0代表定位成功其他的为定位失败具体的可以参照官网定位错误码说明
if (aMapLocation.getErrorCode() == 0) {
Log.e(TAG, "onLocationChanged: " + "定位成功");
Log.e(TAG, "onLocationChanged: " + aMapLocation.getAddress());
Log.e(TAG, "onLocationChanged: getLongitude = " + aMapLocation.getLongitude());
mMMKV.encode(LONGITUDE_KEY, String.valueOf(aMapLocation.getLongitude()));
Log.e(TAG, "onLocationChanged: getLatitude = " + aMapLocation.getLatitude());
mMMKV.encode(LATITUDE_KEY, String.valueOf(aMapLocation.getLatitude()));
Log.e(TAG, "onLocationChanged: getAddress = " + aMapLocation.getAddress());
mMMKV.encode(ADDRESS_KEY, aMapLocation.getAddress());
} else {
//定位失败
Log.e(TAG, "onLocationChanged: " + "定位失败");
Log.e(TAG, "onLocationChanged: " + aMapLocation.getErrorInfo());
}
}
});
//设置场景模式后最好调用一次stop再调用start以保证场景模式生效
locationClient.stopLocation();
locationClient.startLocation();
Log.e(TAG, "initAmap: " + "startLocation");
}
public AMapLocation getNowAMapLocation() {
if (nowAMapLocation == null) {
String aMapLocationjson = mMMKV.decodeString(AMAPLOCATION_JSON_KEY, "");
Type type = new TypeToken<AMapLocation>() {
}.getType();
AMapLocation aMapLocation = new Gson().fromJson(aMapLocationjson, type);
return aMapLocation;
}
return this.nowAMapLocation;
}
public void startLocation(AMapLocationListener aMapLocationListener) {
initAmap();
locationClient.stopLocation();
locationClient.startLocation();
locationClient.setLocationListener(new AMapLocationListener() {
@Override
public void onLocationChanged(AMapLocation aMapLocation) {
Log.d(TAG, "onLocationChanged: " + aMapLocation.toStr());
if (aMapLocation.getErrorCode() == 0) {
nowAMapLocation = aMapLocation;
mMMKV.encode(AMAPLOCATION_JSON_KEY, aMapLocation.toStr());
mMMKV.encode(LONGITUDE_KEY, String.valueOf(aMapLocation.getLongitude()));
mMMKV.encode(LATITUDE_KEY, String.valueOf(aMapLocation.getLatitude()));
mMMKV.encode(ADDRESS_KEY, aMapLocation.getAddress());
} else {
mMMKV.encode(AMAPLOCATION_JSON_KEY, "");
}
aMapLocationListener.onLocationChanged(aMapLocation);
}
});
}
public String getLocation() {
AMapLocation aMapLocation = getNowAMapLocation();
if (aMapLocation == null) {
return "0,0";
} else {
String location = aMapLocation.getLongitude() + "," + aMapLocation.getLatitude();
Log.e(TAG, "getLocation: " + location);
return location;
}
}
}