Unity and Android’s GPS

Here’s something that frustrated me for about an hour. Hopefully this will help anyone else who wants to use the GPS with Unity Android since the documentation doesn’t have any examples… not to mention the answer is pretty confusing.

I’ll save you the long story and show you exactly how it’s done. The first thing we have to do is start the iPhoneSettings.StartLocationServiceUpdates function.  Yes, I know you’re on Android… you  have to use this function anyway.  Here’s a test script for you – put this javascript on your main camera and do a test build.


#pragma strict
var gpsPos : LocationInfo;

function Start() {

// Start service before querying location
iPhoneSettings.StartLocationServiceUpdates();

// Wait until service initializes
var maxWait : int = 20;
while (iPhoneSettings.locationServiceStatus ==
LocationServiceStatus.Initializing && maxWait > 0) {
yield WaitForSeconds(1);
maxWait–;
}

// Service didn’t initialize in 20 seconds
if (maxWait < 1) {
print(“Timed out”);
return;
}

// User denied access to device location
if (iPhoneSettings.locationServiceStatus == LocationServiceStatus.Failed) {
print(“User denied access to device location”);
return;
}
// Access granted and location value could be retrieved
else {
print(“Location: ” + iPhoneInput.lastLocation.latitude + ” ” +
iPhoneInput.lastLocation.longitude + ” ” +
iPhoneInput.lastLocation.altitude + ” ” +
iPhoneInput.lastLocation.horizontalAccuracy + ” ” +
iPhoneInput.lastLocation.timestamp);

}

// Initialize our empty LocationInfo object
gpsPos = new LocationInfo();
}

function Update() {

// Update the LocationInfo object with the last information we got from the Location Service
gpsPos = iPhoneInput.lastLocation;

}

function OnGUI() {
// Walk around and look at your screen. You location will update every 5-20 seconds depending on signal strength
GUI.Label(Rect(Screen.width / 2, Screen.height / 2, 200, 100), gpsPos.longitude.ToString() + ” ” + gpsPos.latitude.ToString() + ” ” + gpsPos.altitude.ToString());
}

Hopefully this will solve the mystery for some of you. There’s more you can do – check out the iPhoneSettings function in the Unity Docs.

Advertisements

4 Responses to “Unity and Android’s GPS”


  1. 1 Ryan W. August 4, 2011 at 2:48 am

    Thanks. Any idea how to access the compass?

  2. 2 Timothy Johnson August 4, 2011 at 3:52 pm

    Hey Ryan,

    At the moment there’s no built-in function to access the compass in Unity. (As of 3.3 anyway). Gaining access to the compass means that you’ll have to write a custom Unity plug-in.

    http://unity3d.com/support/documentation/Manual/Plugins.html

    I was thinking of making a tutorial as a blog post, but there’s so much information to cover I haven’t gotten around to it yet.

  3. 3 mike August 22, 2012 at 7:32 pm

    Hi there, it is one year later but the problem seems to remain. I’ve been trying to write an app in javascript/unityscript to use the GPS of my android phone and apart from the iPhoneSettings.StartLocationServiceUpdates(); I have not been able to get access to my GPS although in Unity 3.5 Input.location.Start(); is supposed to work: http://docs.unity3d.com/Documentation/ScriptReference/LocationService.Start.html . Could anyone confirm that you still have to use iPhoneSettings.StartLocationServiceUpdates(); to get access to the GPS?

  4. 4 Softwizz September 7, 2012 at 4:35 pm

    @mike
    I used this code based on your link and it worked and was fairly accurate and showed my location to be 17 houses away from where I actually was.
    I did turn on the GPS and WIFI on my phone (Samsung Galaxy Ace 2).

    Here is the code:

    function Start () {
    // First, check if user has location service enabled
    if (!Input.location.isEnabledByUser)
    return;

    // Start service before querying location
    Input.location.Start ();

    // Wait until service initializes
    var maxWait : int = 20;
    while (Input.location.status
    == LocationServiceStatus.Initializing && maxWait > 0) {
    yield WaitForSeconds (1);
    maxWait–;
    }

    // Service didn’t initialize in 20 seconds
    if (maxWait < 1) {
    print ("Timed out");
    return;
    }

    // Connection has failed
    if (Input.location.status == LocationServiceStatus.Failed) {
    print ("Unable to determine device location");
    return;
    }
    // Access granted and location value could be retrieved
    else {
    print ("Location: " + Input.location.lastData.latitude + " " +
    Input.location.lastData.longitude + " " +
    Input.location.lastData.altitude + " " +
    Input.location.lastData.horizontalAccuracy + " " +
    Input.location.lastData.timestamp);
    }

    // Stop service if there is no need to query location updates continuously
    Input.location.Stop ();
    }

    function OnGUI() {
    // Walk around and look at your screen. You location will update every 5-20 seconds depending on signal strength
    GUI.Label(Rect(Screen.width / 4, 20, 200, 30),"Longitude "+ Input.location.lastData.longitude.ToString());
    GUI.Label(Rect(Screen.width / 4, 60, 200, 30),"Latitude "+ Input.location.lastData.latitude.ToString());
    GUI.Label(Rect(Screen.width / 4, 100, 200, 30),"Altitude "+ Input.location.lastData.altitude.ToString());
    GUI.Label(Rect(Screen.width / 4, 140, 200, 30),"H Accuracy "+ Input.location.lastData.horizontalAccuracy.ToString());
    GUI.Label(Rect(Screen.width / 4, 180, 200, 30),"T Stamp "+ Input.location.lastData.timestamp.ToString());
    }


Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s





%d bloggers like this: