作者:hhxsv5 | 来源:互联网 | 2024-10-17 14:04
高德定位SDK_高德地图api使用教程1.LocationManagerProxy获取当前Context创建一个LocationManagerProxy变量mAMapLocMana
1.LocationManagerProxy 获取当前Context 创建一个LocationManagerProxy 变量 mAMapLocManager = LocationManagerProxy.getInstance(this);
2.mAMapLocManager.requestLocationUpdates(LocationProviderProxy.AMapNetwork, 5000, 10, this); //设定 精度 5000m 监听器为当前Activity 所以当前Activiy需要继承AMapLocationListener,
1 private Handler handler = new Handler();
2 private Runnable stop = new Runnable() {
3
4 @Override
5 public void run() {
6 if (aMapLocation == null) {
7 Toast.makeText(getApplicationContext(), "12秒内还没有定位成功,停止定位", Toast.LENGTH_SHORT).show();
8 stopLocation();// 销毁掉定位
9 }
10 }
11 };
12
13 handler.postDelayed(stop, 12000);
在使用requestLocationUpdates 进行定位的同时,我们必须设定超时时间,使用Runnable和handler来进行超时检验, 和OC的GCD、Block很像。
3.重写onPause() ,onLocationChanged(AMapLocation location) 函数,
在onLocationChanged中
1 if (location != null) {
2 this.aMapLocation = location;// 判断超时机制
3 double geoLat = location.getLatitude();
4 double geoLng = location.getLongitude();
5
6 String cityCode = "";
7 String desc = "";
8 Bundle locBundle = location.getExtras();
9 if (locBundle != null) {
10 cityCode = locBundle.getString("citycode");
11 desc = locBundle.getString("desc");
12 }
13
14 String str = ("定位成功:(" + geoLng + "," + geoLat + ")"
15 + "\n精 度 :" + location.getAccuracy() + "米"
16 + "\n定位方式:" + location.getProvider() + "\n定位时间:"
17 + new Date(location.getTime()).toLocaleString() + "\n城市编码:"
18 + cityCode + "\n位置描述:" + desc + "\n省:"
19 + location.getProvince() + "\n市:" + location.getCity()
20 + "\n区(县):" + location.getDistrict() + "\n区域编码:" + location
21 .getAdCode());
22 tv_address.setText(str);
23 }
这样就可以解析出地址了。