Files
CubeAoleyunSN/app/src/main/java/com/aoleyun/sn/manager/AmapManager.java
Godfather 683f31087f version:
fix:优化请求次数
update:
2022-04-02 18:37:49 +08:00

91 lines
3.8 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.aoleyun.sn.manager;
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.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内精度最高的一次定位结果。
// 如果设置其为truesetOnceLocation(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) {
Log.e(TAG, "onLocationChanged: " + "定位成功");
Log.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());
SPUtils.put(mContext, "AmapError", "");
} else {
//定位失败
sb.append("定位失败" + "\n");
Log.e(TAG, "onLocationChanged: " + "定位失败");
SPUtils.put(mContext, "AmapError", String.valueOf(aMapLocation.getErrorInfo()));
}
Log.e(TAG, (String) SPUtils.get(mContext, "AmapAddress", "-"));
Log.e(TAG, (String) SPUtils.get(mContext, "AmapError", "-"));
Log.e(TAG, "amap: " + sb.toString());
}
});
//设置场景模式后最好调用一次stop再调用start以保证场景模式生效
locationClient.stopLocation();
locationClient.startLocation();
Log.e(TAG, "initAmap: " + "startLocation");
}
}