• Unity Android 接入高德定位SDK


    Unity版本 2020

    第一步首先去高德开放平台注册账号然后申请Key

    高德开放平台 | 高德地图API

    注册好账号之后 点击控制台 然后选择管理key 新建一个应用 

     选择添加  然后创建一个key 选择Android平台  SHA1码获取方式参考我的另一篇博客

    Android 签名文件.keystore转换.jks_小张不爱写代码的博客-CSDN博客_keystore转jks

     然后直接创建key即可

    创建完毕之后去下载定位SDK

    相关下载-Android 定位SDK | 高德地图API

    打开Unity创建一个Unity项目

    并创建如下脚本

    1. using UnityEngine;
    2. using UnityEngine.UI;
    3. public class Sdk : MonoBehaviour
    4. {
    5. public Text t;
    6. private AndroidJavaClass jc;
    7. private AndroidJavaObject jo;
    8. private void Awake()
    9. {
    10. if (Application.platform == RuntimePlatform.Android)
    11. {
    12. jc = new AndroidJavaClass("com.unity3d.player.UnityPlayer");
    13. jo = jc.GetStatic("currentActivity");
    14. }
    15. }
    16. public void StartAMap()
    17. {
    18. switch (Application.platform)
    19. {
    20. case RuntimePlatform.Android:
    21. jo.Call("StartAMap");
    22. break;
    23. default:
    24. StopAMapCallBack();
    25. break;
    26. }
    27. }
    28. public void StopAMap()
    29. {
    30. switch (Application.platform)
    31. {
    32. case RuntimePlatform.Android:
    33. jo.Call("StopAMap");
    34. break;
    35. default:
    36. break;
    37. }
    38. }
    39. public void StartAMapCallBack(string msg)
    40. {
    41. t.text = msg;
    42. }
    43. public void StopAMapCallBack()
    44. {
    45. t.text = "1111";
    46. }
    47. }

    然后创建如下结构 Button点击事件分别指向StartAMap和StopAMap即可

     然后直接出包导出AndroidStudio项目

    导出完成之后 现在AS工程中的Libs文件夹中放入解压的高德SDK jar包

     导入之后在如图位置写入如下代码

    implementation fileTree(dir: 'libs', include: ['*.jar'])

    在AndroidManifest.xml上面写入如下权限

    1. <uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
    2. <uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
    3. <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
    4. <uses-permission android:name="android.permission.ACCESS_WIFI_STATE" />
    5. <uses-permission android:name="android.permission.CHANGE_WIFI_STATE" />
    6. <uses-permission android:name="android.permission.INTERNET" />
    7. <uses-permission android:name="android.permission.READ_PHONE_STATE" />
    8. <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"
    9. tools:ignore="ScopedStorage" />
    10. <uses-permission android:name="android.permission.ACCESS_LOCATION_EXTRA_COMMANDS" />
    11. <uses-permission android:name="android.permission.FOREGROUND_SERVICE"/>
    12. <uses-permission android:name="android.permission.ACCESS_BACKGROUND_LOCATION"/>
    13. <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"
    14. tools:ignore="ScopedStorage" />

    在AndroidManifest.xml的Application节点内写入如下内容

    1. <meta-data
    2. android:name="com.amap.api.v2.apikey"
    3. android:value="6c8580a005a4f6d162f59e17ace1fbca" />
    4. <service android:name="com.amap.api.location.APSService" />

    打开UnityPlayerActivity脚本 在末尾加入如下代码 用以动态申请权限

    1. //权限管理
    2. private static final String[] PERMISSIONS =
    3. {
    4. Manifest.permission.INTERNET,
    5. Manifest.permission.ACCESS_COARSE_LOCATION,
    6. Manifest.permission.ACCESS_FINE_LOCATION,
    7. Manifest.permission.ACCESS_NETWORK_STATE,
    8. Manifest.permission.ACCESS_WIFI_STATE,
    9. Manifest.permission.CHANGE_WIFI_STATE,
    10. Manifest.permission.READ_PHONE_STATE,
    11. Manifest.permission.ACCESS_LOCATION_EXTRA_COMMANDS,
    12. Manifest.permission.BLUETOOTH,
    13. Manifest.permission.BLUETOOTH_ADMIN,
    14. };
    15. private static final int PERMISSIONS_CODE = 1;
    16. //申请权限
    17. private void requestPermission()
    18. {
    19. // 当API大于 23 时,才动态申请权限
    20. if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M)
    21. {
    22. ActivityCompat.requestPermissions(this,PERMISSIONS,PERMISSIONS_CODE);
    23. }
    24. }
    25. @Override
    26. public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults)
    27. {
    28. super.onRequestPermissionsResult(requestCode, permissions, grantResults);
    29. switch (requestCode) {
    30. case PERMISSIONS_CODE:
    31. //权限请求失败
    32. for(int i=0;i1;i++)
    33. {
    34. if(grantResults[i] != PackageManager.PERMISSION_GRANTED)
    35. {
    36. Log.e("Permissions", "onRequestPermissionsResult: 权限请求失败:"+permissions[i].toString());
    37. }
    38. }
    39. break;
    40. }
    41. }

    然后在UnityPlayerActivity脚本中的OnCreate方法中调用requestPermission方法来申请权限

    新创建一个类 写入如下代码

    1. package com.unity3d.player.GaoDe;
    2. import android.util.Log;
    3. import com.amap.api.location.AMapLocation;
    4. import com.amap.api.location.AMapLocationClient;
    5. import com.amap.api.location.AMapLocationClientOption;
    6. import com.amap.api.location.AMapLocationListener;
    7. import com.unity3d.player.UnityPlayer;
    8. import com.unity3d.player.UnityPlayerActivity;
    9. public class GaoDe {
    10. private static GaoDe instance = null;
    11. private static UnityPlayerActivity ccActivity = null;
    12. public static GaoDe Ins() {
    13. if (instance == null) {
    14. instance = new GaoDe();
    15. }
    16. return instance;
    17. }
    18. //声明AMapLocationClient类对象
    19. public AMapLocationClient mLocationClient = null;
    20. //声明定位回调监听器
    21. public AMapLocationListener mLocationListener = null;
    22. //声明AMapLocationClientOption对象
    23. public AMapLocationClientOption mLocationOption = null;
    24. public void Init(UnityPlayerActivity act){
    25. ccActivity = act;
    26. //初始化定位
    27. AMapLocationClient.updatePrivacyShow(ccActivity, true, true);
    28. AMapLocationClient.updatePrivacyAgree(ccActivity, true);
    29. try {
    30. //初始化定位
    31. mLocationClient = new AMapLocationClient(ccActivity);
    32. }catch (Exception e){
    33. e.printStackTrace();
    34. Log.d("Unity", "UnityPlayerActivity Init error: "+e);
    35. }
    36. //初始化AMapLocationClientOption对象
    37. mLocationOption = new AMapLocationClientOption();
    38. //设置定位监听
    39. mLocationListener = new AMapLocationListener() {
    40. @Override
    41. public void onLocationChanged(AMapLocation aMapLocation) {
    42. OnLocationChanged(aMapLocation);
    43. }
    44. };
    45. //设置定位回调监听
    46. mLocationClient.setLocationListener(mLocationListener);
    47. //设置定位模式为AMapLocationMode.Hight_Accuracy,高精度模式。
    48. mLocationOption.setLocationMode(AMapLocationClientOption.AMapLocationMode.Hight_Accuracy);
    49. //设置定位间隔,单位毫秒,默认为2000ms,最低1000ms。
    50. mLocationOption.setInterval(1000);
    51. //单位是毫秒,默认30000毫秒,建议超时时间不要低于8000毫秒。
    52. mLocationOption.setHttpTimeOut(8000);
    53. //优先使用gps
    54. mLocationOption.setGpsFirst(true);
    55. Log.d("Unity", "高德: InitOK");
    56. }
    57. private void OnLocationChanged(AMapLocation amapLocation){
    58. Log.d("Unity", "高德: OnLocationChanged");
    59. if(amapLocation == null) {
    60. amapLocation = mLocationClient.getLastKnownLocation();
    61. }
    62. if (amapLocation != null)
    63. {
    64. Log.e("AmapError", "amapLocation~=null start:");
    65. mLocationClient.stopLocation();
    66. int errorCode = amapLocation.getErrorCode();
    67. Log.e("AmapError", "amapLocation~=null errorCode:"+errorCode);
    68. //定位成功回调信息,设置相关消息
    69. int locationType = amapLocation.getLocationType();//获取当前定位结果来源,如网络定位结果,详见定位类型表
    70. double longitude = amapLocation.getLongitude();//获取经度
    71. double latitude = amapLocation.getLatitude();//获取纬度
    72. String address = amapLocation.getAddress();
    73. String country = amapLocation.getCountry();
    74. String province = amapLocation.getProvince();
    75. String city = amapLocation.getCity();
    76. String district = amapLocation.getDistrict();
    77. String re = errorCode + "|" + longitude + "|" + latitude + "|" + address +"|"+ country +"|"+ province +"|"+ city +"|"+ district+" errrrrrrr";
    78. Log.e("AmapError", "amapLocation~=null 位置信息:"+re);
    79. UnityPlayer.UnitySendMessage("Main Camera", "StartAMapCallBack", re);
    80. }
    81. else
    82. {
    83. //定位失败时,可通过ErrCode(错误码)信息来确定失败的原因,errInfo是错误信息,详见错误码表。
    84. Log.e("AmapError","location Error, ErrCode:"
    85. + amapLocation.getErrorCode() + ", errInfo:"
    86. + amapLocation.getErrorInfo());
    87. mLocationClient.stopLocation();
    88. UnityPlayer.UnitySendMessage("Main Camera", "StartAMapCallBack", "0|0|0");
    89. }
    90. }
    91. //开始定位
    92. public void StartAMap()
    93. {
    94. if(mLocationClient!=null) {
    95. //给定位客户端对象设置定位参数
    96. mLocationClient.setLocationOption(mLocationOption);
    97. //启动定位
    98. mLocationClient.startLocation();
    99. Log.d("Unity", "StartAMap: 已开启定位");
    100. }
    101. else{
    102. if(ccActivity!=null)
    103. Init(ccActivity);
    104. else
    105. Log.d("Unity", "StartAMap: mLocationClient是空的");
    106. }
    107. }
    108. //停止定位
    109. public void StopAMap()
    110. {
    111. if(mLocationClient!=null) {
    112. mLocationClient.stopLocation();
    113. mLocationClient.onDestroy();
    114. mLocationOption = null;
    115. mLocationListener = null;
    116. UnityPlayer.UnitySendMessage("Main Camera", "StopAMapCallBack","");
    117. }
    118. }
    119. }

    打开UnityPlayerActivity脚本在最末尾加入如下代码 在OnCreate申请权限下面调用

    GaoDe.Ins().Init(this); 用以初始化SDK

    1. public void StartAMap(){
    2. GaoDe.Ins().StartAMap();
    3. }
    4. public void StopAMap()
    5. {
    6. GaoDe.Ins().StopAMap();
    7. }

    然后就可以打包apk进行测试了  出现如下结果即可

  • 相关阅读:
    渗透测试漏洞挖掘技巧
    手写消息队列(基于RabbitMQ)
    【云原生K8S】Kubernetes之Service
    百择电商:抖音超级福袋怎么使用?
    关于Linux下Redis自动化部署的一些笔记
    opengl环境配置,使用glew和glfw(C++)
    【UniApp】-uni-app-扩展组件
    一文了解国自然热点“超级增强子”的重要标记——H3K27ac
    【sql注入】sql关卡1~4
    基于Vite+React构建在线Excel
  • 原文地址:https://blog.csdn.net/qq_41973169/article/details/127579931