unity3d,android平台下,高德地图定位

这里,用了一个比较偷懒的办法,直接用高德提供的android定位sdk,没有重新编译。好处是省事,坏处是,没法修改默认的定位模式。部分信息获取不到。

如果需要完整的功能,还是需要重新编译高德地图的sdk。


unity3d 5.3.2

高德android 定位 sdk 2.3


首先,注册高德sdk的用户,并且获取key。这里的key和编译环境的sha1相关。



新建一个unity项目



先把项目编译成android,记得Bundle Identifier必须和高德key里面的pakage一致。



用apk逆向工具把编译好的apk文件解开,


找到cert.rsa,用命令行获取调式用的sha1。官方只提供了android studio调式用的sha1获取的办法。

keytool -printcert -file cert.rsa


下载sdk包



把下载的jar和逆相出来的AndroidManifest.xml导入到unity工程的Plugins/Android目录下



修改AndroidManifest.xml文件


<?xml version="1.0" encoding="utf-8" standalone="no"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android" android:installLocation="preferExternal" package="com.nsh.gpsar" platformBuildVersionCode="23" platformBuildVersionName="6.0-2438415">
  <supports-screens android:anyDensity="true" android:largeScreens="true" android:normalScreens="true" android:smallScreens="true" android:xlargeScreens="true"/>
  <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">
    <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">
      <intent-filter>
        <action android:name="android.intent.action.MAIN"/>
        <category android:name="android.intent.category.LAUNCHER"/>
        <category android:name="android.intent.category.LEANBACK_LAUNCHER"/>
      </intent-filter>
      <meta-data android:name="unityplayer.UnityActivity" android:value="true"/>
    </activity>
  </application>
  <uses-feature android:glEsVersion="0x20000"/>
  <uses-feature android:name="android.hardware.touchscreen" android:required="false"/>
  <uses-feature android:name="android.hardware.touchscreen.multitouch" android:required="false"/>
  <uses-feature android:name="android.hardware.touchscreen.multitouch.distinct" android:required="false"/>
  <uses-permission android:name="android.permission.INTERNET" />
  <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
  <uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
  <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
  <uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
  <uses-permission android:name="android.permission.READ_PHONE_STATE" />
  <uses-permission android:name="android.permission.CHANGE_WIFI_STATE" />
  <uses-permission android:name="android.permission.ACCESS_WIFI_STATE" />
  <uses-permission android:name="android.permission.CHANGE_CONFIGURATION" />
  <uses-permission android:name="android.permission.WAKE_LOCK" />
  <uses-permission android:name="android.permission.WRITE_SETTINGS" />
</manifest>

做个简单的界面



添加类AmapEvent,将android的事件转化成unity的事件

using UnityEngine;
using System.Collections;

public class AmapEvent : AndroidJavaProxy {

	public AmapEvent ()
		: 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;
}

添加locationmange类,调用高德的sdk。定位时间和定位速度无法获取到,会报错。似乎在android studio里可以。

using UnityEngine;
using System.Collections;
using System;
using UnityEngine.UI;

public class LocationManage : MonoBehaviour
{

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

	public void StartLocation ()
	{
		try {
			txtInfo.text = "start location...";

			txtInfo.text = txtInfo.text + "\r\n";
			jcu = new AndroidJavaClass ("com.unity3d.player.UnityPlayer"); 
			jou = jcu.GetStatic<AndroidJavaObject> ("currentActivity");
			txtInfo.text = txtInfo.text + "currentActivity get...";

			txtInfo.text = txtInfo.text + "\r\n";
			mLocationClient = new AndroidJavaObject ("com.amap.api.location.AMapLocationClient", jou);
			txtInfo.text = txtInfo.text + "AMapLocationClient get...";

			txtInfo.text = txtInfo.text + "\r\n";
			mLocationOption = new AndroidJavaObject ("com.amap.api.location.AMapLocationClientOption");
			txtInfo.text = txtInfo.text + "AMapLocationClientOption get...";

			txtInfo.text = txtInfo.text + "\r\n";
			mLocationClient.Call ("setLocationOption", mLocationOption);
			txtInfo.text = txtInfo.text + "setLocationOption...";

			amap = new AmapEvent ();
			amap.locationChanged += OnLocationChanged;

			txtInfo.text = txtInfo.text + "\r\n";
			mLocationClient.Call ("setLocationListener", amap);
			txtInfo.text = txtInfo.text + "setLocationListener...";

			txtInfo.text = txtInfo.text + "\r\n";
			mLocationClient.Call ("startLocation");
			txtInfo.text = txtInfo.text + "startLocation...";

		} catch (Exception ex) {
			txtInfo.text = txtInfo.text + "\r\n";
			txtInfo.text = txtInfo.text + "--------------------";
			txtInfo.text = txtInfo.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 = ">>success:";

				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<AndroidJavaObject> ("getTime").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<string> ("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 {
			txtInfo.text = "amaplocation is null.";
		}
	}

}

编译以后,运行,结果如下



源码及apk下载:http://download.csdn.net/detail/wuyt2008/9449732

评论 5
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值