unity 定位
2015-12-27 22:45
549 查看
总结一下前段时间做的地理定位功能吧
需求:定位玩家的当前位置,(不用太精细)
实现步骤:(说明,因为项目不需要定位太过精细,所以将逆地址解析放在了服务端,如果需要精细的定位的话,需要在客户端接SDK的)
1、使用unity的api获取当前设备的经纬度,
\
2、将经纬度上传到服务端,
3、通过服务端访问百度或者高德的定位服务(这一步不再细说,需要注册百度或高德的开发者账户然后获得key)
4、从服务端获得地址解析的信息
具体实现如下:
其中的Input.location.lastData.latitude 和 Input.location.lastData.longitude就是获得的设备经纬度
需求:定位玩家的当前位置,(不用太精细)
实现步骤:(说明,因为项目不需要定位太过精细,所以将逆地址解析放在了服务端,如果需要精细的定位的话,需要在客户端接SDK的)
1、使用unity的api获取当前设备的经纬度,
\
2、将经纬度上传到服务端,
3、通过服务端访问百度或者高德的定位服务(这一步不再细说,需要注册百度或高德的开发者账户然后获得key)
4、从服务端获得地址解析的信息
具体实现如下:
[code] IEnumerator StartGPS() { //MyDebuger.LogWarning("IEnumerator StartGPS()"); GetGPSStatus status = GetGPSStatus.Initializing; // Input.location 用于访问设备的位置属性(手持设备), 静态的LocationService位置 // LocationService.isEnabledByUser 用户设置里的定位服务是否启用 if (!Input.location.isEnabledByUser) { gps_info = "isEnabledByUser value is:" + Input.location.isEnabledByUser.ToString() + " Please turn on the GPS"; } //LocationService.Start();//启动位置服务的更新,最后一个位置坐标会被使用 Input.location.Start(1.0f, 1.0f); int maxWait = 20; while (Input.location.status == LocationServiceStatus.Initializing && maxWait > 0) { // 暂停协同程序的执行(1秒) yield return new WaitForSeconds(1); maxWait--; } if (Input.location.status == LocationServiceStatus.Failed) { gps_info = "Unable to determine device location"; } else { gps_info = "N南北:" + Input.location.lastData.latitude + " E东西:" + Input.location.lastData.longitude; gps_info = gps_info + " Time:" + Input.location.lastData.timestamp; } if (maxWait < 1) { gps_info = "超时";//"Init GPS service time out"; status = GetGPSStatus.OutTime; } else { status = (GetGPSStatus)Enum.Parse(typeof(GetGPSStatus),Input.location.status.ToString()); } StopGPS(); List<object> list_obj = new List<object>(); list_obj.Add(status); //UIMgr.Single().postMsg((int)_eUiMsg.GetGPSStatus,list_obj); //发送定位点的经纬度 }
其中的Input.location.lastData.latitude 和 Input.location.lastData.longitude就是获得的设备经纬度
相关文章推荐
- 从Unity引擎过度到Unreal4引擎(最终版)
- Unity官网教程之Tips
- Unity3D必备知识: 物理学公式
- 解读Unity中的CG编写Shader系列1——初识CG
- 【Unity Shader】基于UGUI的水波倒影按钮
- C# 文件选择对话框,Unity3d文件保存对话框
- Unity3D教程:茄子童萌會
- Unity3D 发布无边框exe
- Unity3D DllNotFoundException/System.DllNotFoundException
- Unity3D 使用C#指针unsafe
- NullReferenceException UnityEngine.Transform.get_localPosition
- Unity3D 调用模态对话框/Unity3D MessageBox
- Unity3D-微信-飞机大战-项目经过
- 【Unity Shaders】使用CgInclude让你的Shader模块化——Unity内置的CgInclude文件
- 关于Unity3D的编辑器崩溃时的线索定位
- UNITY 位移
- unity shader的固定管线(fixed function shader)
- [置顶] unity3d基于ugui的ui模块
- {Unity} Shader初步
- {Unity} 2D游戏,Canvas转世界坐标