所以我最初在Unity3d 5.4.4f1中开发了类似于Pokemon Go for Android的应用程序,然后我最近将它构建到iOS 10.3并发现Input.location
根本不起作用。这是代码:
public class Locate : MonoBehaviour {
public float latitude;
public float longitude;
IEnumerator coroutine;
IEnumerator Start() {
coroutine = updateGPS();
if (!Input.location.isEnabledByUser)
yield break;
Input.location.Start();
int maxWait = 4;
while (Input.location.status == LocationServiceStatus.Initializing && maxWait > 0) {
yield return new WaitForSeconds(1);
maxWait--;
}
if (maxWait < 1) {
print("Timed out");
yield break;
}
if (Input.location.status == LocationServiceStatus.Failed) {
print("Unable to determine device location");
yield break;
} else {
print("Location: " + Input.location.lastData.latitude + " " + Input.location.lastData.longitude + " " + Input.location.lastData.altitude + " " + Input.location.lastData.horizontalAccuracy + " " + Input.location.lastData.timestamp);
longitude = Input.location.lastData.longitude;
latitude = Input.location.lastData.latitude;
StartCoroutine(coroutine);
}
}
IEnumerator updateGPS() {
float UPDATE_TIME = 1f;
WaitForSeconds updateTime = new WaitForSeconds(UPDATE_TIME);
while (true) {
print("Location: " + Input.location.lastData.latitude + " " + Input.location.lastData.longitude + " " + Input.location.lastData.altitude + " " + Input.location.lastData.horizontalAccuracy + " " + Input.location.lastData.timestamp);
longitude = Input.location.lastData.longitude;
latitude = Input.location.lastData.latitude;
yield return updateTime;
}
}
void stopGPS() {
Input.location.Stop();
StopCoroutine(coroutine);
}
void OnDisable() {
stopGPS();
}
}
当我正在研究这个问题时,有些人说国家总是处于“初始化”状态,并且永远不会改变。我认为发生的另一件事是iOS不允许应用程序使用位置,但我进入限制并将其设置为“始终”,但仍然没有。
答案 0 :(得分:3)
从iOS 10开始,它要求您将 NSLocationWhenInUseUsageDescription 添加到 info.plist 文件中。这与将Android中的传感器权限添加到Manifest文件几乎相同。
NSLocationWhenInUseUsageDescription =&#34;您使用此传感器的原因&#34;;
执行此操作时,您将被允许访问GPS传感器。