user7451270
user7451270

Reputation:

GPS Location on iOS

So I was developing an application in Unity3d 5.4.4f1 similar to Pokemon Go for Android originally, then I recently built it to iOS 10.3 and found out that Input.location doesn't work at all. Here is the code:

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();
    }
}

As I was doing research into the problem, some people say that the state is always in "Initializing" and never changes. Another thing that I thought was happening was that iOS wasn't allowing the app to use location, but I went into the restrictions and set it to "Always" and still nothing.

Upvotes: 0

Views: 2414

Answers (1)

Programmer
Programmer

Reputation: 125245

Starting from iOS 10, it required that you add NSLocationWhenInUseUsageDescription to the info.plist file. This is almost the-same as adding sensor permissions in Android to the Manifest file.

NSLocationWhenInUseUsageDescription = "Your Reason for using this sensor";

You will be allowed to access the GPS sensor when you do this.

Upvotes: 3

Related Questions