unity 定位服务GPS API

来源:互联网 发布:淘宝魔镜插件免费版 编辑:程序博客网 时间:2024/04/30 20:57

Unity使用GPS 的API


在unity的官方文档中,与设备定位(GPS经纬度、水平精度等等)相关的API,目前我只找到两个:LocationService 和 LocationInfo 。

先来个简单的理解:

LocationService 负责启动和关闭定位服务

LocationInfo  在服务启动后,获取定位数据信息


LocationService

官方说明链接:

http://docs.unity3d.com/Documentation/ScriptReference/LocationService.Start.html

LocationService 中有三个属性,和两个方法:

(1)isEnabledByUser   -- 用户设置里的定位服务是否启用。(实测发现,都为true,似乎不大起作用)

(2)lastData   -- 最近一次测量的地理位置(LocationInfo lastData; 也就是要和 LocationInfo 关联了)

(3)status   -- 定位服务的状态。

定位服务的状态包括:

Stopped
Location service is stopped. 定位服务已经停止
Initializing
Location service is initializing, some time later it will switch to.  定位服务正在初始化,在一段时间后,状态会切换回来。
Running
Location service is running and locations could be queried.  位置服务正在运行,位置可以获取。
Failed
Location service failed (user denied access to location service).  位置服务失败(用户拒绝访问位置服务


(4)Start ( )   -- 启动定位服务,更新定位数据。可以获取最近更新的位置坐标。

     数据接收,是通过 Input.location.lastData 来实现的。服务不能马上获得定位数据。代码必须检查Input.location.status以获取当前的定位服务状态。

看一下函数定义:

void Start(float desiredAccuracyInMeters = 10f, float updateDistanceInMeters = 10f); 

参数详解:

desiredAccuracyInMeters  服务所需的精度,以米为单位。如果使用较高的值,比如500,那么通常不需要打开GPS芯片(比如可以利用信号基站进行三角定位),从而节省电池电量像5-10这样的值,可以被用来获得最佳的精度。默认值是10米。

updateDistanceInMeters  最小距离(以米为单位)的设备必须横向移动Input.location属性被更新。较高的值,如500意味着更少的开销默认值是10米。

(5)Stop ( )  -- 停止定位服务的定位更新。对节省电池的电量非常有用。void


LocationInfo

属性如下:

(1) altitude -- 海拔高度 

(2) horizontalAccuracy -- 水平精度

(3) latitude -- 纬度

(4) longitude -- 经度

(5) timestamp -- 最近一次定位的时间戳,从1970开始

(6) verticalAccuracy -- 垂直精度


这些属性,除了timestamp为double外, 其余全为 float 型。



下面是Unity使用GPS 的例子

1,新建一个项目。

2,编写脚本 GetGPS.cs ,挂到主摄像机上。

[csharp] view plain copy
  1. using UnityEngine;  
  2. using System.Collections;  
  3.   
  4. public class GetGPS : MonoBehaviour {  
  5.   
  6.     public string gps_info = "";  
  7.     public int flash_num = 1;  
  8.   
  9.     // Use this for initialization  
  10.     void Start () {  
  11.       
  12.     }  
  13.       
  14.     void OnGUI () {  
  15.         GUI.skin.label.fontSize = 28;  
  16.         GUI.Label(new Rect(20,20,600,48),this.gps_info);   
  17.         GUI.Label(new Rect(20,50,600,48),this.flash_num.ToString());   
  18.           
  19.         GUI.skin.button.fontSize = 50;  
  20.         if (GUI.Button(new Rect(Screen.width/2-110,200,220,85),"GPS定位"))  
  21.         {  
  22.             // 这里需要启动一个协同程序  
  23.             StartCoroutine(StartGPS());  
  24.         }  
  25.         if (GUI.Button(new Rect(Screen.width/2-110,500,220,85),"刷新GPS"))  
  26.         {  
  27.             this.gps_info = "N:" + Input.location.lastData.latitude + " E:"+Input.location.lastData.longitude;  
  28.             this.gps_info = this.gps_info + " Time:" + Input.location.lastData.timestamp;  
  29.             this.flash_num += 1;   
  30.         }  
  31.     }  
  32.   
  33.     // Input.location = LocationService  
  34.     // LocationService.lastData = LocationInfo   
  35.   
  36.     void StopGPS () {  
  37.         Input.location.Stop();  
  38.     }  
  39.   
  40.     IEnumerator StartGPS () {  
  41.         // Input.location 用于访问设备的位置属性(手持设备), 静态的LocationService位置  
  42.         // LocationService.isEnabledByUser 用户设置里的定位服务是否启用  
  43.         if (!Input.location.isEnabledByUser) {  
  44.             this.gps_info = "isEnabledByUser value is:"+Input.location.isEnabledByUser.ToString()+" Please turn on the GPS";   
  45.             return false;  
  46.         }  
  47.   
  48.         // LocationService.Start() 启动位置服务的更新,最后一个位置坐标会被使用  
  49.         Input.location.Start(10.0f, 10.0f);  
  50.   
  51.         int maxWait = 20;  
  52.         while (Input.location.status == LocationServiceStatus.Initializing && maxWait > 0) {  
  53.             // 暂停协同程序的执行(1秒)  
  54.             yield return new WaitForSeconds(1);  
  55.             maxWait--;  
  56.         }  
  57.   
  58.         if (maxWait < 1) {  
  59.             this.gps_info = "Init GPS service time out";  
  60.             return false;  
  61.         }  
  62.   
  63.         if (Input.location.status == LocationServiceStatus.Failed) {  
  64.             this.gps_info = "Unable to determine device location";  
  65.             return false;  
  66.         }   
  67.         else {  
  68.             this.gps_info = "N:" + Input.location.lastData.latitude + " E:"+Input.location.lastData.longitude;  
  69.             this.gps_info = this.gps_info + " Time:" + Input.location.lastData.timestamp;  
  70.             yield return new WaitForSeconds(100);  
  71.         }  
  72.     }  
  73. }  

3,导出到手机,运行,即可看到效果。


转职至:http://blog.csdn.net/chenggong2dm/article/details/24488469


0 0