赞
踩
1 依赖
- //定位
- implementation 'com.amap.api:location:latest.integration'
-
-
- //3D地图
- implementation 'com.amap.api:3dmap:latest.integration'
-
- //地图搜索
- implementation 'com.amap.api:search:latest.integration'
2 定位activity信息
- public class SelectAreaActivity extends BaseActivity implements EasyPermissions.PermissionCallbacks {
-
-
- private AMapLocation mAMapLocation;
-
- @Override
- protected void onCreate(@Nullable Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- setContentView(R.layout.activity_selectarea);
- ButterKnife.bind(this);
-
-
- requestLocationPermissions();
-
- }
-
- @Override
- public void onPermissionsGranted(int requestCode, List<String> perms) {
- getLocation();
- }
-
- private void requestLocationPermissions() {
- String[] perms = {Manifest.permission.ACCESS_COARSE_LOCATION,
- Manifest.permission.ACCESS_FINE_LOCATION};
- if (EasyPermissions.hasPermissions(this, perms)) {
- getLocation();
- } else {
- EasyPermissions.requestPermissions(this, "请求获取您的位置", Constans.GET_LOCATION_PERMISSION, perms);
- }
- }
-
-
- //声明定位回调监听器
- //声明AMapLocationClient类对象
- public AMapLocationClient mLocationClient = null;
- //声明AMapLocationClientOption对象
- public AMapLocationClientOption mLocationOption = null;
- public AMapLocationListener mLocationListener = new AMapLocationListener() {
- @Override
- public void onLocationChanged(AMapLocation aMapLocation) {
- if (aMapLocation != null) {
- if (aMapLocation.getErrorCode() == 0) {
- //获取纬度
- App.mMyLatitude = aMapLocation.getLatitude();
- //获取经度
- App.mMyLongitude = aMapLocation.getLongitude();
- App.mAddress = aMapLocation.getPoiName();
- mAMapLocation = aMapLocation;
- tvNowlocation.setText(aMapLocation.getAddress());
- nowadCode = mAMapLocation.getAdCode() + mAMapLocation.getCityCode();
- StringBuilder stringBuilder = new StringBuilder(nowadCode);
- for (int i = 0; i < 12 - nowadCode.length(); i++) {
- stringBuilder.append("0");
- }
- nowadCode = stringBuilder.toString();
-
- } else {
- Log.e("AmapError", "location Error, ErrCode:"
- + aMapLocation.getErrorCode() + ", errInfo:"
- + aMapLocation.getErrorInfo());
- }
- }
- }
- };
-
- public void getLocation() {
- //初始化定位
- mLocationClient = new AMapLocationClient(getApplicationContext());
- //设置定位回调监听
- mLocationClient.setLocationListener(mLocationListener);
- //初始化AMapLocationClientOption对象
- mLocationOption = new AMapLocationClientOption();
- //设置定位模式为AMapLocationMode.Hight_Accuracy,高精度模式。
- // mLocationOption.setLocationMode(AMapLocationClientOption.AMapLocationMode
- // .Hight_Accuracy);
- //设置定位模式为AMapLocationMode.Battery_Saving,低功耗模式。
- mLocationOption.setLocationMode(AMapLocationClientOption.AMapLocationMode.Battery_Saving);
- //获取一次定位结果:
- //该方法默认为false。
- mLocationOption.setOnceLocation(true);
- //关闭缓存机制
- mLocationOption.setLocationCacheEnable(false);
- //给定位客户端对象设置定位参数
- mLocationClient.setLocationOption(mLocationOption);
- //启动定位
- mLocationClient.startLocation();
- }
-
- @Override
- public void onPermissionsDenied(int requestCode, List<String> perms) {
-
- }
-
-
- }
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。