Created
March 29, 2017 06:24
-
-
Save anonymous/ca71bef6eb4af40290dd9bf9707e8f27 to your computer and use it in GitHub Desktop.
This file contains 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
/********* | |
* This Gist was created at CSharpPad.com | |
* To run this file, open http://csharppad.com/gist/0a3f5e5c833e4c983abe7654450f1b9c | |
**********/ | |
using UnityEngine; | |
using UnityEngine.UI; | |
using System; | |
public class GPSCtrl : MonoBehaviour | |
{ | |
public Text distance_label; | |
public GameObject distanceObject; | |
// *** DELETE WHEN DONE - text labels for testing | |
public Text testP1; | |
public Text testP2; | |
public Text testMsg; | |
public Text testMsg2; | |
public GameObject p1Object; | |
public GameObject p2Object; | |
public GameObject test; | |
public GameObject test2; | |
public double long1 = 0; | |
public double lat1 = 0; | |
public double long2 = 0; | |
public double lat2 = 0; | |
public double currLong; | |
public double currLat; | |
public double distanceWalked = 0; | |
void Start () | |
{ | |
distance_label = distanceObject.GetComponent<Text> (); | |
// *** DELETE WHEN DONE - text labels for testing | |
testP1 = p1Object.GetComponent<Text> (); | |
testP2 = p2Object.GetComponent<Text> (); | |
testMsg = test.GetComponent<Text> (); | |
testMsg2 = test2.GetComponent<Text> (); | |
distance_label.text = "You and your blobs have walked 0.00 km this trip."; | |
} | |
void Update () | |
{ | |
GetCurrentLocation (); | |
HasTraveled (); | |
testP1.text = "<Point 1 Coordinates> Lat: " + lat1 + ", Long: " + long1; | |
testP2.text = "<Point 2 Coordinates> Lat: " + lat2 + ", Long: " + long2; | |
// Compare GPS coordinates rounded to 4 decimal places | |
/* | |
if (latRounded1 != latRounded2 || longRounded1 != longRounded2) { | |
testMsg.text = "Lat/Long don't match."; | |
distanceWalked += CalculateDistance (longRounded1, latRounded1, longRounded2, latRounded2); | |
distance_label.text = "You and your blobs have walked " + distanceWalked + " km."; | |
long1 = long2; | |
lat1 = lat2; | |
} | |
*/ | |
} | |
public void GetCurrentLocation() | |
{ | |
testMsg.text = "Location status: " + Input.location.status; | |
if (Input.location.status == LocationServiceStatus.Running) { | |
lat2 = Input.location.lastData.latitude; | |
long2 = Input.location.lastData.longitude; | |
} | |
} | |
public void HasTraveled() | |
{ | |
if (lat1 != lat2 || long1 != long2) { | |
testMsg2.text = "Inside HasTraveled()"; | |
distanceWalked = CalculateDistance (lat1, long1, lat2, long2); | |
distance_label.text = "You and your blobs have walked " + Math.Round (distanceWalked, 2) + " km this trip."; | |
long1 = long2; | |
lat1 = lat2; | |
} | |
} | |
/// <summary> | |
/// Makes a call to start GPS service when Start button is clicked. | |
/// </summary> | |
public void StartButtonClicked () | |
{ | |
Debug.Log ("Start button clicked..."); | |
StartCoroutine (StartLocationService ()); | |
} | |
/// <summary> | |
/// Stops GPS service when Stop button is clicked. | |
/// </summary> | |
public void StopButtonClicked () | |
{ | |
Debug.Log ("Stop button clicked..."); | |
Input.location.Stop (); | |
distance_label.text = "Stopped."; | |
} | |
public void ResetButtonClicked() | |
{ | |
Debug.Log("Reset button clicked..."); | |
distanceWalked = 0; | |
Input.location.Stop (); | |
Debug.Log ("distanceWalked after reset: " + distanceWalked); | |
} | |
/// <summary> | |
/// Starts the location service. | |
/// </summary> | |
IEnumerator StartLocationService () | |
{ | |
// First, check if user has location service enabled | |
if (!Input.location.isEnabledByUser) | |
//yield break; | |
distance_label.text = "Not initialized"; | |
// Start service before querying location | |
Input.location.Start (5f, 5f); // accuracy = 5m, update frequency = 5m | |
// Wait until service initializes | |
int maxWait = 20; | |
while (Input.location.status == LocationServiceStatus.Initializing && maxWait > 0) { | |
yield return new WaitForSeconds (1); | |
maxWait--; | |
} | |
// Service didn't initialize in 20 seconds | |
if (maxWait < 1) { | |
distance_label.text = "Timed out"; | |
yield break; | |
} | |
// Connection has failed | |
if (Input.location.status == LocationServiceStatus.Failed) { | |
distance_label.text = "Unable to determine device location"; | |
yield break; | |
} else { | |
long1 = long2 = Input.location.lastData.longitude; | |
lat1 = lat2 = Input.location.lastData.latitude; | |
// Access granted and location value could be retrieved | |
// distance_label.text = "Location: " + Input.location.lastData.latitude + " " + | |
// Input.location.lastData.longitude + " " + | |
// Input.location.lastData.altitude; | |
} | |
// Stop service if there is no need to query location updates continuously | |
//Input.location.Stop (); | |
} | |
/// <summary> | |
/// Calculate the distance between two geo coordinates. | |
/// </summary> | |
/// <returns>Distance traveled in km.</returns> | |
/// <param name="latitude1">Latitude1.</param> | |
/// <param name="longitude1">Longitude1.</param> | |
/// <param name="latitude2">Latitude2.</param> | |
/// <param name="longitude2">Longitude2.</param> | |
public double CalculateDistance (double latitude1, double longitude1, double latitude2, double longitude2) | |
{ | |
double radiansOverDegrees = (Math.PI / 180.0); | |
double R = 6371; // Earth radius in km | |
double radiansLat1 = latitude1 * radiansOverDegrees; | |
double radiansLong1 = longitude1 * radiansOverDegrees; | |
double radiansLat2 = latitude2 * radiansOverDegrees; | |
double radiansLong2 = longitude2 * radiansOverDegrees; | |
double dLat = radiansLat2 - radiansLat1; | |
double dLong = radiansLong2 - radiansLong1; | |
double a = Math.Sin (dLat / 2) * Math.Sin (dLat / 2) + | |
Math.Sin (dLong / 2) * Math.Sin (dLong / 2) * Math.Cos (radiansLat1) * Math.Cos (radiansLat2); | |
double c = 2 * Math.Atan2 (Math.Sqrt (a), Math.Sqrt (1 - a)); | |
double d = R * c; | |
return d; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment