Unity接入高德定位sdk簡單三步無需與安卓工程互動
阿新 • • 發佈:2018-11-10
原始碼,原工程下載地址:https://download.csdn.net/download/qq_37310110/10729281
參考地址:https://blog.csdn.net/qq_37310110/article/details/83145193
一:高德定位有效key的獲取
參考官方文件地址:獲取有效key的詳細流程
注意拿到的key確定是正式的,如果只是測試的話就不能運用到生產環境
二: sdk的使用
1.把下載下來的sdk下的jar包放進unity的Plugins<Android< AMap_Location_V4.2.0_20180809.jar
2.合併配置AndroidManifest 檔案,
新增許可權:
<?xml version="1.0" encoding="utf-8" standalone="no"?> <manifest xmlns:android="http://schemas.android.com/apk/res/android" android:installLocation="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:icon="@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:configChanges="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:screenOrientation="portrait"> </application> </manifest>
設定從高德拿到的key:
三: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.";
}
}
}