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米。
(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 ,挂到主摄像机上。
- using UnityEngine;
- using System.Collections;
- public class GetGPS : MonoBehaviour {
- public string gps_info = "";
- public int flash_num = 1;
- // Use this for initialization
- void Start () {
- }
- void OnGUI () {
- GUI.skin.label.fontSize = 28;
- GUI.Label(new Rect(20,20,600,48),this.gps_info);
- GUI.Label(new Rect(20,50,600,48),this.flash_num.ToString());
- GUI.skin.button.fontSize = 50;
- if (GUI.Button(new Rect(Screen.width/2-110,200,220,85),"GPS定位"))
- {
- // 这里需要启动一个协同程序
- StartCoroutine(StartGPS());
- }
- if (GUI.Button(new Rect(Screen.width/2-110,500,220,85),"刷新GPS"))
- {
- this.gps_info = "N:" + Input.location.lastData.latitude + " E:"+Input.location.lastData.longitude;
- this.gps_info = this.gps_info + " Time:" + Input.location.lastData.timestamp;
- this.flash_num += 1;
- }
- }
- // Input.location = LocationService
- // LocationService.lastData = LocationInfo
- void StopGPS () {
- Input.location.Stop();
- }
- IEnumerator StartGPS () {
- // Input.location 用于访问设备的位置属性(手持设备), 静态的LocationService位置
- // LocationService.isEnabledByUser 用户设置里的定位服务是否启用
- if (!Input.location.isEnabledByUser) {
- this.gps_info = "isEnabledByUser value is:"+Input.location.isEnabledByUser.ToString()+" Please turn on the GPS";
- return false;
- }
- // LocationService.Start() 启动位置服务的更新,最后一个位置坐标会被使用
- Input.location.Start(10.0f, 10.0f);
- int maxWait = 20;
- while (Input.location.status == LocationServiceStatus.Initializing && maxWait > 0) {
- // 暂停协同程序的执行(1秒)
- yield return new WaitForSeconds(1);
- maxWait--;
- }
- if (maxWait < 1) {
- this.gps_info = "Init GPS service time out";
- return false;
- }
- if (Input.location.status == LocationServiceStatus.Failed) {
- this.gps_info = "Unable to determine device location";
- return false;
- }
- else {
- this.gps_info = "N:" + Input.location.lastData.latitude + " E:"+Input.location.lastData.longitude;
- this.gps_info = this.gps_info + " Time:" + Input.location.lastData.timestamp;
- yield return new WaitForSeconds(100);
- }
- }
- }
3,导出到手机,运行,即可看到效果。
转职至:http://blog.csdn.net/chenggong2dm/article/details/24488469
- unity 定位服务GPS API
- unity通过GPS定位
- GPS定位API
- android gps 定位服务
- 调用GPS定位服务
- 【Unity】使用GPS定位经纬度
- Unity之GPS定位详解
- 百度GPS定位API使用方法
- Unity ios平台GPS地址位置定位
- Unity ios平台GPS地址位置定位
- Unity内部类实现GPS定位
- 百度地图API安卓版的GPS定位
- Arcgis API for Android之GPS定位
- android中的与GPS定位的API
- Android下实现GPS定位服务
- 位置服务及GPS定位的调用
- 判断GPS定位服务是否打开
- Android 位置服务与GPS实时定位
- Android RxJava详解(二)
- centos6.5下使用yum完美搭建LNMP环境(php5.6)
- idea关于tomcat发布项目与eclipse的不同
- Ceilometer原理及介绍
- Informatics Training
- unity 定位服务GPS API
- bulid签名apk出错
- 如何重置CentOS 7的Root密码
- Spring公共头部信息,基于本地验证的
- NYOJ +-字符串
- Linux入门(3)——Ubuntu16.04下安装VMware
- 10.在HTTP下进行异步编程
- Android开发AndroidStudio多渠道打包
- Retrofit2.0使用