自动更新GPS位置 [英] Auto-updating GPS location

查看:203
本文介绍了自动更新GPS位置的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我当时正在使用Unity的GPS系统,却发现它从未更新过.我必须关闭该应用程序然后再次启动它才能获得当前位置.

I was using Unity's GPS System and noticed that it never updated. I had to close the app and launch it again in order to get my current position.

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();
    }
}

在此自动更新到我当前位置的过程中,我将提供一些帮助,我们将不胜感激!预先感谢!

I would appreciate some help in making this auto-update to my current location! Thanks in advance!

推荐答案

您拥有的代码将显示一次GPS位置,然后调用Input.location.Stop();停止GPS.

The code you have will show the GPS location once then Input.location.Stop(); is called to stop the GPS.

放入

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循环中

,然后在while循环内添加yield return new WaitForSeconds(1);以等待一段时间.执行此操作的另一种方法是启动另一个协程,该协程将随着时间的推移更新GPS位置.

in a while loop then add yield return new WaitForSeconds(1); inside the while loop to wait for some time. Another way to do this is to start another coroutine that will update the GPS location over time.

类似这样的东西:

public class TestLocationService : MonoBehaviour
{

    public GameObject longText;
    public GameObject latText;

    IEnumerator coroutine;

    IEnumerator Start()
    {
        coroutine = updateGPS();

        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;
            StartCoroutine(coroutine);
        }
    }

    IEnumerator updateGPS()
    {
        float UPDATE_TIME = 3f; //Every  3 seconds
        WaitForSeconds updateTime = new WaitForSeconds(UPDATE_TIME);

        while (true)
        {
            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;
            yield return updateTime;
        }
    }

    void stopGPS()
    {
        Input.location.Stop();
        StopCoroutine(coroutine);
    }

    void OnDisable()
    {
        stopGPS();
    }
}

这篇关于自动更新GPS位置的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆