LocationManager.java
13.7 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
package com.glsx.aicar.manager;
import android.os.Handler;
import android.text.TextUtils;
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.amap.api.location.AMapLocationQualityReport;
import com.glsx.aicar.app.AiCarApplication;
import com.blankj.utilcode.util.LogUtils;
import com.glsx.commonres.util.ToastUtil;
import com.glsx.commonres.util.ToolUtils;
import java.util.ArrayList;
/**
* 定位管理器工具,后续可根据需求改动
*/
public class LocationManager {
private String TAG = "LocationManager";
// private LocationClient client = null;
// private LocationClientOption mOption;
// private BDLocation mLocation;
//高德地图
private AMapLocationClient locationClient = null;
private AMapLocationClientOption locationOption = null;
private AMapLocation mAmapLocation;
public double mLatitude = 0;
public double mLongitude = 0;
private Handler mHandler;
private final long STOP_BDLOCATION_DELAY_TIME = 10 * 60 * 1000;//延迟停止时间10分钟
private LocationManager() {
//高德地图初始化
if (locationClient == null) {
initLocation();
}
mHandler = new Handler();
}
public static LocationManager getInstance() {
return Holder.INSTANCE;
}
private static class Holder {
private static final LocationManager INSTANCE = new LocationManager();
}
private boolean isRegister = false;
/**
* 启动高德定位监听
*/
public void startAMapLocationClientListener() {
LogUtils.e(TAG, "startAMapLocationClientListener , isRegister=" + isRegister);
if (isRegister) {
if (locationClient != null)
// 开始请求定位
locationClient.startLocation();
} else {
//先设置监听,再开始请求定位
registerAMapListener();
}
if (mHandler != null) {
mHandler.removeCallbacks(stopAMapLocationRunnable);
}
}
/**
* 停止高德定位监听
*/
public void stopAmapClientListener() {
LogUtils.e(TAG, "stopAmapClientListener , isRegister=" + isRegister);
if (isRegister) {
unregisterAMapListener();
}
}
public AMapLocation getmAmapLocationn() {
return mAmapLocation;
}
/***
* 注册高德定位监听
*/
private void registerAMapListener() {
if (locationListener != null && !isRegister) {
locationClient.setLocationListener(locationListener);
isRegister = true;
locationClient.startLocation();
}
}
/**
* 注销高德地图定位监听,并停止定位
*/
private void unregisterAMapListener() {
if (locationListener != null && isRegister) {
locationClient.unRegisterLocationListener(locationListener);
isRegister = false;
locationClient.stopLocation();
}
}
//高德地图
public interface IAMapTrackViewLocationObserver {
void onReceiveAMapLocation(AMapLocation location) throws Exception;
}
//高德地图
private ArrayList<IAMapTrackViewLocationObserver> mAMapTrackViewLocationObserverList = new ArrayList<>();
//注册高德地图位置回调监听
public void registerAMapTVListener(IAMapTrackViewLocationObserver hhxObserver) throws Exception {
if (mAMapTrackViewLocationObserverList != null)
mAMapTrackViewLocationObserverList.add(hhxObserver);
if (null != mAmapLocation) {
notifReceiveAMapLocation(mAmapLocation);
}
}
//注销高德地图文字监听
public void unregisterAMapTVListener(IAMapTrackViewLocationObserver hhxObserver) {
if (mAMapTrackViewLocationObserverList != null)
mAMapTrackViewLocationObserverList.remove(hhxObserver);
}
//高德地图
public void notifReceiveAMapLocation(AMapLocation location) throws Exception {
for (int i = 0; i < mAMapTrackViewLocationObserverList.size(); i++) {
mAMapTrackViewLocationObserverList.get(i).onReceiveAMapLocation(location);
}
}
public void stopAMaplocationAfterDelay() {
if (mHandler != null) {
mHandler.removeCallbacks(stopAMapLocationRunnable);
mHandler.postDelayed(stopAMapLocationRunnable, STOP_BDLOCATION_DELAY_TIME);//
}
}
Runnable stopAMapLocationRunnable = new Runnable() {
@Override
public void run() {
LogUtils.e(TAG, "stopBDlocationRunnable in run..");
stopAmapClientListener();
}
};
/**
* 初始化定位
*
* @author hongming.wang
* @since 2.8.0
*/
private void initLocation() {
//初始化client
try {
locationClient = new AMapLocationClient(AiCarApplication.getConext().getApplicationContext());
locationOption = getDefaultOption();
//设置定位参数
locationClient.setLocationOption(locationOption);
} catch (Exception e) {
e.printStackTrace();
}
}
/**
* 默认的定位参数
*
* @author hongming.wang
* @since 2.8.0
*/
private AMapLocationClientOption getDefaultOption() {
AMapLocationClientOption mOption = new AMapLocationClientOption();
mOption.setLocationMode(AMapLocationClientOption.AMapLocationMode.Hight_Accuracy);//可选,设置定位模式,可选的模式有高精度、仅设备、仅网络。默认为高精度模式
mOption.setGpsFirst(false);//可选,设置是否gps优先,只在高精度模式下有效。默认关闭
mOption.setHttpTimeOut(30000);//可选,设置网络请求超时时间。默认为30秒。在仅设备模式下无效
mOption.setInterval(2000 * 30);//可选,设置定位间隔。默认为2秒
mOption.setNeedAddress(true);//可选,设置是否返回逆地理地址信息。默认是true
mOption.setOnceLocation(false);//可选,设置是否单次定位。默认是false
mOption.setOnceLocationLatest(false);//可选,设置是否等待wifi刷新,默认为false.如果设置为true,会自动变为单次定位,持续定位时不要使用
AMapLocationClientOption.setLocationProtocol(AMapLocationClientOption.AMapLocationProtocol.HTTP);//可选, 设置网络请求的协议。可选HTTP或者HTTPS。默认为HTTP
mOption.setSensorEnable(false);//可选,设置是否使用传感器。默认是false
mOption.setWifiScan(true); //可选,设置是否开启wifi扫描。默认为true,如果设置为false会同时停止主动刷新,停止以后完全依赖于系统刷新,定位位置可能存在误差
mOption.setLocationCacheEnable(true); //可选,设置是否使用缓存定位,默认为true
mOption.setGeoLanguage(AMapLocationClientOption.GeoLanguage.DEFAULT);//可选,设置逆地理信息的语言,默认值为默认语言(根据所在地区选择语言)
return mOption;
}
/**
* 定位监听
*/
AMapLocationListener locationListener = new AMapLocationListener() {
@Override
public void onLocationChanged(AMapLocation location) {
if (null != location) {
StringBuffer sb = new StringBuffer();
//errCode等于0代表定位成功,其他的为定位失败,具体的可以参照官网定位错误码说明
if (location.getErrorCode() == 0) {
sb.append("定位成功" + "\n");
sb.append("定位类型: " + location.getLocationType() + "\n");
sb.append("经 度 : " + location.getLongitude() + "\n");
sb.append("纬 度 : " + location.getLatitude() + "\n");
sb.append("精 度 : " + location.getAccuracy() + "米" + "\n");
sb.append("提供者 : " + location.getProvider() + "\n");
sb.append("速 度 : " + location.getSpeed() + "米/秒" + "\n");
sb.append("角 度 : " + location.getBearing() + "\n");
// 获取当前提供定位服务的卫星个数
sb.append("星 数 : " + location.getSatellites() + "\n");
sb.append("国 家 : " + location.getCountry() + "\n");
sb.append("省 : " + location.getProvince() + "\n");
sb.append("市 : " + location.getCity() + "\n");
sb.append("城市编码 : " + location.getCityCode() + "\n");
sb.append("区 : " + location.getDistrict() + "\n");
sb.append("区域 码 : " + location.getAdCode() + "\n");
sb.append("地 址 : " + location.getAddress() + "\n");
sb.append("兴趣点 : " + location.getPoiName() + "\n");
//定位完成的时间
sb.append("定位时间: " + ToolUtils.formatUTC(location.getTime(), "yyyy-MM-dd HH:mm:ss") + "\n");
mAmapLocation = location;
mLatitude = mAmapLocation.getLatitude();
mLongitude = mAmapLocation.getLongitude();
LogUtils.v("LocationManager", "aMapLocation:" + mAmapLocation.getLatitude() + "," + mAmapLocation.getLongitude());
if (!TextUtils.isEmpty(mAmapLocation.getAddress())) {
LogUtils.v("LocationManager", "onReceiveLocation aMapLocation:" + mAmapLocation.getAddress());
try {
notifReceiveAMapLocation(mAmapLocation);
} catch (Exception e) {
e.printStackTrace();
}
}
} else {
//定位失败
sb.append("定位失败" + "\n");
sb.append("错误码:" + location.getErrorCode() + "\n");
sb.append("错误信息:" + location.getErrorInfo() + "\n");
sb.append("错误描述:" + location.getLocationDetail() + "\n");
}
sb.append("***定位质量报告***").append("\n");
sb.append("* WIFI开关:").append(location.getLocationQualityReport().isWifiAble() ? "开启" : "关闭").append("\n");
sb.append("* GPS状态:").append(getGPSStatusString(location.getLocationQualityReport().getGPSStatus())).append("\n");
sb.append("* GPS星数:").append(location.getLocationQualityReport().getGPSSatellites()).append("\n");
sb.append("* 网络类型:" + location.getLocationQualityReport().getNetworkType()).append("\n");
sb.append("* 网络耗时:" + location.getLocationQualityReport().getNetUseTime()).append("\n");
sb.append("****************").append("\n");
//定位之后的回调时间
sb.append("回调时间: " + ToolUtils.formatUTC(System.currentTimeMillis(), "yyyy-MM-dd HH:mm:ss") + "\n");
//解析定位结果,
String result = sb.toString();
//TODO 定位结果展示
} else {
ToastUtil.show("定位失败,请确认设备定位环境");
}
}
};
/**
* 获取GPS状态的字符串
*
* @param statusCode GPS状态码
* @return
*/
private String getGPSStatusString(int statusCode) {
String str = "";
switch (statusCode) {
case AMapLocationQualityReport.GPS_STATUS_OK:
str = "GPS状态正常";
break;
case AMapLocationQualityReport.GPS_STATUS_NOGPSPROVIDER:
str = "手机中没有GPS Provider,无法进行GPS定位";
break;
case AMapLocationQualityReport.GPS_STATUS_OFF:
str = "GPS关闭,建议开启GPS,提高定位质量";
break;
case AMapLocationQualityReport.GPS_STATUS_MODE_SAVING:
str = "选择的定位模式中不包含GPS定位,建议选择包含GPS定位的模式,提高定位质量";
break;
case AMapLocationQualityReport.GPS_STATUS_NOGPSPERMISSION:
str = "没有GPS定位权限,建议开启gps定位权限";
break;
}
return str;
}
/**
* 开始定位
*
* @author hongming.wang
* @since 2.8.0
*/
private void startLocation() {
try {
if (locationClient != null) {
// 启动定位
locationClient.startLocation();
}
} catch (Exception e) {
e.printStackTrace();
}
}
/**
* 停止定位
*
* @author hongming.wang
* @since 2.8.0
*/
private void stopLocation() {
try {
if (locationClient != null) {
// 停止定位
locationClient.stopLocation();
}
} catch (Exception e) {
e.printStackTrace();
}
}
/**
* 销毁定位
*
* @author hongming.wang
* @since 2.8.0
*/
private void destroyLocation() {
if (null != locationClient) {
/**
* 如果AMapLocationClient是在当前Activity实例化的,
* 在Activity的onDestroy中一定要执行AMapLocationClient的onDestroy
*/
locationClient.onDestroy();
locationClient = null;
locationOption = null;
}
}
}