2017-01-21 166 views
1

我使用的是Unity的GPS系統,並注意到它從未更新過。我不得不關閉應用程序並重新啓動它以獲取當前位置。自動更新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

你指望它來更新?你有什麼嘗試? – Jecoms

回答

1

您所擁有的密碼將顯示一次GPS位置,然後撥打Input.location.Stop();即可停止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循環再加入yield return new WaitForSeconds(1);while循環中等待一段時間。另一種方法是啓動另一個協程,隨着時間的推移更新GPS位置。

事情是這樣的:

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

出於某種原因,應用程序首次打開後崩潰,然後我不得不重新打開它,此時它在大約20秒後崩潰。每次我打開它後,它也會在20秒後崩潰,直到我完全退出它,然後立即崩潰。它確實顯示我的位置數據。順便說一下,我在Android棒棒糖上。 –

+0

我沒有看到任何可以在這裏崩潰的應用程序。你的Unity版本是什麼?你可以嘗試在另一個Android設備上試試這個,看看崩潰是否仍然存在? – Programmer

+0

我正在使用Unity 5.5.0f3,我在另一個Android設備上嘗試了它,發生了同樣的事情。 –