当前位置:   article > 正文

Amap定位(高德)_com.amap.api:location

com.amap.api:location

1 依赖

  1. //定位
  2. implementation 'com.amap.api:location:latest.integration'
  3. //3D地图
  4. implementation 'com.amap.api:3dmap:latest.integration'
  5. //地图搜索
  6. implementation 'com.amap.api:search:latest.integration'

2 定位activity信息

  1. public class SelectAreaActivity extends BaseActivity implements EasyPermissions.PermissionCallbacks {
  2. private AMapLocation mAMapLocation;
  3. @Override
  4. protected void onCreate(@Nullable Bundle savedInstanceState) {
  5. super.onCreate(savedInstanceState);
  6. setContentView(R.layout.activity_selectarea);
  7. ButterKnife.bind(this);
  8. requestLocationPermissions();
  9. }
  10. @Override
  11. public void onPermissionsGranted(int requestCode, List<String> perms) {
  12. getLocation();
  13. }
  14. private void requestLocationPermissions() {
  15. String[] perms = {Manifest.permission.ACCESS_COARSE_LOCATION,
  16. Manifest.permission.ACCESS_FINE_LOCATION};
  17. if (EasyPermissions.hasPermissions(this, perms)) {
  18. getLocation();
  19. } else {
  20. EasyPermissions.requestPermissions(this, "请求获取您的位置", Constans.GET_LOCATION_PERMISSION, perms);
  21. }
  22. }
  23. //声明定位回调监听器
  24. //声明AMapLocationClient类对象
  25. public AMapLocationClient mLocationClient = null;
  26. //声明AMapLocationClientOption对象
  27. public AMapLocationClientOption mLocationOption = null;
  28. public AMapLocationListener mLocationListener = new AMapLocationListener() {
  29. @Override
  30. public void onLocationChanged(AMapLocation aMapLocation) {
  31. if (aMapLocation != null) {
  32. if (aMapLocation.getErrorCode() == 0) {
  33. //获取纬度
  34. App.mMyLatitude = aMapLocation.getLatitude();
  35. //获取经度
  36. App.mMyLongitude = aMapLocation.getLongitude();
  37. App.mAddress = aMapLocation.getPoiName();
  38. mAMapLocation = aMapLocation;
  39. tvNowlocation.setText(aMapLocation.getAddress());
  40. nowadCode = mAMapLocation.getAdCode() + mAMapLocation.getCityCode();
  41. StringBuilder stringBuilder = new StringBuilder(nowadCode);
  42. for (int i = 0; i < 12 - nowadCode.length(); i++) {
  43. stringBuilder.append("0");
  44. }
  45. nowadCode = stringBuilder.toString();
  46. } else {
  47. Log.e("AmapError", "location Error, ErrCode:"
  48. + aMapLocation.getErrorCode() + ", errInfo:"
  49. + aMapLocation.getErrorInfo());
  50. }
  51. }
  52. }
  53. };
  54. public void getLocation() {
  55. //初始化定位
  56. mLocationClient = new AMapLocationClient(getApplicationContext());
  57. //设置定位回调监听
  58. mLocationClient.setLocationListener(mLocationListener);
  59. //初始化AMapLocationClientOption对象
  60. mLocationOption = new AMapLocationClientOption();
  61. //设置定位模式为AMapLocationMode.Hight_Accuracy,高精度模式。
  62. // mLocationOption.setLocationMode(AMapLocationClientOption.AMapLocationMode
  63. // .Hight_Accuracy);
  64. //设置定位模式为AMapLocationMode.Battery_Saving,低功耗模式。
  65. mLocationOption.setLocationMode(AMapLocationClientOption.AMapLocationMode.Battery_Saving);
  66. //获取一次定位结果:
  67. //该方法默认为false。
  68. mLocationOption.setOnceLocation(true);
  69. //关闭缓存机制
  70. mLocationOption.setLocationCacheEnable(false);
  71. //给定位客户端对象设置定位参数
  72. mLocationClient.setLocationOption(mLocationOption);
  73. //启动定位
  74. mLocationClient.startLocation();
  75. }
  76. @Override
  77. public void onPermissionsDenied(int requestCode, List<String> perms) {
  78. }
  79. }

 

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/我家小花儿/article/detail/580167
推荐阅读
相关标签
  

闽ICP备14008679号