天天看點

unity擷取裝置經緯度(unity使用GPS)詳解

Unity使用GPS 的API

在unity的官方文檔中,與裝置定位(GPS經緯度、水準精度等等)相關的API,目前我隻找到兩個:LocationService 和 LocationInfo 。

先來個簡單的了解:

LocationService 負責啟動和關閉定位服務。

LocationInfo  在服務啟動後,擷取定位資料資訊。

LocationService

官方說明連結:

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

unity擷取裝置經緯度(unity使用GPS)詳解

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 ,挂到主錄影機上。

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,導出到手機,運作,即可看到效果。