Created
April 30, 2017 12:55
-
-
Save t04glovern/b10b1fd7888a04a1dba69af2b5da95a6 to your computer and use it in GitHub Desktop.
Unity3D GPS Connector script
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System.Collections; | |
using System.Collections.Generic; | |
using UnityEngine; | |
public class GPSController : MonoBehaviour { | |
public LocationInfo currentGPSInfo; | |
public float updateRate = 3f; | |
public bool debugging = true; | |
private bool gpsInit = false; | |
IEnumerator Init() { | |
if (!Input.location.isEnabledByUser) | |
yield break; | |
Input.location.Start(1, 1); | |
// Wait at max 20 serconds for GPS to init | |
int maxWait = 20; | |
// Init GPS data | |
while(Input.location.status == LocationServiceStatus.Initializing && maxWait > 0) { | |
yield return new WaitForSeconds(1); | |
if(debugging) | |
Debug.Log("GPS Polling (" + maxWait.ToString() + ")"); | |
maxWait--; | |
} | |
// GPS init failed to occur within 20 seconds | |
if(maxWait < 1) { | |
if(debugging) | |
Debug.Log("GPS Timed out"); | |
yield break; | |
} | |
// GPS connection has failed | |
if(Input.location.status == LocationServiceStatus.Failed) { | |
if(debugging) | |
Debug.Log("Unable to determine location"); | |
yield break; | |
} else { | |
// Connection successful | |
// Toggle init boolean | |
gpsInit = true; | |
if(debugging) | |
Debug.Log("GPS init successful"); | |
} | |
} | |
void Start() { | |
StartCoroutine(Init()); | |
InvokeRepeating("UpdateGPSData", 2.0f, updateRate); | |
} | |
void UpdateGPSData() { | |
if(gpsInit) { | |
// Set the currentGPSInfo object to have | |
// the lastest polled GPSData | |
currentGPSInfo = Input.location.lastData; | |
if(debugging) { | |
Debug.Log("Longitude: " + currentGPSInfo.longitude.ToString()); | |
Debug.Log("Latitude: " + currentGPSInfo.latitude.ToString()); | |
Debug.Log("Altitude: " + currentGPSInfo.altitude.ToString ()); | |
Debug.Log("Horizontal Accuracy: " + currentGPSInfo.horizontalAccuracy.ToString()); | |
Debug.Log("Vertical Accuracy: " + currentGPSInfo.verticalAccuracy.ToString ()); | |
Debug.Log("Timestamp: " + currentGPSInfo.timestamp.ToString ()); | |
} | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment