相信技术的力量

Android集成高德地图

Mac获取SHA1安全码

发布版安全码

1.使用Android创建jks文件

2.在项目根目录下执行 keytool -v -list -keystore {jks文件路径}

例如:
keytool -v -list -keystore /Users/crocutax/Documents/svn/190306_微时代/03_dev/android/app/weishidai.jks

控制台会输出自己想要的SHA1信息

3.开发环境使用生产环境的签名配置

signingConfigs {
    release {
        keyAlias 'weishidai'
        keyPassword 'crocutax'
        storeFile file('weishidai.jks')
        storePassword 'crocutax'
    }
}

buildTypes {
    release {
        minifyEnabled false
        proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
    }
    debug {
        signingConfig signingConfigs.release
    }
}

正常集成高德SDK

创建应用

在高德控制台根据包名SHA1生成应用,获取签名(只需要填入发布版SHA1即可)

gradle集成

3D地图

compile 'com.amap.api:3dmap:latest.integration'

2D地图

compile 'com.amap.api:map2d:latest.integration'

导航

compile 'com.amap.api:navi-3dmap:latest.integration'

搜索

compile 'com.amap.api:search:latest.integration'

定位

compile 'com.amap.api:location:latest.integration'

很详细
https://blog.csdn.net/u011068996/article/details/49741543

添加权限

<!--用于进行网络定位-->
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION"></uses-permission>
<!--用于访问GPS定位-->
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION"></uses-permission>
<!--用于获取运营商信息,用于支持提供运营商信息相关的接口-->
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"></uses-permission>
<!--用于访问wifi网络信息,wifi信息会用于进行网络定位-->
<uses-permission android:name="android.permission.ACCESS_WIFI_STATE"></uses-permission>
<!--用于获取wifi的获取权限,wifi信息会用来进行网络定位-->
<uses-permission android:name="android.permission.CHANGE_WIFI_STATE"></uses-permission>
<!--用于访问网络,网络定位需要上网-->
<uses-permission android:name="android.permission.INTERNET"></uses-permission>
<!--用于读取手机当前的状态-->
<uses-permission android:name="android.permission.READ_PHONE_STATE"></uses-permission>
<!--用于写入缓存数据到扩展存储卡-->
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"></uses-permission>
<!--用于申请调用A-GPS模块-->
<uses-permission android:name="android.permission.ACCESS_LOCATION_EXTRA_COMMANDS"></uses-permission>
<!--用于申请获取蓝牙信息进行室内定位-->
<uses-permission android:name="android.permission.BLUETOOTH"></uses-permission>
<uses-permission android:name="android.permission.BLUETOOTH_ADMIN"></uses-permission>

Application.xml中配置在高德控制台生成的key

<application
         android:icon="@drawable/icon"
         android:label="@string/app_name" >
         <meta-data
            android:name="com.amap.api.v2.apikey"
            android:value="请输入您的用户Key"/>
            ……
</application>

定位代码

//声明AMapLocationClient类对象
public AMapLocationClient mLocationClient = null;
//声明mLocationOption对象
private AMapLocationClientOption mLocationOption = null;

private void initLocation() {
    KLog.i("初始化高德定位");
    //初始化定位
    mLocationClient = new AMapLocationClient(this);
    //初始化定位参数
    mLocationOption = new AMapLocationClientOption();
    //设置定位模式为高精度模式,Battery_Saving为低功耗模式,Device_Sensors是仅设备模式
    mLocationOption.setLocationMode(AMapLocationClientOption.AMapLocationMode.Hight_Accuracy);
    //设置是否返回地址信息(默认返回地址信息)
    mLocationOption.setNeedAddress(true);
    //设置是否只定位一次,默认为false
    mLocationOption.setOnceLocation(true);
    //设置是否强制刷新WIFI,默认为强制刷新
    mLocationOption.setWifiActiveScan(true);
    //设置是否允许模拟位置,默认为false,不允许模拟位置
    mLocationOption.setMockEnable(false);
    //设置定位间隔,单位毫秒,默认为2000ms
    //mLocationOption.setInterval(2000);
    //给定位客户端对象设置定位参数
    mLocationClient.setLocationOption(mLocationOption);
    //设置定位回调监听
    mLocationClient.setLocationListener(new AMapLocationListener() {
        @Override
        public void onLocationChanged(final AMapLocation aMapLocation) {
            //定位成功回调
            if (aMapLocation != null) {
                KLog.i("onLocationChanged!..." + aMapLocation.toString());
                if (aMapLocation.getErrorCode() == 0) {
                     KLog.i("定位成功了:纬度:"+aMapLocation.getLatitude()+"..经度:"+aMapLocation.getLongitude()+"..省:"+aMapLocation.getProvince()+"..市:"+aMapLocation.getCity()+"..区:"+aMapLocation.getDistrict()+"..地址:"+aMapLocation.getAddress());
                } else {
                    //显示错误信息ErrCode是错误码,errInfo是错误信息,详见错误码表。
                    KLog.i("location Error, ErrCode:"
                            + aMapLocation.getErrorCode() + ", errInfo:"
                            + aMapLocation.getErrorInfo());
                }
            }
        }
    });
    mLocationClient.startLocation();
}

鉴权失败-INVALID_USER_SCODE

错误信息

I/authErrLog: ================================================================================
I/authErrLog:                                    鉴权错误信息                                  
I/authErrLog: ================================================================================
I/authErrLog: |SHA1Package:DD:C2:7E:73:C4:61:34:70:7E:F4:7D:27:1C:21:21:87:A2:C3:16:3A:com.lo|
I/authErrLog: |ngke.errand                                                                   |
I/authErrLog: |key:49f5ade8c4cc9f068399a1f7900a8ab0                                          |
I/authErrLog: |csid:9f030aa8c712411facefd2cf5319e25e                                         |
I/authErrLog: |gsid:fb81dd1920dfb990828042f228062c95                                         |
I/authErrLog: |json:{"info":"INVALID_USER_SCODE","infocode":"10008","status":"0","sec_code_de|
I/authErrLog: |bug":"d41d8cd98f00b204e9800998ecf8427e","key":"49f5ade8c4cc9f068399a1f7900a8ab|
I/authErrLog: |0","sec_code":"3c9ca42f23abd2cc15b9837283f617c0"}                             |
I/authErrLog:                                                                                
I/authErrLog: 请在高德开放平台官网中搜索"INVALID_USER_SCODE"相关内容进行解决
I/authErrLog: ================================================================================

解决方案

https://lbs.amap.com/faq/top/hot-questions/253/?wd=INVALID_USER_SCODE&cateId=&page=&detail=true

获取本机SHA1的方法,通过此方式,可以获取debugrelease的SHA1,设置高德后台。

/**
 * 获取本机SHA1的方法,参数为上下文
 */
public String getSHA1(Context context) {
    try {
        PackageInfo info = context.getPackageManager().getPackageInfo(
                context.getPackageName(), PackageManager.GET_SIGNATURES);
        byte[] cert = info.signatures[0].toByteArray();
        MessageDigest md = MessageDigest.getInstance("SHA1");
        byte[] publicKey = md.digest(cert);
        StringBuffer hexString = new StringBuffer();
        for (int i = 0; i < publicKey.length; i++) {
            String appendString = Integer.toHexString(0xFF & publicKey[i])
                    .toUpperCase(Locale.US);
            if (appendString.length() == 1)
                hexString.append("0");
            hexString.append(appendString);
            hexString.append(":");
        }
        String result = hexString.toString();
        return result.substring(0, result.length()-1);
    } catch (PackageManager.NameNotFoundException e) {
        e.printStackTrace();
    } catch (NoSuchAlgorithmException e) {
        e.printStackTrace();
    }
    return null;
}

⬆️