Unity版本 2020
第一步首先去高德开放平台注册账号然后申请Key
注册好账号之后 点击控制台 然后选择管理key 新建一个应用
选择添加 然后创建一个key 选择Android平台 SHA1码获取方式参考我的另一篇博客
Android 签名文件.keystore转换.jks_小张不爱写代码的博客-CSDN博客_keystore转jks
然后直接创建key即可
创建完毕之后去下载定位SDK
打开Unity创建一个Unity项目
并创建如下脚本
- using UnityEngine;
- using UnityEngine.UI;
-
- public class Sdk : MonoBehaviour
- {
- public Text t;
- private AndroidJavaClass jc;
- private AndroidJavaObject jo;
- private void Awake()
- {
- if (Application.platform == RuntimePlatform.Android)
- {
- jc = new AndroidJavaClass("com.unity3d.player.UnityPlayer");
- jo = jc.GetStatic
("currentActivity"); - }
- }
-
- public void StartAMap()
- {
- switch (Application.platform)
- {
- case RuntimePlatform.Android:
- jo.Call("StartAMap");
- break;
- default:
- StopAMapCallBack();
- break;
- }
- }
-
- public void StopAMap()
- {
- switch (Application.platform)
- {
- case RuntimePlatform.Android:
- jo.Call("StopAMap");
- break;
- default:
- break;
- }
- }
- public void StartAMapCallBack(string msg)
- {
- t.text = msg;
- }
-
- public void StopAMapCallBack()
- {
- t.text = "1111";
- }
- }
然后创建如下结构 Button点击事件分别指向StartAMap和StopAMap即可
然后直接出包导出AndroidStudio项目
导出完成之后 现在AS工程中的Libs文件夹中放入解压的高德SDK jar包
导入之后在如图位置写入如下代码
implementation fileTree(dir: 'libs', include: ['*.jar'])
在AndroidManifest.xml上面写入如下权限
-
- <uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
-
- <uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
-
- <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
-
- <uses-permission android:name="android.permission.ACCESS_WIFI_STATE" />
-
- <uses-permission android:name="android.permission.CHANGE_WIFI_STATE" />
-
- <uses-permission android:name="android.permission.INTERNET" />
-
- <uses-permission android:name="android.permission.READ_PHONE_STATE" />
-
- <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"
- tools:ignore="ScopedStorage" />
-
- <uses-permission android:name="android.permission.ACCESS_LOCATION_EXTRA_COMMANDS" />
-
- <uses-permission android:name="android.permission.FOREGROUND_SERVICE"/>
-
- <uses-permission android:name="android.permission.ACCESS_BACKGROUND_LOCATION"/>
-
- <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"
- tools:ignore="ScopedStorage" />
在AndroidManifest.xml的Application节点内写入如下内容
- <meta-data
- android:name="com.amap.api.v2.apikey"
- android:value="6c8580a005a4f6d162f59e17ace1fbca" />
- <service android:name="com.amap.api.location.APSService" />
打开UnityPlayerActivity脚本 在末尾加入如下代码 用以动态申请权限
- //权限管理
- private static final String[] PERMISSIONS =
- {
- Manifest.permission.INTERNET,
- Manifest.permission.ACCESS_COARSE_LOCATION,
- Manifest.permission.ACCESS_FINE_LOCATION,
- Manifest.permission.ACCESS_NETWORK_STATE,
- Manifest.permission.ACCESS_WIFI_STATE,
- Manifest.permission.CHANGE_WIFI_STATE,
- Manifest.permission.READ_PHONE_STATE,
- Manifest.permission.ACCESS_LOCATION_EXTRA_COMMANDS,
- Manifest.permission.BLUETOOTH,
- Manifest.permission.BLUETOOTH_ADMIN,
- };
- private static final int PERMISSIONS_CODE = 1;
- //申请权限
- private void requestPermission()
- {
- // 当API大于 23 时,才动态申请权限
- if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M)
- {
- ActivityCompat.requestPermissions(this,PERMISSIONS,PERMISSIONS_CODE);
- }
- }
-
- @Override
- public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults)
- {
- super.onRequestPermissionsResult(requestCode, permissions, grantResults);
- switch (requestCode) {
- case PERMISSIONS_CODE:
- //权限请求失败
- for(int i=0;i
1;i++) - {
- if(grantResults[i] != PackageManager.PERMISSION_GRANTED)
- {
- Log.e("Permissions", "onRequestPermissionsResult: 权限请求失败:"+permissions[i].toString());
- }
- }
- break;
- }
- }
然后在UnityPlayerActivity脚本中的OnCreate方法中调用requestPermission方法来申请权限
新创建一个类 写入如下代码
- package com.unity3d.player.GaoDe;
- import android.util.Log;
- import com.amap.api.location.AMapLocation;
- import com.amap.api.location.AMapLocationClient;
- import com.amap.api.location.AMapLocationClientOption;
- import com.amap.api.location.AMapLocationListener;
- import com.unity3d.player.UnityPlayer;
- import com.unity3d.player.UnityPlayerActivity;
-
- public class GaoDe {
- private static GaoDe instance = null;
- private static UnityPlayerActivity ccActivity = null;
-
- public static GaoDe Ins() {
- if (instance == null) {
- instance = new GaoDe();
- }
- return instance;
- }
- //声明AMapLocationClient类对象
- public AMapLocationClient mLocationClient = null;
- //声明定位回调监听器
- public AMapLocationListener mLocationListener = null;
- //声明AMapLocationClientOption对象
- public AMapLocationClientOption mLocationOption = null;
-
- public void Init(UnityPlayerActivity act){
- ccActivity = act;
- //初始化定位
- AMapLocationClient.updatePrivacyShow(ccActivity, true, true);
- AMapLocationClient.updatePrivacyAgree(ccActivity, true);
- try {
- //初始化定位
- mLocationClient = new AMapLocationClient(ccActivity);
- }catch (Exception e){
- e.printStackTrace();
- Log.d("Unity", "UnityPlayerActivity Init error: "+e);
- }
- //初始化AMapLocationClientOption对象
- mLocationOption = new AMapLocationClientOption();
- //设置定位监听
- mLocationListener = new AMapLocationListener() {
- @Override
- public void onLocationChanged(AMapLocation aMapLocation) {
- OnLocationChanged(aMapLocation);
- }
- };
- //设置定位回调监听
- mLocationClient.setLocationListener(mLocationListener);
- //设置定位模式为AMapLocationMode.Hight_Accuracy,高精度模式。
- mLocationOption.setLocationMode(AMapLocationClientOption.AMapLocationMode.Hight_Accuracy);
- //设置定位间隔,单位毫秒,默认为2000ms,最低1000ms。
- mLocationOption.setInterval(1000);
- //单位是毫秒,默认30000毫秒,建议超时时间不要低于8000毫秒。
- mLocationOption.setHttpTimeOut(8000);
- //优先使用gps
- mLocationOption.setGpsFirst(true);
- Log.d("Unity", "高德: InitOK");
- }
-
- private void OnLocationChanged(AMapLocation amapLocation){
- Log.d("Unity", "高德: OnLocationChanged");
- if(amapLocation == null) {
- amapLocation = mLocationClient.getLastKnownLocation();
- }
- if (amapLocation != null)
- {
- Log.e("AmapError", "amapLocation~=null start:");
- mLocationClient.stopLocation();
- int errorCode = amapLocation.getErrorCode();
- Log.e("AmapError", "amapLocation~=null errorCode:"+errorCode);
- //定位成功回调信息,设置相关消息
- int locationType = amapLocation.getLocationType();//获取当前定位结果来源,如网络定位结果,详见定位类型表
- double longitude = amapLocation.getLongitude();//获取经度
- double latitude = amapLocation.getLatitude();//获取纬度
- String address = amapLocation.getAddress();
- String country = amapLocation.getCountry();
- String province = amapLocation.getProvince();
- String city = amapLocation.getCity();
- String district = amapLocation.getDistrict();
- String re = errorCode + "|" + longitude + "|" + latitude + "|" + address +"|"+ country +"|"+ province +"|"+ city +"|"+ district+" errrrrrrr";
- Log.e("AmapError", "amapLocation~=null 位置信息:"+re);
- UnityPlayer.UnitySendMessage("Main Camera", "StartAMapCallBack", re);
- }
- else
- {
- //定位失败时,可通过ErrCode(错误码)信息来确定失败的原因,errInfo是错误信息,详见错误码表。
- Log.e("AmapError","location Error, ErrCode:"
- + amapLocation.getErrorCode() + ", errInfo:"
- + amapLocation.getErrorInfo());
- mLocationClient.stopLocation();
- UnityPlayer.UnitySendMessage("Main Camera", "StartAMapCallBack", "0|0|0");
- }
- }
-
- //开始定位
- public void StartAMap()
- {
- if(mLocationClient!=null) {
- //给定位客户端对象设置定位参数
- mLocationClient.setLocationOption(mLocationOption);
- //启动定位
- mLocationClient.startLocation();
- Log.d("Unity", "StartAMap: 已开启定位");
- }
- else{
- if(ccActivity!=null)
- Init(ccActivity);
- else
- Log.d("Unity", "StartAMap: mLocationClient是空的");
- }
- }
-
- //停止定位
- public void StopAMap()
- {
- if(mLocationClient!=null) {
- mLocationClient.stopLocation();
- mLocationClient.onDestroy();
- mLocationOption = null;
- mLocationListener = null;
- UnityPlayer.UnitySendMessage("Main Camera", "StopAMapCallBack","");
- }
- }
- }
打开UnityPlayerActivity脚本在最末尾加入如下代码 在OnCreate申请权限下面调用
GaoDe.Ins().Init(this); 用以初始化SDK
- public void StartAMap(){
- GaoDe.Ins().StartAMap();
- }
-
- public void StopAMap()
- {
- GaoDe.Ins().StopAMap();
- }
然后就可以打包apk进行测试了 出现如下结果即可