首页 > 其他分享 >unity3d,android平台下,高德地图定位

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

时间:2023-01-04 13:35:33浏览次数:45  
标签:unity3d text txtInfo txtLocation amapLocation Call ToString android 高德


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

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


unity3d 5.3.2

高德android 定位 sdk 2.3


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

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


新建一个unity项目

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


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

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


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

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

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

keytool -printcert -file cert.rsa



下载sdk包

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


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

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


修改AndroidManifest.xml文件

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

<?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>


做个简单的界面

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


添加类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.";
}
}

}


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

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



标签:unity3d,text,txtInfo,txtLocation,amapLocation,Call,ToString,android,高德
From: https://blog.51cto.com/u_15929643/5988314

相关文章

  • unity3d 尝试 基于地理定位的 增强现实
    首先说,这个尝试失败,属于死在去医院的路上那种。基于地理定位的增强现实,AR全息实景,是一种高大上的说法,说直白点就是山寨类似随便走这样的应用。打开应用,搜索周边信息,然后再把......
  • unity3d,异步加载场景
    很简单,代码如下:usingUnityEngine;usingSystem.Collections;usingUnityEngine.UI;publicclassLoading:MonoBehaviour{publicSliderloading_bar;privateAsyncO......
  • unity+高德定位=pokemon go 山寨demo安卓版
    这两周尝试了下用高德地理定位和Unity来做个山寨的pokemongo的demo,只能在安卓下使用。游戏过程视频:​​http://www.bilibili.com/video/av6836823/​​场景一这里是获取......
  • 《Unity3D平台AR开发快速上手--基于EasyAR4.0》随书资源和相关说明
    新手《Unity3D平台AR开发快速上手–基于EasyAR4.0》上市了,现在京东和淘宝都有卖。书分为2个部分,第一部分是EasyAR4.0基础内容和使用,第二部分是利用EasyAR的稀疏空间地图做室......
  • Android笔记--如何在Android studio里面打开数据库
    具体操作1、找到界面内的DeviceFileExplorer这里找可以;这里直接打开也行2、在里面找到与java下面的第一个package名称相同的文件夹3、找到下面的databases会看......
  • Android笔记--修改Device File Explorer的文件打开方式
    在首次打开该文件时,不小心选错了打开方式,导致以后每次打开也是同样的打开方式,也不会弹出第一次那样的打开方式的选择弹窗在这里提供修改文件的默认打开方式的方法:首先通......
  • Android笔记--基础的连接数据库的操作
    start.javapackagecom.example.myapplication;importandroidx.activity.result.ActivityResult;importandroidx.activity.result.ActivityResultCallback;importa......
  • Android学习day03【跑马灯】
    这个学习内容很简单,说一下自己笨蛋的点吧要实现这种情况,我始终没有办法实现  后来发现(你先别说话,我知道很傻,但是大话先别说太早)android:layout_width="wrap_cont......
  • 【安卓】Qt for Android开发环境搭建
    安装Qt略(参考网上的安装Qt即可,暂时不考虑其他组件)安装jdk17下载1.8版本网上推荐使用JDK1.8,而不是其他版本,不建议使用最新版。下载1.8JavaDownloads|Oracle设置环......
  • vue 高德地图 西湖区 点标记 001
    效果:index.html中 引入amap<linkrel="stylesheet"href="https://a.amap.com/jsapi_demos/static/demo-center/css/demo-center.css"/><scripttype="text/javascript"s......