Unity接入高德定位sdk简单三步无需与安卓工程交互

时间:2022-09-29 18:18:11


欢迎加入Unity业内qq交流群:956187480



一:高德定位有效key的获取

参考官方文档地址:​​获取有效key的详细流程​

注意拿到的key确定是正式的,如果只是测试的话就不能运用到生产环境

二: sdk的使用

1.把下载下来的sdk下的jar包放进unity的Plugins<Android<    AMap_Location_V4.2.0_20180809.jar

Unity接入高德定位sdk简单三步无需与安卓工程交互

2.合并配置AndroidManifest 文件,

添加权限:

<?xml versinotallow="1.0" encoding="utf-8" standalnotallow="no"?>
<manifest
xmlns:android="http://schemas.android.com/apk/res/android"
android:installLocatinotallow="preferExternal"
package="自己的包名com.xxx.xxx">
<supports-screens
android:anyDensity="true"
android:largeScreens="true"
android:normalScreens="true"
android:smallScreens="true"
android:xlargeScreens="true"/>

<uses-permission android:name="android.permission.RECORD_AUDIO" />
<uses-permission android:name="android.permission.INTERNET" />
<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_NETWORK_STATE" />
<uses-permission android:name="android.permission.READ_PHONE_STATE" />
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
<uses-permission android:name="android.permission.READ_CONTACTS" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.WRITE_SETTINGS" />
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />

<!--这个权限用于获取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" />

<!-- 不是SDK需要的权限,是示例中的后台唤醒定位需要的权限 -->
<uses-permission android:name="android.permission.WAKE_LOCK" />

<!-- 需要运行时注册的权限 -->
<!--用于进行网络定位-->
<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.WRITE_EXTERNAL_STORAGE" />
<!--读取缓存数据-->
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />

<!--用于读取手机当前的状态-->
<uses-permission android:name="android.permission.READ_PHONE_STATE" />

<!-- 更改设置 -->
<uses-permission android:name="android.permission.WRITE_SETTINGS" />

<!-- 3.2.0版本增加 -->
<uses-permission android:name="android.permission.BLUETOOTH_ADMIN" />
<!-- 3.2.0版本增加-->
<uses-permission android:name="android.permission.BLUETOOTH" />

<application android:banner="@drawable/app_banner"
android:debuggable="false"
android:icnotallow="@drawable/app_icon"
android:isGame="true"
android:label="@string/app_name"
android:theme="@style/UnityThemeSelector">

<!-- 设置key值 -->
<meta-data
android:name="com.amap.api.v2.apikey"
android:value="自己申请的key" />
<!-- 定位需要的服务 -->
<service android:name="com.amap.api.location.APSService" ></service>


<activity android:cnotallow="locale|fontScale|keyboard|keyboardHidden|mcc|mnc|navigation|orientation|screenLayout|screenSize|smallestScreenSize|touchscreen|uiMode" android:label="@string/app_name" android:launchMode="singleTask" android:name="com.unity3d.player.UnityPlayerActivity" android:screenOrientatinotallow="portrait">
</application>
</manifest>

设置从高德拿到的key:

Unity接入高德定位sdk简单三步无需与安卓工程交互

Unity接入高德定位sdk简单三步无需与安卓工程交互

 

三:Unity端代码的使用

两个脚本足以 : 

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class ChangeJavaToC : AndroidJavaProxy
{
public ChangeJavaToC() : base("com.amap.api.location.AMapLocationListener") { }

void onLocationChanged(AndroidJavaObject amapLocation)
{
if (locationChanged != null)
{
locationChanged(amapLocation);
}
}

public delegate void DelegateOnLocationChanged(AndroidJavaObject amap);
public event DelegateOnLocationChanged locationChanged;
}
using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;

public class LocationManager : MonoBehaviour {

public Text txtLocation;
private ChangeJavaToC amap;
private AndroidJavaClass jcu;
private AndroidJavaObject jou;
private AndroidJavaObject mLocationClient;
private AndroidJavaObject mLocationOption;

public void StartLocation()
{
try
{
jcu = new AndroidJavaClass("com.unity3d.player.UnityPlayer");
jou = jcu.GetStatic<AndroidJavaObject>("currentActivity");
mLocationClient = new AndroidJavaObject("com.amap.api.location.AMapLocationClient", jou);
mLocationOption = new AndroidJavaObject("com.amap.api.location.AMapLocationClientOption");
mLocationClient.Call("setLocationOption", mLocationOption);
amap = new ChangeJavaToC();
amap.locationChanged += OnLocationChanged;

mLocationClient.Call("setLocationListener", amap);

mLocationClient.Call("startLocation");

}
catch (Exception ex)
{

txtLocation.text = ex.Message;

EndLocation();
}
}

public void EndLocation()
{
if (amap != null)
{
amap.locationChanged -= OnLocationChanged;
}

if (mLocationClient != null)
{
mLocationClient.Call("stopLocation");
mLocationClient.Call("onDestroy");
}

txtLocation.text = "";
}

private void OnLocationChanged(AndroidJavaObject amapLocation)
{
if (amapLocation != null)
{
if (amapLocation.Call<int>("getErrorCode") == 0)
{
txtLocation.text = "成功定位获取数据:";

try
{
txtLocation.text = txtLocation.text + "\r\n>>定位结果来源:" + amapLocation.Call<int>("getLocationType").ToString();
txtLocation.text = txtLocation.text + "\r\n>>纬度:" + amapLocation.Call<double>("getLatitude").ToString();
txtLocation.text = txtLocation.text + "\r\n>>经度:" + amapLocation.Call<double>("getLongitude").ToString();
txtLocation.text = txtLocation.text + "\r\n>>精度信息:" + amapLocation.Call<float>("getAccuracy").ToString();
txtLocation.text = txtLocation.text + "\r\n>>地址:" + amapLocation.Call<string>("getAddress").ToString();
txtLocation.text = txtLocation.text + "\r\n>>国家:" + amapLocation.Call<string>("getCountry").ToString();
txtLocation.text = txtLocation.text + "\r\n>>省:" + amapLocation.Call<string>("getProvince").ToString();
txtLocation.text = txtLocation.text + "\r\n>>城市:" + amapLocation.Call<string>("getCity").ToString();
txtLocation.text = txtLocation.text + "\r\n>>城区:" + amapLocation.Call<string>("getDistrict").ToString();
txtLocation.text = txtLocation.text + "\r\n>>街道:" + amapLocation.Call<string>("getStreet").ToString();
txtLocation.text = txtLocation.text + "\r\n>>门牌:" + amapLocation.Call<string>("getStreetNum").ToString();
txtLocation.text = txtLocation.text + "\r\n>>城市编码:" + amapLocation.Call<string>("getCityCode").ToString();
txtLocation.text = txtLocation.text + "\r\n>>地区编码:" + amapLocation.Call<string>("getAdCode").ToString();
txtLocation.text = txtLocation.text + "\r\n>>海拔:" + amapLocation.Call<double>("getAltitude").ToString();
txtLocation.text = txtLocation.text + "\r\n>>方向角:" + amapLocation.Call<float>("getBearing").ToString();
txtLocation.text = txtLocation.text + "\r\n>>定位信息描述:" + amapLocation.Call<string>("getLocationDetail").ToString();
txtLocation.text = txtLocation.text + "\r\n>>兴趣点:" + amapLocation.Call<string>("getPoiName").ToString();
txtLocation.text = txtLocation.text + "\r\n>>提供者:" + amapLocation.Call<string>("getProvider").ToString();
txtLocation.text = txtLocation.text + "\r\n>>卫星数量:" + amapLocation.Call<int>("getSatellites").ToString();
txtLocation.text = txtLocation.text + "\r\n>>当前速度:" + amapLocation.Call<float> ("getSpeed").ToString ();

}
catch (Exception ex)
{
txtLocation.text = txtLocation.text + "\r\n--------------ex-------------:";
txtLocation.text = txtLocation.text + "\r\n" + ex.Message;
}

}
else
{
txtLocation.text = ">>amaperror:";
txtLocation.text = txtLocation.text + ">>getErrorCode:" + amapLocation.Call<int>("getErrorCode").ToString();
txtLocation.text = txtLocation.text + ">>getErrorInfo:" + amapLocation.Call<string>("getErrorInfo");
}
}
else
{
txtLocation.text = "amaplocation is null.";
}
}
}

真机打包测试

Unity接入高德定位sdk简单三步无需与安卓工程交互



 

欢迎加入Unity业内qq交流群:956187480