zoukankan      html  css  js  c++  java
  • Android调用高德地图API实现定位

    以下内容开发环境为:Android Studio+ API 23+AMap_Location_V3.5.0_20170731.jar

    真机测试环境:华为 Android7.0

    要实现高德地图API在android中实现定位需实现以下步骤:

    1. 获取高德地图应用Key及配置Key;
    2. 配置定位权限
    3. 开启定位服务,获取定位数据

    第一:获取高德地图应用Key及配置Key。这个直接根据高德API说明文档配置。

             但是在获取SHA1时,如果需要配置keystore。KeyStore配置方法如下:http://blog.csdn.net/nimasike/article/details/51457229

    第二:配置定位权限。可以从高德地图android的Demo中获取,我给出以下配置:

     <!-- 获取手机号权限 --> 
    <uses-permission android:name="android.permission.READ_PHONE_STATE"/> 
    <!-- 用于进行网络定位 -->
     <uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION"/> 
    <!-- 用于访问GPS定位 -->    
    <uses-permission android:name="android.permission.ACCESS_FINE_LOCATION"/>  
    <!--用于提高GPS定位速度-->
    <uses-permission android:name="android.permission.ACCESS_LOCATION_EXTRA_COMMANDS" />
    <!-- 获取运营商信息,用于支持提供运营商信息相关的接口 -->
    <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"/>  
    <!-- 用于访问wifi网络信息,wifi信息会用于进行网络定位 -->
    <uses-permission android:name="android.permission.ACCESS_WIFI_STATE"/>
    <!-- 这个权限用于获取wifi的获取权限,wifi信息会用来进行网络定位 -->
    <uses-permission android:name="android.permission.CHANGE_WIFI_STATE"/> 
    <uses-permission android:name="android.permission.CHANGE_CONFIGURATION" />
     <!-- 用于访问网络,网络定位需要上网 -->
     <uses-permission android:name="android.permission.INTERNET"/>  
    View Code

      在andorid6.0以后,要求动态获取定位权限。动态获取定位权限如下:

    1)在MyUtils类中自定义方法selfPermissionGranted,校验是否开启权限

     1 /*类MyUtils中自定义权限是否开启方法*/ 
     2 public static boolean selfPermissionGranted(String permission, Context context) {
     3         // For Android < Android M, self permissions are always granted.
     4         boolean result = true;
     5         if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
     6             if (Integer.valueOf(android.os.Build.VERSION.SDK) >= Build.VERSION_CODES.M) {
     7                 // targetSdkVersion >= Android M, we can
     8                 // use Context#checkSelfPermission
     9                 result = context.checkSelfPermission(permission)
    10                         == PackageManager.PERMISSION_GRANTED;
    11             } else {
    12                 // targetSdkVersion < Android M, we have to use PermissionChecker
    13                 result = PermissionChecker.checkSelfPermission(context, permission)
    14                         == PermissionChecker.PERMISSION_GRANTED;
    15             }
    16         }
    17         return result;
    18     }
    View Code

    2)在进入系统第一个Activity获取定位权限

    1  if(!MyUtils.selfPermissionGranted(Manifest.permission.ACCESS_COARSE_LOCATION, getApplicationContext())){
    2             ActivityCompat.requestPermissions(this,
    3                     new String[]{Manifest.permission.ACCESS_COARSE_LOCATION},
    4                     Constant.LOCATION_STATE);
    5 /*Constant.LOCATION_STATE 为自己定义的一个常量,为权限弹窗回调时使用*/
    6         }
     @Override
        public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
            super.onRequestPermissionsResult(requestCode, permissions, grantResults);
            if(grantResults.length<=0){ return;}
            if (requestCode == Constant.LOCATION_STATE){
                if (grantResults[0] == PackageManager.PERMISSION_GRANTED) {
                    initPermission();
                } else if (grantResults[0] == PackageManager.PERMISSION_DENIED) {
                    Toast.makeText(EntryActivity.this, "获取位置权限被禁用", Toast.LENGTH_SHORT).show();
                    finish();
                }
            }
        }

    第三,获取定位服务。定位服务可能是GPS定位,可能是WIFI定位及移动基站定位。定位服务开启时,比较消耗电量,所以在需要定位的地方裁判定位服务是否开启。没有开启则调用系统设置-->高级设置-->定位服务,让用户开启定位服务。定位服务开启后,可根据高德地图Demo实现定位(高德地图API中没有实现调用系统设置定位服务的方法)。

    以下给出定位获取定位类完整代码:

    public class GetLocation  implements AMapLocationListener {
        private AMapLocationClient locationClient = null;
        private AMapLocationClientOption locationOption = null;
        private Context context;
        public Handler mHandler=null; 
    
        public GetLocation(Context context,Handler mHandle) {
            this.context = context;
            this.mHandler=mHandle;
        }
      /*获取定位数据*/
    public void getLocation() { locationClient = new AMapLocationClient(context); locationClient.setLocationListener(this); locationOption = new AMapLocationClientOption(); // 设置定位模式为低功耗模式 locationOption.setLocationMode(AMapLocationClientOption.AMapLocationMode.Battery_Saving); locationOption.setNeedAddress(true); // 设置定位参数 locationClient.setLocationOption(locationOption); // 启动定位 locationClient.startLocation(); } /*校验权限*/ public boolean CheckPermission (){ if (MyUtils.selfPermissionGranted(Manifest.permission.ACCESS_COARSE_LOCATION, context)) { return true; } else { return false;} } /*j校验定位服务是否开启*/ public boolean CheckAPSService(){ LocationManager locationManager = (LocationManager) context.getSystemService(context.LOCATION_SERVICE); // 通过GPS卫星定位,定位级别可以精确到街(通过24颗卫星定位,在室外和空旷的地方定位准确、速度快) boolean gps = locationManager.isProviderEnabled(LocationManager.GPS_PROVIDER); // 通过WLAN或移动网络(3G/2G)确定的位置(也称作AGPS,辅助GPS定位。主要用于在室内或遮盖物(建筑群或茂密的深林等)密集的地方定位) boolean network = locationManager.isProviderEnabled(LocationManager.NETWORK_PROVIDER); if (gps || network) { return true; } return false; } // 定位监听 @Override public void onLocationChanged(AMapLocation loc) { Message msg = new Message(); msg.obj = loc; msg.what = 1000; mHandler.sendMessage(msg); locationClient.stopLocation(); } }

    在Activity中如果没有开启定位服务,开启定位服务的的核心代码如下:

                if(!getLocation.CheckAPSService()){
                    new  AlertDialog.Builder(this).setMessage("请打开GPS或者WIFI开关").setPositiveButton("开启", new DialogInterface.OnClickListener() {
                        @Override
                        public void onClick(DialogInterface dialog, int which) {
                            Intent intent =  new Intent(Settings.ACTION_LOCATION_SOURCE_SETTINGS);//开定系统定位服务设置,需添加 import android.provider.Settings;
                            startActivity(intent);
                        }
                    }).show();
            }
                else {
             //定位
             getLocation.getLocation();
           }
  • 相关阅读:
    Java_zip_多源文件压缩到指定目录下
    Linux:ps -ef命令
    Linux:find命令中
    String.split()与StringUtils.split()的区别
    StringUtils工具类的常用方法
    Java:substring()
    Java:indexof()
    Linux:grep命令
    12、spring工厂+web前台搭建
    11、redis使用ruby实现集群高可用
  • 原文地址:https://www.cnblogs.com/XieDong/p/7724556.html
Copyright © 2011-2022 走看看