美文网首页Unity3D
Unity接入高德地图SDK【Android】

Unity接入高德地图SDK【Android】

作者: 稚琦 | 来源:发表于2018-01-25 09:50 被阅读0次

想要做一个基于地理位置的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

相关文章

网友评论

    本文标题:Unity接入高德地图SDK【Android】

    本文链接:https://www.haomeiwen.com/subject/sbwuaxtx.html