Advertisement
Guest User

Untitled

a guest
Apr 30th, 2017
58
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.23 KB | None | 0 0
  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4.  
  5. public class GPSController : MonoBehaviour {
  6.  
  7. public LocationInfo currentGPSInfo;
  8. public float updateRate = 3f;
  9. public bool debugging = true;
  10.  
  11. private bool gpsInit = false;
  12.  
  13. IEnumerator Init() {
  14.  
  15. if (!Input.location.isEnabledByUser)
  16. yield break;
  17.  
  18. Input.location.Start(1, 1);
  19.  
  20. // Wait at max 20 serconds for GPS to init
  21. int maxWait = 20;
  22.  
  23. // Init GPS data
  24. while(Input.location.status == LocationServiceStatus.Initializing && maxWait > 0) {
  25. yield return new WaitForSeconds(1);
  26. if(debugging)
  27. Debug.Log("GPS Polling (" + maxWait.ToString() + ")");
  28. maxWait--;
  29. }
  30.  
  31. // GPS init failed to occur within 20 seconds
  32. if(maxWait < 1) {
  33. if(debugging)
  34. Debug.Log("GPS Timed out");
  35. yield break;
  36. }
  37.  
  38. // GPS connection has failed
  39. if(Input.location.status == LocationServiceStatus.Failed) {
  40. if(debugging)
  41. Debug.Log("Unable to determine location");
  42. yield break;
  43. } else {
  44. // Connection successful
  45. // Toggle init boolean
  46. gpsInit = true;
  47. if(debugging)
  48. Debug.Log("GPS init successful");
  49. }
  50. }
  51.  
  52. void Start() {
  53. StartCoroutine(Init());
  54. InvokeRepeating("UpdateGPSData", 2.0f, updateRate);
  55. }
  56.  
  57. void UpdateGPSData() {
  58.  
  59. if(gpsInit) {
  60. // Set the currentGPSInfo object to have
  61. // the lastest polled GPSData
  62. currentGPSInfo = Input.location.lastData;
  63.  
  64. if(debugging) {
  65. Debug.Log("Longitude: " + currentGPSInfo.longitude.ToString());
  66. Debug.Log("Latitude: " + currentGPSInfo.latitude.ToString());
  67. Debug.Log("Altitude: " + currentGPSInfo.altitude.ToString ());
  68. Debug.Log("Horizontal Accuracy: " + currentGPSInfo.horizontalAccuracy.ToString());
  69. Debug.Log("Vertical Accuracy: " + currentGPSInfo.verticalAccuracy.ToString ());
  70. Debug.Log("Timestamp: " + currentGPSInfo.timestamp.ToString ());
  71. }
  72. }
  73. }
  74. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement