version:1.0
update:2021-10-13 18:52:13 fix:去除okgo,rxAndroid1,优化依赖 add:切换到奥乐云平台
This commit is contained in:
89
app/src/main/java/com/aoleyun/sn/manager/AmapManager.java
Normal file
89
app/src/main/java/com/aoleyun/sn/manager/AmapManager.java
Normal file
@@ -0,0 +1,89 @@
|
||||
package com.aoleyun.sn.manager;
|
||||
|
||||
import android.content.Context;
|
||||
|
||||
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.aoleyun.sn.utils.Logutils;
|
||||
import com.aoleyun.sn.utils.SPUtils;
|
||||
|
||||
public class AmapManager {
|
||||
private static AmapManager sInstance;
|
||||
private Context mContext;
|
||||
public static AMapLocationClient locationClient = null;
|
||||
private String TAG = AmapManager.class.getSimpleName();
|
||||
|
||||
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内精度最高的一次定位结果。
|
||||
// 如果设置其为true,setOnceLocation(boolean b)接口也会被设置为true,反之不会,默认为false。
|
||||
option.setOnceLocationLatest(true);
|
||||
locationClient.setLocationOption(option);
|
||||
//设置定位模式为AMapLocationMode.Hight_Accuracy,高精度模式。
|
||||
//设置定位监听
|
||||
locationClient.setLocationListener(new AMapLocationListener() {
|
||||
|
||||
@Override
|
||||
public void onLocationChanged(AMapLocation aMapLocation) {
|
||||
StringBuilder sb = new StringBuilder();
|
||||
//errCode等于0代表定位成功,其他的为定位失败,具体的可以参照官网定位错误码说明
|
||||
if (aMapLocation.getErrorCode() == 0) {
|
||||
Logutils.e(TAG, "onLocationChanged: " + "定位成功");
|
||||
Logutils.e(TAG, "onLocationChanged: " + aMapLocation.getAddress());
|
||||
sb.append(aMapLocation.getAddress()).append("\n");
|
||||
SPUtils.put(mContext, "AmapAddress", aMapLocation.getAddress());
|
||||
SPUtils.put(mContext, "longitude", aMapLocation.getLongitude());
|
||||
SPUtils.put(mContext, "latitude", aMapLocation.getLatitude());
|
||||
} else {
|
||||
//定位失败
|
||||
sb.append("定位失败" + "\n");
|
||||
Logutils.e(TAG, "onLocationChanged: " + "定位失败");
|
||||
SPUtils.put(mContext, "AmapError", String.valueOf(aMapLocation.getErrorInfo()));
|
||||
}
|
||||
Logutils.e(TAG, (String) SPUtils.get(mContext, "AmapAddress", "-"));
|
||||
Logutils.e(TAG, (String) SPUtils.get(mContext, "AmapError", "-"));
|
||||
Logutils.e(TAG, "amap: " + sb.toString());
|
||||
}
|
||||
});
|
||||
//设置场景模式后最好调用一次stop,再调用start以保证场景模式生效
|
||||
locationClient.stopLocation();
|
||||
locationClient.startLocation();
|
||||
Logutils.e(TAG, "initAmap: " + "startLocation");
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user