-
Notifications
You must be signed in to change notification settings - Fork 0
/
get_location.cs
63 lines (54 loc) · 1.69 KB
/
get_location.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class get_location : MonoBehaviour
{
public static get_location Instance { set; get; }
public float latitude;
public float longitude;
public float altitude;
private void Start()
{
Instance = this;
DontDestroyOnLoad(gameObject);
StartCoroutine(StartLocationService());
}
private IEnumerator StartLocationService()
{
// First, check if user has location service enabled
if (!Input.location.isEnabledByUser)
{
Debug.Log("GPS not enabled");
yield break;
}
// Start service before querying location
Input.location.Start();
// 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 <= 0)
{
Debug.Log("Timed out");
yield break;
}
// Connection has failed
if (Input.location.status == LocationServiceStatus.Failed)
{
Debug.Log("Unable to determine device location");
yield break;
}
// Set locational infomations
while (true)
{
latitude = Input.location.lastData.latitude;
longitude = Input.location.lastData.longitude;
altitude = Input.location.lastData.altitude;
yield return new WaitForSeconds(10);
}
}
}