Question by
dalegtfc · May 11, 2017 at 04:09 AM ·
coroutinestartcoroutine
StartCoroutine(getLocation());
Hi,
I am wondering if anyone can help me to updating my GPS co-ordinates real time. I am currently using the following script:
using UnityEngine; using System.Collections; using UnityEngine.UI; using Kudan.AR.Samples;
public class DetectLocation : MonoBehaviour {
private Vector2 targetCoordinates;
private Vector2 deviceCoordinates;
private float distanceFromTarget = 0.00004f;
private float proximity = 0.001f;
private float sLatitude, sLongitude;
public float dLatitude = 53.54876f, dLongitude = -0.1091547f;
private bool enableByRequest = true;
public int maxWait = 10;
public bool ready = false;
public Text text;
public SampleApp sa;
void Start(){
targetCoordinates = new Vector2 (dLatitude, dLongitude);
StartCoroutine (getLocation ());
}
IEnumerator getLocation(){
LocationService service = Input.location;
if (!enableByRequest && !service.isEnabledByUser) {
Debug.Log("Location Services not enabled by user");
yield break;
}
service.Start();
while (service.status == LocationServiceStatus.Initializing && maxWait > 0) {
yield return new WaitForSeconds(1);
maxWait--;
}
if (maxWait < 1){
Debug.Log("Timed out");
yield break;
}
if (service.status == LocationServiceStatus.Failed) {
Debug.Log("Unable to determine device location");
yield break;
} else {
text.text = "Target Location : "+dLatitude + ", "+dLongitude+"\nMy Location: " + service.lastData.latitude + ", " + service.lastData.longitude;
sLatitude = service.lastData.latitude;
sLongitude = service.lastData.longitude;
}
//service.Stop();
ready = true;
startCalculate ();
}
void Update(){
}
public void startCalculate(){
deviceCoordinates = new Vector2 (sLatitude, sLongitude);
proximity = Vector2.Distance (targetCoordinates, deviceCoordinates);
if (proximity <= distanceFromTarget) {
text.text = text.text + "\nDistance : " + proximity.ToString ();
text.text += "\nTarget Detected";
sa.StartClicked ();
} else {
text.text = text.text + "\nDistance : " + proximity.ToString ();
text.text += "\nTarget not detected, too far!";
}
}
}
Thanks!
Comment
Your answer
Follow this Question
Related Questions
The order of the sound played 0 Answers
StartCouroutine doesn't work with If statements? 1 Answer
StartCoroutine() causes NullReferenceException when scenes are changed. 0 Answers
StopCoroutine not working. 0 Answers
Performance Issue with Coroutines 2 Answers