Files
Xuewang365OSNeutral/app/src/main/java/com/uiui/aios/manager/AmapManager.java
tongtongstudio b87c2e99b9 version:6.3
fix:
update:优化管控页面
2022-11-17 09:36:01 +08:00

143 lines
6.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.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.blankj.utilcode.util.SPUtils;
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 = SPUtils.getInstance().getString(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;
SPUtils.getInstance().put(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 {
SPUtils.getInstance().put(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;
}
}
}