自动更新GPS位置

时间:2017-01-21 19:03:14

标签: c# unity3d

我使用的是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();
    }
}

我很感激帮助我对当前位置进行自动更新!提前谢谢!

1 个答案:

答案 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