Unity接入高德地图SDK【Android】

想要做一个基于地理位置的AR程序,首先第一步就是要获得自己当前位置,Unity自身提供了一个基于GPS的定位方法:(详见https://docs.unity3d.com/ScriptReference/LocationService.html)但是经过测试发现定位显示的是经纬度,输入查询后发现有偏差。于是就在网上查资料,发现可以用接入地图的SDK来直接方便的实现定位功能。

详细步骤:
1.申请高德地图开发者账号 地址: http://lbs.amap.com/
2.生成keystore,在控制台打开javaJDK目录,输入 keytool -genkey -alias android.keystore -keyalg RSA -validity 20000 -keystore android.keystore 命令,填写信息后生成一个keystore, 新建一个unity工程,修改包名,添加这个keystore,导出apk

生成keystore.png

3.将生成的apk后缀改为rar,进行解压
4.解压后找到cert.rsa文件,复制路径
5.在控制台中输入:keytool -printcert -file “你的CERT.RSA路径” 获得apk的SHA1值
获得SHA1值.png

6.进入高的开放平台控制台,在“我的应用”里点“创建新应用”
创建应用.png

7.输入应用名称和应用类型后点击创建
输入名称和类型.png

8.然后点击“添加key”,其中,发布的安全码SHA1为第五步获得的SHA1值,包名为unity工程的包名。点击提交按钮即可获得key
添加key.png

9.然后去下载高德地图的安卓SDK,下载地址:http://lbs.amap.com/api/android-sdk/download
10.打开unity工程,将"\AMap_Android_Location_SDK_All\AMapLocation\AMap_Location_V3.7.0_20171218.jar"文件放到unity工程的 /Assets/Plugins/Android/ 下面
11.参考下载的SDK里面的AndroidMenifest。首先把包名从“com.unity3d.player”改为你unity工程的包名,然后把权限添加上

<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" />

然后加上key和service

<meta-data
       android:name="com.amap.api.v2.apikey"
       android:value="此处填入获得的高德地图的key" />
<!-- 定位需要的服务 -->
<service android:name="com.amap.api.location.APSService" ></service>

12.添加类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。定位时间和定位速度无法获取到,会报错。暂未找到原因。

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

Unity项目工程详细代码:https://gitee.com/Zhiqi_Kou/unityJieRuGaoDeDiTuAnZhuoSDK

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 157,198评论 4 359
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 66,663评论 1 290
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 106,985评论 0 237
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 43,673评论 0 202
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 51,994评论 3 285
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 40,399评论 1 211
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 31,717评论 2 310
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 30,407评论 0 194
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 34,112评论 1 239
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 30,371评论 2 241
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 31,891评论 1 256
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 28,255评论 2 250
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 32,881评论 3 233
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 26,010评论 0 8
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 26,764评论 0 192
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 35,412评论 2 269
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 35,299评论 2 260