相关的还有 Input.location 、 LocationServiceStatus
Input.location —— 用于访问设备的位置(仅支持手持设备)(只读)(LocationService类型)
LocationService 类
这个类也不是静态类,但是 貌似默认的就是用于被 Input.location 调用
大致流程应该是这样的 :
(1)如果用户没有启动定位功能 if( ! Input.location.isEnabledByUser ),那么用户手动开启GPS
(2)开启了GPS之后,才能够 Input.location.Start(),由于不是立刻就能从几个状态切换,所以最好用协程
(3)等待启动的结果,if(Input.location.Status == LocationServiceStatus.xxx) { … }
(4)如果服务正在运行,那么可以用 Input.location.lastData 获取数据
注意:貌似 Input.location.isEnabledByUser 总是 true(至少在PC上win10测试结果是这样,因为Input.location仅支持手持设备),另一个人的文章中也提到这个 http://blog.csdn.net/chenggong2dm/article/details/24488469
Input.location —— Property for accessing device location (handheld devices only). (Read Only) —— API文档
另外,关于 Input.location.isEnabledByUser,文档中提到
Specifies whether location service is enabled in user settings.
.
You should check this property before starting location updates to determine if the user has location services enabled for the current device.
.
On iOS, if this property is false and you start location updates anyway, the CoreLocation framework prompts the user with a confirmation panel asking whether location services should be reenabled. The user can enable or disable location services altogether from the Settings application by toggling the switch in Settings>General>LocationServices.
.
说的是 iOS 中,如果用户没有手动开启GPS,那么使用 Input.location.Start() 的时候会自动提示用户去开启(没啥用)
这个类,必须要用真机测试,导出apk到手机上才行。。。。。。
属性 | 说明 |
---|---|
isEnabledByUser | 用户是否开启定位功能(bool) |
lastData | 最后测量设备的地理位置。(LocationInfo),在获取这个值之前必须先 Input.location.Start() |
status | 返回位置服务状态(LocationServiceStatus) |
方法 | 说明 |
---|---|
Start | 启动位置服务更新。可以通过 Input.location.lastData 获取最后的坐标,但是也不是执行方法立刻就能更新位置服务数据,为此需要先检查位置服务状态 Input.location.status(意思就是可能Start之后初始化花了一段时间、初始化之后可能失败) |
Stop | 停止位置服务更新。这对节省电池寿命可能有用。 |
public void Start(float desiredAccuracyInMeters = 10f, float updateDistanceInMeters = 10f);
public void Stop();
LocationServiceStatus 枚举
值 | 说明 |
---|---|
Stopped | 位置服务停止。 |
Initializing | 位置服务正在初始化,一段时间后它将切换到 Running 或 Failed。 |
Running | 位置服务正在运行,可以查询位置。 |
Failed | 位置服务失败(用户拒绝访问位置服务)。 |
LocationInfo 结构体
描述设备的位置信息,以米为单位。
属性 | 说明 |
---|---|
altitude | 海拔高度 |
latitude | 设备的地理位置的纬度 |
longitude | 设备的地理位置的纬度(貌似同上,而且文档中没有其他说明) |
timestamp | 位置最后更新的时间戳(自1970年计数的秒数) |
horizontalAccuracy | 水平精度 |
verticalAccuracy | 垂直精度 |
//关于 LocationService.Start 的官方示例
using UnityEngine;
using System.Collections;
public class TestLocationService : MonoBehaviour
{
IEnumerator Start()
{
// First, check if user has location service enabled
if (!Input.location.isEnabledByUser)
yield break;
// Start service before querying location
Input.location.Start();
// Wait until service initializes
int maxWait = 20;
while (Input.location.status == LocationServiceStatus.Initializing && maxWait > 0)
{
yield return new WaitForSeconds(1);
maxWait--;
}
// Service didn't initialize in 20 seconds
if (maxWait < 1)
{
print("Timed out");
yield break;
}
// Connection has failed
if (Input.location.status == LocationServiceStatus.Failed)
{
print("Unable to determine device location");
yield break;
}
else
{
// Access granted and location value could be retrieved
print("Location: " + Input.location.lastData.latitude + " " + Input.location.lastData.longitude + " " + Input.location.lastData.altitude + " " + Input.location.lastData.horizontalAccuracy + " " + Input.location.lastData.timestamp);
}
// Stop service if there is no need to query location updates continuously
Input.location.Stop();
}
}
public class LocationTest3 : MonoBehaviour
{
IEnumerator StartLocation ()
{
if (!Input.location.isEnabledByUser)
yield break;
Input.location.Start ();
int maxWait = 20;
while (Input.location.status == LocationServiceStatus.Initializing && maxWait > 0)
{
yield return new WaitForSeconds (1);
maxWait--;
}
if (maxWait < 1)
{
print ("time out");
yield break;
}
if (Input.location.status == LocationServiceStatus.Failed)
{
print ("unable to determine device location");
yield break;
}
else
{
print("location : "+ Input.location.lastData.altitude + " "+Input.location.lastData.latitude + " "+ Input.location.lastData.longitude );
}
}
void OnGUI()
{
if (GUILayout.Button ("START")) {
StartCoroutine ("StartLocation");
}
if (GUILayout.Button ("STOP")) {
Input.location.Stop ();
}
GUILayout.Label ("Input.location.isEnabledByUser : " + Input.location.isEnabledByUser);
GUILayout.Label ("Input.location.Status : " + Input.location.status);
GUILayout.Label ("Input.location.lastData : " +
Input.location.lastData.latitude+ " " +
Input.location.lastData.longitude);
}
}