我使用的是Unity的GPS系统并注意到它从未更新过。我不得不关闭应用程序并再次启动它以获得我当前的位置。
using UnityEngine;
using UnityEngine.UI;
using System.Collections;
public class TestLocationService : MonoBehaviour {
public GameObject longText;
public GameObject latText;
IEnumerator Start() {
Text longitudeText = longText.GetComponent<Text>();
Text latitudeText = latText.GetComponent<Text>();
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("Timed out");
yield break;
}
if (Input.location.status == LocationServiceStatus.Failed) {
print("Unable to determine device location");
yield break;
} else {
print("Location: " + Input.location.lastData.latitude + " " + Input.location.lastData.longitude + " " + Input.location.lastData.altitude + " " + Input.location.lastData.horizontalAccuracy + " " + Input.location.lastData.timestamp);
longitudeText.text = "Longitude: " + Input.location.lastData.longitude;
latitudeText.text = "Latitude: " + Input.location.lastData.latitude;
}
Input.location.Stop();
}
}
我很感激帮助我对当前位置进行自动更新!提前谢谢!
答案 0 :(得分:1)
您拥有的代码会显示GPS位置,然后调用ColorModel
来停止GPS。
放
Input.location.Stop();
在print("Location: " + Input.location.lastData.latitude + " " + Input.location.lastData.longitude + " " + Input.location.lastData.altitude + " " + Input.location.lastData.horizontalAccuracy + " " + Input.location.lastData.timestamp);
longitudeText.text = "Longitude: " + Input.location.lastData.longitude;
latitudeText.text = "Latitude: " + Input.location.lastData.latitude;
循环中,然后在while
循环中添加yield return new WaitForSeconds(1);
等待一段时间。另一种方法是启动另一个协程,它将随着时间的推移更新GPS位置。
这样的事情:
while