首先去高德地圖官網下載定位功能的jar文件

image.png
下載并且導入依賴

image.png
在MaiActivity中代碼如下 獲取定位信息
//聲明AMapLocationClient類對象
public AMapLocationClient mLocationClient = null;
//聲明AMapLocationClientOption對象
public AMapLocationClientOption mLocationOption = null;
//定位回調字符串
private String LocationInfo;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
}
public String GetInfo(){
startLocation();
return LocationInfo;
}
@Override
protected void onStart() {
// TODO Auto-generated method stub
super.onStart();
}
private void startLocation(){
//初始化定位
mLocationClient = new AMapLocationClient(getApplicationContext());
//設置定位回調監(jiān)聽
mLocationClient.setLocationListener(mLocationListener);
//初始化AMapLocationClientOption對象
mLocationOption = new AMapLocationClientOption();
//設置定位模式為AMapLocationMode.Hight_Accuracy,高精度模式。
mLocationOption.setLocationMode(AMapLocationClientOption.AMapLocationMode.Hight_Accuracy);
//設置定位間隔,單位毫秒,默認為2000ms,最低1000ms。
mLocationOption.setInterval(2000);
//給定位客戶端對象設置定位參數(shù)
mLocationClient.setLocationOption(mLocationOption);
//啟動定位
mLocationClient.startLocation();
}
public AMapLocationListener mLocationListener = new AMapLocationListener() {
@Override
public void onLocationChanged(AMapLocation location) {
// TODO Auto-generated method stub
if (location != null) {
if (location.getErrorCode() == 0) {
StringBuffer sb = new StringBuffer(256);
sb.append("時間: ");
sb.append(location.getTime());
sb.append("\n緯度:");
sb.append(location.getLatitude());
sb.append("\n緯度:");
sb.append(location.getLongitude());
sb.append("\n精度:");
sb.append(location.getAccuracy());
sb.append("\n地址:");
sb.append(location.getAddress());
sb.append("\n國家信息:");
sb.append(location.getCountry());
sb.append("\n省信息:");
sb.append(location.getProvince());
sb.append("\n城市信息:");
sb.append(location.getCity());
sb.append("\n城區(qū)信息:");
sb.append(location.getDistrict());
sb.append("\n街道信息:");
sb.append(location.getStreet());
sb.append("\n街道門牌號信息:");
sb.append(location.getStreetNum());
sb.append("\n城市編碼:");
sb.append(location.getCityCode());
sb.append("\n地區(qū)編碼:");
sb.append(location.getAdCode());
sb.append("\n定位點AOI信息:");
sb.append(location.getAoiName());
LocationInfo = sb.toString();
}else {
//定位失敗時,可通過ErrCode(錯誤碼)信息來確定失敗的原因,errInfo是錯誤信息,詳見錯誤碼表。
Log.e("AmapError","location Error, ErrCode:"
+ location.getErrorCode() + ", errInfo:"
+ location.getErrorInfo());
}
}
}
};
在AndroidMainfest.xml中,修改如下
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.xc.mylibrary"
android:versionCode="1"
android:versionName="1.0">
<uses-sdk
android:minSdkVersion="16"
android:targetSdkVersion="21" />
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION"></uses-permission>
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION"></uses-permission>
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"></uses-permission>
<uses-permission android:name="android.permission.ACCESS_WIFI_STATE"></uses-permission>
<uses-permission android:name="android.permission.CHANGE_WIFI_STATE"></uses-permission>
<uses-permission android:name="android.permission.INTERNET"></uses-permission>
<!--用于讀取手機當前的狀態(tài)-->
<uses-permission android:name="android.permission.READ_PHONE_STATE"></uses-permission>
<!--用于寫入緩存數(shù)據(jù)到擴展存儲卡-->
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"></uses-permission>
<application
android:allowBackup="true"
android:label="@string/app_name"
android:supportsRtl="true">
<service android:name="com.amap.api.location.APSService"></service>
<activity android:name=".MainActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<meta-data android:name="com.amap.api.v2.apikey" android:value="************************">//你項目對應的key值
</meta-data>
</application>
</manifest>
同理 生成jar

image.png
然后將指定的兩個jar包,導入unity中

image.png
并且將AndroidMainfest.xml復制到unity
在unity中
usingusing UnityEngine;
UnityEngine; usingusing System.Collections;
System.Collections using UnityEngine.UI;
public class GetLocationInfo : MonoBehaviour {
public Text text;
// Use this for initialization
void Start () {
}
// Update is called once per frame
void Update () {
}
public void StartLocation() {
AndroidJavaClass jc = new AndroidJavaClass("com.unity3d.player.UnityPlayer");
AndroidJavaObject jo = jc.GetStatic<AndroidJavaObject>("currentActivity");
text.text = jo.Call<string>("GetInfo");
}
}
完成對接 獲取到信息