Unity接入高德SDK實現定位
阿新 • • 發佈:2019-02-04
一.在高德官網下載需要對應的SDK http://lbs.amap.com/api/android-location-sdk
通過SHA1值獲取對應的key值
a.SHA1值得獲取: 1.在cmd中定位到 .android
2.輸入keytool -list -v -keystore debug.keystore 口令是:android
二.匯入下載的高德sdk 和classes jar檔案放在lib檔案下
並點選右鍵新增依賴
三.編寫主函式
public class MainActivity extends UnityPlayerActivity { //宣告mLocationClient物件 public AMapLocationClient mLocationClient = null; public AMapLocationClientOption mLocationOption = null; private String LocationInfo; private String ErrorInfo=""; protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); } //獲取定位資訊 public String[] GetInfo() { String[] a=new String[2]; a[0]=this.LocationInfo; a[1]=this.ErrorInfo; startLocation(); return a; } protected void onStart() { super.onStart(); } private void startLocation() { this.mLocationClient = new AMapLocationClient(getApplicationContext()); //回撥監聽 this.mLocationClient.setLocationListener(this.mLocationListener); //初始化定位引數 this.mLocationOption = new AMapLocationClientOption(); this.mLocationOption.setLocationMode(AMapLocationClientOption.AMapLocationMode.Hight_Accuracy); this.mLocationOption.setInterval(1000L); this.mLocationOption.setHttpTimeOut(10000l); this.mLocationOption.setNeedAddress(true); this.mLocationClient.setLocationOption(this.mLocationOption); this.mLocationClient.startLocation(); } public AMapLocationListener mLocationListener = new AMapLocationListener() { @Override public void onLocationChanged(AMapLocation location) { if (location != null) { if (location.getErrorCode() == 0) { StringBuffer sb = new StringBuffer(256); SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); Date date = new Date(location.getTime()); String time=df.format(date); sb.append("時間: " + time); sb.append("\n緯度:" + location.getLatitude()); sb.append("\n經度:" + location.getLongitude()); sb.append("\n精度:" + location.getAccuracy()); sb.append("\n地址:" + location.getAddress()); sb.append("\n國家資訊:" + location.getCountry()); sb.append("\n省資訊:" + location.getProvince()); sb.append("\n城市資訊:" + location.getCity()); sb.append("\n城區資訊:" + location.getDistrict()); sb.append("\n街道資訊:" + location.getStreet()); sb.append("\n街道門牌號資訊:" + location.getStreetNum()); sb.append("\n城市編碼:" + location.getCityCode()); sb.append("\n地區編碼:" + location.getAdCode()); LocationInfo = sb.toString(); }else { StringBuffer errorinfo = new StringBuffer(256); errorinfo.append("錯誤程式碼:"+location.getErrorCode()); errorinfo.append("\n"+location.getErrorInfo()); ErrorInfo=errorinfo.toString(); } } } };
四.將模組打包成aar格式供unity互動
修改 build.gradle檔案
第一步:將apply plugin: 'com.android.appcation'修改為
apply plugin: 'com.android.library'
第二部:刪除 applicationId "com.test.sdk1"
第三步:在AndroidMainfest檔案中修改
android:theme="@android:style/Theme.NoTitleBar"> 新增
<meta-data android:name="unityplayer.UnityActivity"android:value="true"/>
第四步:在AndroidManifest.xml的application標籤中配置Key:
<meta-data android:name="com.amap.api.v2.apikey" android:value="您的Key"></meta-data>
在application標籤中宣告service元件:
<service android:name="com.amap.api.location.APSService"></service>
在AndroidManifest.xml中配置許可權:
<!--用於進行網路定位-->
<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>
最後build就可以打包成aar包了五.unity中訪問android
將打包出來的aar檔案和AndroidMainifest檔案放在Plugins/Android資料夾下
此時就可以編寫c#程式碼了
public class Test : MonoBehaviour
{
public Text content;
public Button button;
AndroidJavaClass jc;
AndroidJavaObject jo;
void Start()
{
content.text = "654321";
OnStart();
}
public void GetLocation()
{
string[] a;
OnStart();
a = jo.Call<string[]>("GetInfo");
content.text = a[0];
if (a[1] != "")
{
content.text = a[1];
}
}
void OnStart()
{
jc = new AndroidJavaClass("com.unity3d.player.UnityPlayer");
jo = jc.GetStatic<AndroidJavaObject>("currentActivity");
}
unity呼叫android中的一些函式
這樣就簡單的實現高德的定位功能啦!